Recent

Author Topic: Formatting in FreePascal  (Read 12400 times)

Zoran

  • Hero Member
  • *****
  • Posts: 1831
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Formatting in FreePascal
« Reply #75 on: July 31, 2019, 01:25:29 pm »
"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:

Code: Pascal  [Select][+][-]
  1.   with TBarSeries(Result) do
  2.     case APlotType of
  3.       ptBars:
  4.         begin
  5.           AxisIndexX := AChart.LeftAxis.Index;
  6.           AxisIndexY := AChart.BottomAxis.Index;
  7.         end;
  8.       ptWhatever:
  9.         begin
  10.           AxisIndexX := something;
  11.           AxisIndexY := something_else;
  12.         end;
  13.       else
  14.           AxisIndexX := AChart.BottomAxis.Index;
  15.           AxisIndexY := AChart.LeftAxis.Index;
  16.     end; // this ends case
  17.   end; // what does this one do here?

Will not compile.

440bx

  • Hero Member
  • *****
  • Posts: 4067
Re: Formatting in FreePascal
« Reply #76 on: July 31, 2019, 01:28:41 pm »
@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".

At least in those cases, the compiler will emit an error but, that isn't always the case as one of the above examples demonstrates.


(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Formatting in FreePascal
« Reply #77 on: July 31, 2019, 01:35:07 pm »
"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:
You are right - "with" does require a begin/end. Apologies for posting a misleading statement and uncompilable code.
What I should have posted was the following:
Code: Pascal  [Select][+][-]
  1. with TBarSeries(Result) do
  2.   case APlotType of
  3.     ptBars:
  4.       begin
  5.         AxisIndexX := AChart.LeftAxis.Index;
  6.         AxisIndexY := AChart.BottomAxis.Index;
  7.       end;
  8.     ptWhatever:
  9.       begin
  10.         AxisIndexX := something;
  11.         AxisIndexY := something_else;
  12.       end;
  13.     else
  14.         AxisIndexX := AChart.BottomAxis.Index;
  15.         AxisIndexY := AChart.LeftAxis.Index;
  16.   end;
Although this will never satisfy indentation purists, 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.
« Last Edit: July 31, 2019, 02:54:22 pm by howardpc »

440bx

  • Hero Member
  • *****
  • Posts: 4067
Re: Formatting in FreePascal
« Reply #78 on: July 31, 2019, 03:30:18 pm »
What I should have posted was the following:
Code: Pascal  [Select][+][-]
  1. with TBarSeries(Result) do
  2.   case APlotType of
  3.     ptBars:
  4.       begin
  5.         AxisIndexX := AChart.LeftAxis.Index;
  6.         AxisIndexY := AChart.BottomAxis.Index;
  7.       end;
  8.     ptWhatever:
  9.       begin
  10.         AxisIndexX := something;
  11.         AxisIndexY := something_else;
  12.       end;
  13.     else
  14.         AxisIndexX := AChart.BottomAxis.Index;
  15.         AxisIndexY := AChart.LeftAxis.Index;
  16.   end;
Although this will never satisfy indentation purists
Change "indentation" to "language" and I can agree with that statement.


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'd format it similarly:
Code: Pascal  [Select][+][-]
  1. with TBarSeries(Result) do
  2. case APlotType of
  3.   ptBars:
  4.   begin
  5.    AxisIndexX := AChart.LeftAxis.Index;
  6.    AxisIndexY := AChart.BottomAxis.Index;
  7.   end;
  8.  
  9.   ptWhatever:
  10.   begin
  11.     AxisIndexX := something;
  12.     AxisIndexY := something_else;
  13.   end;
  14.  
  15. else
  16.   AxisIndexX := AChart.BottomAxis.Index;
  17.   AxisIndexY := AChart.LeftAxis.Index;
  18. end;
The above formatting for the following reasons: "case"/"else"/"end" aligned because they are a single Pascal construct.  "case" aligned with "with" because the "case" belongs to the "with".   "begin"/end" aligned with the conditions because that's what they are associated with (owned by.)

if Delphi supported "otherwise" in "case" statements, I would use "otherwise" to make it visually noticeable that the statements under it are not associated with an "if".

One thing I find particularly interesting is that, probably NONE of the programmers that use "begin" on the same line as an "else" (or something else) code something like this:
Code: Pascal  [Select][+][-]
  1. procedure MyProc; begin
  2. end;
which indicates that their formatting choice is not particularly consistent and lacks a solid foundation.  Putting the "begin" on the same line as "procedure" would after all, save one line, just as it does in other constructions.
« Last Edit: July 31, 2019, 03:32:48 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: Formatting in FreePascal
« Reply #79 on: August 01, 2019, 09:21:31 am »
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.
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.

 

TinyPortal © 2005-2018