One of the reasons for success of Python is its code formatting/indentation.You may be right but, personally, I doubt it.
I find that Pascal formatting, though very clear, can be further simplified to Python-type formatting.When I find code formatted in the "python way" you suggested, the first thing I do is reformat it to make its logical blocks more evident.
The modified version results in less lines (separate line for begin is avoided) and less indentation (indentation for begin and end are avoided) so that 3 statement blocks are seen clearly.A separate line for "begin", not only should _not_ be avoided, it should be required because it marks the beginning of a block which should be aligned with the keyword that marks its end (in this case "end".)
I am interested in views of other users on this forum.Good indentation can make a program much easier to read and understand.
One of the reasons for success of Python is its code formatting/indentation. I find that Pascal formatting, though very clear, can be further simplified to Python-type formatting.Python requires proper formatting as there it is part of the syntax. In other languages and especially Pascal this is not the case as everyone can choose their own style. Thus "measuring" the success of Pascal on that is nonsense.
By the way, when not working on the compiler, this is how I format my code:The problem with that formatting is that it shows an "if" terminated with two "end"(s). If statements are not terminated with "end"(s).
with TBarSeries(Result) do begin <snip> if APlotType = ptBars then begin AxisIndexX := AChart.LeftAxis.Index; AxisIndexY := AChart.BottomAxis.Index; end else begin AxisIndexX := AChart.BottomAxis.Index; AxisIndexY := AChart.LeftAxis.Index; end; end;
The modified version results in less lines (separate line for begin is avoided) and less indentation (indentation for begin and end are avoided) so that 3 statement blocks are seen clearly.
I am interested in views of other users on this forum.
This way, to scan structure validity you have to constantly move your eyes from left to right to catch all those begins. Pascal isn't a tennis game.The visual braces are build by "if [...]" and "end". You can see it from the indentation, that its a block. So there has to be a begin in the end of the line, no need to search for it.
Moreover while a known alternative, it is the minority position, and going against majority rules is generally a bad thing anyway.Thats true. Especially if you work on more than one codebase.
@PascalDragon : you are 50% with Python-type formatting!
This way, to scan structure validity you have to constantly move your eyes from left to right to catch all those begins. Pascal isn't a tennis game.The visual braces are build by "if [...]" and "end". You can see it from the intendation, that its a block. So there has to be a begin in the end of the line, no need to search for it.
We often have to use "//do" after 'end's to clarify. This is not needed in pythonic (or whatever name it can be given) format:
Additionally, statements that use then exit, then continue, then break or then goto Somelabel should always be written on 2 lines
(snip)
Reason : in all these cases there is a real code flow change and enhancing the visual aspect helps find errors.
Having an important instruction just stuck at the end of a line makes code more difficult to read.
More importantly, you need them on separate lines to be able to put breakpoints on the THEN constructs. So that is not a matter of taste. At least not with the current debugging support. (but Delphi is no different)The break point is not a good reason because with the following code
I prefer https://wiki.delphi-jedi.org/wiki/Project_JEDI_Delphi_Language_Style_Guide (https://wiki.delphi-jedi.org/wiki/Project_JEDI_Delphi_Language_Style_Guide). All Delphi sources use the same coding style. For example "Never place a begin statement on the same line with any other code".I just read the supplied page.
Well, I align my code to the right, :D
That is surprisingly readable!!! :D
I find the whitespace formatting in Python a little unsettling, especially if I cut and paste code, I like to find where I am.Yes, but in Python the whitespace formatting is part of the language, even its core concept.
Yes, but in Python the whitespace formatting is part of the language, even its core concept.
In other languages it is a matter of taste. In Pascal you can leave almost any whitespace except separators out and it still compiles:
program helloworld;{$mode delphi}uses sysutils;begin writeln('Hello, World');end.
Which is, to stay on subject, an alternative way of formatting.. :D :D O:-)
So? When there are two ends I implicitly know that there's an else inbetween. And with the indentation I can quickly determine where it is. Same with more complex if-constructs.By the way, when not working on the compiler, this is how I format my code:The problem with that formatting is that it shows an "if" terminated with two "end"(s). If statements are not terminated with "end"(s).
with TBarSeries(Result) do begin <snip> if APlotType = ptBars then begin AxisIndexX := AChart.LeftAxis.Index; AxisIndexY := AChart.BottomAxis.Index; end else begin AxisIndexX := AChart.BottomAxis.Index; AxisIndexY := AChart.LeftAxis.Index; end; end;
One of the most unreadable unit is \rtl\inc\objpas.inc with strange indent and spurious empty lines, others use a 1 character indent, these are awful and spells much doubt on the FPC developer comprehension of its code, I wont cite name ...Don't forget that most code of the RTL is grown code, more often than not from a time before a general code formatting settled... And doing a reformatting would break up the history when doing a svn blame which is more often than not more important to have than proper formatting.
So? When there are two ends I implicitly know that there's an else inbetween. And with the indentation I can quickly determine where it is. Same with more complex if-constructs.And the programmer who reads that wrongly formatted code (ifs are not terminated with end), he/she is supposed to "implicitly" know there is an "else" in between ?
Do an F5 on line 8. It will never stop there, at least with my current version of Lazarus. If it has changed, please tell because I might upgrade to something newer in trunk.
One bug is not a counterexample, and did you disable optimization?Effectively I always compile -O1 -OoREGVAR that gives very good speed and is compact. This avoids uncertain optimizations at higher levels.
Delphi also has such issues when optimization are on. They are less pronounced, but they are there.One would have to ask to the king of the debugger.
It can't be avoided I guess.
An other thing is the file names. A file name uFancyControl is much more readable than ufancycontrol. But Lazarus always saves them in lower case. If saved twice with capital letters, then at least in the Editor window the name is shown in the specified caseness, while the file name still is in lower case. Not sure if this a bug or a feature...
A file name uFancyControl is much more readable than ufancycontrol. But Lazarus always saves them in lower case. If saved twice with capital letters, then at least in the Editor window the name is shown in the specified caseness, while the file name still is in lower case. Not sure if this a bug or a feature...An important feature, if you do not only work on Windows.
I think I will change the setting, use upper cases and - earlier or later - fall into the trap~
I like to write Procedure and Function with capital P and F. But the automatic creation of the function body with Shift+Ctrl+C not only creates the new one with lower cases, but also changes all other functions/procedures to lower case p and f.
Some Lazarus features enforce lower case, which I really don't like. E.g. I like to write Procedure and Function with capital P and F. But the automatic creation of the function body with Shift+Ctrl+C not only creates the new one with lower cases, but also changes all other functions/procedures to lower case p and f.You can customise this in IDE Options->Codetools, Words page. Just set the "Keyword policy" radiogroup button to "Lowercase, first letter up".
There is no wrongly formatted code, because formatting is not fixed in Pascal, it's not part of the language. There are only guidelines. And every project can choose it's own style to adhere to.So? When there are two ends I implicitly know that there's an else inbetween. And with the indentation I can quickly determine where it is. Same with more complex if-constructs.And the programmer who reads that wrongly formatted code (ifs are not terminated with end), he/she is supposed to "implicitly" know there is an "else" in between ?
And if you need to add another "if" after that "begin" where is the "end" of that "if" going to be ?... is it going to be a third "end" to the starting if ?
I'll use this style and nothing can make me change my mind there.I expected no different. I would have considered changing your mind a miracle beyond Christ's second coming.
There is no wrongly formatted code, because formatting is not fixed in Pascal, it's not part of the language. There are only guidelines. And every project can choose it's own style to adhere to.Nice try but, there is wrongly formatted code. The quintessential example is
That is wonderful (not!)
with TBarSeries(Result) do begin <snip> if APlotType = ptBars then begin AxisIndexX := AChart.LeftAxis.Index; AxisIndexY := AChart.BottomAxis.Index; end else if APlotType = ptWhatEver then begin // whatever end else begin AxisIndexX := AChart.BottomAxis.Index; AxisIndexY := AChart.LeftAxis.Index; end; end;
Nice try but, there is wrongly formatted code.
.......The formatting you use gives the impression "if" statements are terminated by "end" and,......No, to me it gives the impression that if - begin statements are terminated by end. The bolding makes that so easy.
I'm not getting excited but, it does bother me when indentation does not reflect the structure of the language.Nice try but, there is wrongly formatted code.Oh, lets not get too excited here.
Formatting in Pascal is about style,I honestly don't believe that. In Pascal, "begin" is terminated with an "end", because of that, they should be at the same level. This isn't a choice I make, it's inherent in the structure of the language. I know that "deforming" the language structure is commonly done and swept under the rug of "style" but, I don't consider that to be valid or justified.
PascalDragon's (and my) preferred style is just that, our preference.I understand that but, it certainly isn't the preference of the compiler. If someone chose to write a piece of code like this:
It compiles fineYes, it does but, it visually breaks the structure of the language. That's the part that is objectionable.
and is, to us at least, more readable.I cannot figure out how some people find that "style" more readable. You have to scan more text from left to right to mentally build the blocks because "begin" is not aligned with "end". Because the formatting doesn't reflect the structure of the language, the reader has to mentally scan and restructure the text for it to match the structural semantics of the language. It's a lot more work for the person who reads the code.
If you are uncomfortable reading it, its just because its not a style you use. If you found yourself working in a team where that style was required, I am quite sure you would adapt to it quite quickly, perhaps not prefer it but you would have no trouble reading in.I honestly wouldn't work as part of a team that required that "style". I'd wish them good luck and work someplace else.
There are heaps of other 'personalisations' we all use, capitals, CamelCase etc.There should be a reason for a "personalization" to exist. For instance, it is quite common to uppercase constants. The reason is make it obvious anywhere in the program they are a constant. I consider such "personalizations" to be justified and useful.
I cannot figure out how some people find that "style" more readable....
I honestly wouldn't work as part of a team that required that "style". I'd wish them good luck and work someplace else.
Consider this
if <somecondition> then begin // not a singularity point, calculate the derivative at the point <statement> <statement> end else // a singularity point, inform the reader how it will be handled. begin <statement> <statement> end;
The real problem is that a lot of indentation "styles" have no justification other than "I like this way". That's not much of a solid foundation to justify it.
But honestly, we are fortunate that you are free to do it your way and I am free to do it my way, agree ?Yes, I definitely agree with that.
Davo
with TBarSeries(Result) do begin <snip> if APlotType = ptBars then begin AxisIndexX := AChart.LeftAxis.Index; AxisIndexY := AChart.BottomAxis.Index; end else if APlotType = ptWhatEver then begin // whatever end else begin AxisIndexX := AChart.BottomAxis.Index; AxisIndexY := AChart.LeftAxis.Index; end; end;
There is one problem with your argumentation: I wouldn't format it like that. Instead it would be this:There is no wrongly formatted code, because formatting is not fixed in Pascal, it's not part of the language. There are only guidelines. And every project can choose it's own style to adhere to.Nice try but, there is wrongly formatted code. The quintessential example iswhich is wrong because it incorrectly pretends the else associates with the outer if instead of the inner one. The formatting you use gives the impression "if" statements are terminated by "end" and, as a compiler developer, you know very well they are not. Your formatting is wrong because it violates the language's structure and, you can't slide that under the "style" rug.
if <condition> then if <anothercondition> then <statement> else <statement>
There is one problem with your argumentation: I wouldn't format it like that. Instead it would be this:Of course there is, it was an example to show there is such a thing as incorrect formatting. I'm pleased you see the problem.
And if you need to add another "if" after that "begin" where is the "end" of that "if" going to be ?... is it going to be a third "end" to the starting if ?
Correct:
with TBarSeries(Result) do begin <snip> if APlotType = ptBars then begin AxisIndexX := AChart.LeftAxis.Index; AxisIndexY := AChart.BottomAxis.Index; end else if APlotType = ptWhatEver then begin // whatever end else begin AxisIndexX := AChart.BottomAxis.Index; AxisIndexY := AChart.LeftAxis.Index; end; end;
In Pascal a formatting style can't be incorrect. The code can be incorrect.When the formatting "style" alters the language structure to pretend that "if" statements are terminated by "end", it is incorrect, in turn that often leads to incorrect code as the above showed.
I repeatYes, you are good at repeating.
I waited a few days to see if anyone who uses that "style" would notice that the above is _incorrect_.Those that use that style simply don't care that you think it's “incorrect”. 8-)
Various replies in this thread had already made that quite evident.I waited a few days to see if anyone who uses that "style" would notice that the above is _incorrect_.Those that use that style simply don't care that you think it's “incorrect”. 8-)
And if you need to add another "if" after that "begin" where is the "end" of that "if" going to be ?... is it going to be a third "end" to the starting if ?
Correct:
with TBarSeries(Result) do begin <snip> if APlotType = ptBars then begin AxisIndexX := AChart.LeftAxis.Index; AxisIndexY := AChart.BottomAxis.Index; end else if APlotType = ptWhatEver then begin // whatever end else begin AxisIndexX := AChart.BottomAxis.Index; AxisIndexY := AChart.LeftAxis.Index; end; end;
Its the same.No, not the same. The statements
Edit: And that shows the beauty of that formatting, no nesting. Just a simple if..elseif..else..end structure.When introducing bugs in code is considered "beautiful" that formatting will be "beautiful" too.
Yes, you're right, it's not the same.We're making progress. That's good.
But your example is useless.It wasn't an example. It was a request that an "if" statement be added.
Of course you can add a new level of ifs, but thats not a matter of formatting. This was about inserting an "else if" and than the code is correct.The formatting is the main cause why when that "if" was added a bug was introduced.
Yeah, I couldn't resist ... :DThat's better than upside-down :D
Now the conditions that control when the statementsYou are aware that I merely added the if-statement to illustrate the formatting and did not care at all about the functionality of the code?get executed are no longer what they should be.
AxisIndexX := AChart.BottomAxis.Index; AxisIndexY := AChart.LeftAxis.Index;
However, I have no doubt that's what you intended. Changing the conditions that control the execution of the statements is included in the formatting "style" too.
You are aware that I merely added the if-statement to illustrate the formatting and did not care at all about the functionality of the code?Of course, why would anyone care about the functionality, it's so much more important to save a line or two.
@howardpc: Line 17 end at the same level as with but no begin 8)Yes, yes when "begin" is on a separate line, the probability of such an error is less, because only "case" and "begin" end on the "end". In other cases, this is a mistake. And if you write "begin" anywhere - you have to carefully read each complex structure.
I totally agree with you. Except the indentation of the else clause and that bug it contains :P (no begin end)An else clause within a case construct does not require a begin/end since the "else" and the case's "end" are sufficient.
"with" and "case" are both terminated by "end" correctly.
Neither needs a corresponding "begin".
Not every "end" has a matching "begin".
@howardpc: Line 17 end at the same level as with but no begin 8)And that illustrates another one of the problems with that incorrect formatting. The programmers that use it get used to seeing "end" associated with keywords they don't associate with, which makes it much easier to omit a necessary "begin".
You are right - "with" does require a begin/end. Apologies for posting a misleading statement and uncompilable code."with" and "case" are both terminated by "end" correctly.
Neither needs a corresponding "begin".
Not every "end" has a matching "begin".
You are right for "case" statement, but not for "with", so there is one "end" more than there should be in your code. See:
What I should have posted was the following:Change "indentation" to "language" and I can agree with that statement.Although this will never satisfy indentation purists
with TBarSeries(Result) do case APlotType of ptBars: begin AxisIndexX := AChart.LeftAxis.Index; AxisIndexY := AChart.BottomAxis.Index; end; ptWhatever: begin AxisIndexX := something; AxisIndexY := something_else; end; else AxisIndexX := AChart.BottomAxis.Index; AxisIndexY := AChart.LeftAxis.Index; end;
I find it draws attention in a helpful way to the most important parts of the code (the actions that should follow from each case selector) while relegating the boilerplate stuff (begins and ends) to a less significant position in the construct.That's reasonable.
I merely replied with what it would look like with an additional if-condition. Adding an additional condition to that code of course changes what the code does. But that was not the point, the point was to show how I format such code.You are aware that I merely added the if-statement to illustrate the formatting and did not care at all about the functionality of the code?Of course, why would anyone care about the functionality, it's so much more important to save a line or two.