Recent

Author Topic: Formatting in FreePascal  (Read 12079 times)

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Formatting in FreePascal
« Reply #15 on: July 22, 2019, 11:57:52 am »
It looks too colored initially, and it took some time to get used to it. But now I think that the outlining feature of the IDE is very helpful in finding non-matching "end"s in longer code blocks ("IDE options" > "Editor" > "Display" > "Markup and Matches" > Check "Outline (global)". For the vertical lines to work correctly it is required that block start and end are aligned.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Formatting in FreePascal
« Reply #16 on: July 22, 2019, 12:02:48 pm »
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)

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: Formatting in FreePascal
« Reply #17 on: July 22, 2019, 12:31:14 pm »
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
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.   procedure Test;
  3.   var
  4.     aDbgInt: integer = 0;
  5.   begin
  6.     repeat
  7.       if aDbgInt = 8 then
  8.         break;  // Exit is the same but continue will break (maybe because is a procedure)
  9.       inc(aDbgInt,2);
  10.     until False;
  11.     aDbgInt := 2;
  12.   end;
  13. begin
  14.   Test;
  15. end.
  16.  
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.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Formatting in FreePascal
« Reply #18 on: July 22, 2019, 12:55:48 pm »
I prefer 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".

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Formatting in FreePascal
« Reply #19 on: July 22, 2019, 01:07:45 pm »
More importantly, whatever your own opinions are (and I have a list of critiques there too), the Borland style guide (on which Jedi* is based) are the majority option. Period.

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: Formatting in FreePascal
« Reply #20 on: July 22, 2019, 01:37:24 pm »
I prefer 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.
I adhere to most of it, except for the simple condition with begin on the same line but would certainly not have a fight on that. I just find that correct indentation of the end/until matching the if/repeat/while... position conveys sufficiently the code flow and sparing 1 line allows to have a better on screen readability (more code visible).
One I find pretty difficult to like is the
Code: Pascal  [Select][+][-]
  1.   // CORRECT
  2.   if Condition then
  3.   begin
  4.     DoThis;
  5.   end else
  6.   begin
  7.     DoThat;
  8.   end;
  9.  
I find that the most relevant conditional structure flow information is the else and as such should be the leftmost statement.
I would unscrupulously write it :
Code: Pascal  [Select][+][-]
  1.   if Condition then begin
  2.     DoThis;
  3.   end
  4.   else begin
  5.     DoThat;
  6.   end;
  7.  
  8.  
But at a certain point we fall in personal taste and sure have things to change. I'm never very happy with my variable names ...

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: Formatting in FreePascal
« Reply #21 on: July 22, 2019, 01:40:28 pm »
Well, I align my code to the right, :D:
Code: Pascal  [Select][+][-]
  1.                                   with TBarSeries(Result) do
  2.                                                        begin
  3.                                  BarBrush.Color := AColor;
  4.                                    BarPen.Color := AColor;
  5.                             BarWidthStyle := bwPercentMin;
  6.                                 if APlotType = ptBars then
  7.                                                      begin
  8.                     AxisIndexX := AChart.LeftAxis.Index;
  9.                   AxisIndexY := AChart.BottomAxis.Index;
  10.                                                        end
  11.                                                       else
  12.                                                      begin
  13.                   AxisIndexX := AChart.BottomAxis.Index;
  14.                     AxisIndexY := AChart.LeftAxis.Index;
  15.                                                       end;
  16.                                                         end;
Yours Sincerely
Kai Burghardt

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Formatting in FreePascal
« Reply #22 on: July 22, 2019, 01:48:28 pm »
That is surprisingly readable!!!  :D
Specialize a type, not a var.

rnfpc

  • Full Member
  • ***
  • Posts: 101
Re: Formatting in FreePascal
« Reply #23 on: July 22, 2019, 02:21:22 pm »
I find following to be easiest to see blocks:

Code: Pascal  [Select][+][-]
  1.  if Condition then begin
  2.     DoThis;
  3.     end
  4.  else begin
  5.     DoThat;
  6.     end;

Above clearly shows block below 'if' and one below 'else'.

It is obviously what one is used to work with. It will be interesting to find what beginners in this forum prefer.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Formatting in FreePascal
« Reply #24 on: July 22, 2019, 03:52:26 pm »
Well, I align my code to the right, :D
That is surprisingly readable!!!  :D

And surprisingly beautiful :D
Though it might depend quite a lot on where is your right margin :P
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

rnfpc

  • Full Member
  • ***
  • Posts: 101
Re: Formatting in FreePascal
« Reply #25 on: July 22, 2019, 04:47:39 pm »
A related aspect is indentation/formatting for functions/procedures. Classical is:

Code: Pascal  [Select][+][-]
  1. procedure FFT_2(var Z: array of TComplex);
  2. var
  3.   T1: TComplex;
  4. begin
  5.   T1   := ComplexAdd(Z[0], Z[1]);
  6.   Z[1] := ComplexSub(Z[0], Z[1]);
  7.   Z[0] := T1;
  8. end;
  9.  
  10. procedure FFT_3(var Z: array of TComplex);
  11. var
  12.   T1, M1, M2, S1: TComplex;
  13. begin
  14.   T1   := ComplexAdd(Z[1], Z[2]);
  15.   Z[0] := ComplexAdd(Z[0], T1);
  16.   M1   := ComplexScl(c31, T1);
  17.   M2.Re := c32 * (Z[1].Im - Z[2].Im);
  18.   M2.Im := c32 * (Z[2].Re - Z[1].Re);
  19.   S1   := ComplexAdd(Z[0], M1);
  20.   Z[1] := ComplexAdd(S1, M2);
  21.   Z[2] := ComplexSub(S1, M2);
  22. end;

But following may be clearer:
Code: Pascal  [Select][+][-]
  1. procedure FFT_2(var Z: array of TComplex);
  2.   var
  3.     T1: TComplex;
  4.   begin
  5.     T1   := ComplexAdd(Z[0], Z[1]);
  6.     Z[1] := ComplexSub(Z[0], Z[1]);
  7.     Z[0] := T1;
  8.   end;
  9.  
  10. procedure FFT_3(var Z: array of TComplex);
  11.   var
  12.     T1, M1, M2, S1: TComplex;
  13.   begin
  14.     T1   := ComplexAdd(Z[1], Z[2]);
  15.     Z[0] := ComplexAdd(Z[0], T1);
  16.     M1   := ComplexScl(c31, T1);
  17.     M2.Re := c32 * (Z[1].Im - Z[2].Im);
  18.     M2.Im := c32 * (Z[2].Re - Z[1].Re);
  19.     S1   := ComplexAdd(Z[0], M1);
  20.     Z[1] := ComplexAdd(S1, M2);
  21.     Z[2] := ComplexSub(S1, M2);
  22.   end;
« Last Edit: July 22, 2019, 04:49:16 pm by rnfpc »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Formatting in FreePascal
« Reply #26 on: July 22, 2019, 05:14:28 pm »
There's code folding for that.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Formatting in FreePascal
« Reply #27 on: July 22, 2019, 05:25:08 pm »
Interesting. I format exactly as PascalDragon does. I find it compact and readable, with each block indented by 2.

My style, as many, predates Python. I find the whitespace formatting in Python a little unsettling, especially if I cut and paste code, I like to find where I am.

For example, I recently cut java code from a pdf of Yaroslavskiy's 2009 "Dual-Pivot Quicksort algorithm" paper. Pasting it produced a single line of code, but it was relatively quick work to get it formatted again (and then converted to Pascal :)).
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Formatting in FreePascal
« Reply #28 on: July 22, 2019, 05:34:20 pm »
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.
In other languages it is a matter of taste. In Pascal you can leave almost any whitespace except separators out and it still compiles:
Code: Pascal  [Select][+][-]
  1. 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:-)
Specialize a type, not a var.

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Formatting in FreePascal
« Reply #29 on: July 22, 2019, 05:49:37 pm »
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:
Code: Pascal  [Select][+][-]
  1. 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:-)

Of course I get that, and realize it is because I'm an old fuddy-duddy who did not cut his teeth on Python. I like code that can be parsed despite indentation style (or lack thereof :)). I think I even recall some (lame) code obfuscation techniques using removal of all extra whitespace. :D
« Last Edit: July 22, 2019, 05:58:45 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

 

TinyPortal © 2005-2018