Recent

Author Topic: Formatting in FreePascal  (Read 12080 times)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Formatting in FreePascal
« Reply #30 on: July 23, 2019, 09:17:09 am »
By the way, when not working on the compiler, this is how I format my code:
Code: Pascal  [Select][+][-]
  1. with TBarSeries(Result) do begin
  2.   <snip>
  3.   if APlotType = ptBars then begin
  4.     AxisIndexX := AChart.LeftAxis.Index;
  5.     AxisIndexY := AChart.BottomAxis.Index;
  6.   end else begin
  7.     AxisIndexX := AChart.BottomAxis.Index;
  8.     AxisIndexY := AChart.LeftAxis.Index;
  9.   end;
  10. end;
The problem with that formatting is that it shows an "if" terminated with two "end"(s).  If statements are not terminated with "end"(s).
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.

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.

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Formatting in FreePascal
« Reply #31 on: July 23, 2019, 11:48:06 am »
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 ?

Programmer's don't read the minds of other programmers, they read code.  It's "nice" when the formatting matches the language's constructs, which in this case is, "end" terminates a "begin", not an if or anything else for that matter (correction: an "end" terminates a "case" too.)

That answers your "so ?"
« Last Edit: July 24, 2019, 01:56:18 am 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.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Formatting in FreePascal
« Reply #32 on: July 23, 2019, 12:13:42 pm »
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?

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: Formatting in FreePascal
« Reply #33 on: July 23, 2019, 02:14:09 pm »
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.

As you write, compiling  -O0 does stop there and I just tried  -O0 -OoREGVAR  that also stops there and produces pretty tight code.  From now on,  my debug settings will be -O0 -OoREGVAR.

Bug or not bug, I do not know.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: Formatting in FreePascal
« Reply #34 on: July 23, 2019, 02:15:36 pm »
Delphi also has such issues when optimization are on. They are less pronounced, but they are there.

It can't be avoided I guess.

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: Formatting in FreePascal
« Reply #35 on: July 23, 2019, 02:27:14 pm »
Delphi also has such issues when optimization are on. They are less pronounced, but they are there.

It can't be avoided I guess.
One would have to ask to the king of the debugger.
May be there is not enough code bytes to patch the debug hook when in -O1 and above.

kupferstecher

  • Hero Member
  • *****
  • Posts: 583
Re: Formatting in FreePascal
« Reply #36 on: July 23, 2019, 08:14:01 pm »
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.

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...

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Formatting in FreePascal
« Reply #37 on: July 23, 2019, 09:36:25 pm »
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...

It's a configurable feature: Tools->Options->Environment->Naming
See attached image.
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.

kupferstecher

  • Hero Member
  • *****
  • Posts: 583
Re: Formatting in FreePascal
« Reply #38 on: July 23, 2019, 10:17:04 pm »
@lucamar: Thats great! One annoying thing less~
Thanks!

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: Formatting in FreePascal
« Reply #39 on: July 23, 2019, 10:19:48 pm »
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.

What would happen without it? Suppose you are on Linux. You create a new form and save the unit as "uForm.pas". The form is used in the mainform; you add the new unit to its uses clause, but write the unit name as "UForm". What will happen? The unit will not be found because a file "UForm.pas" does not exist, the existing file is spelled as "uForm.pas"

With this feature, the unit was automatically saved as lower-case filename, i.e. "uform.pas". When the IDE notices that the file "UForm.pas" does not exist, it automatically searches for the lowercase filename, "uform.pas" - which is found, of course.

Once you fell into this trap, you will never call this feature "annoying" again.

kupferstecher

  • Hero Member
  • *****
  • Posts: 583
Re: Formatting in FreePascal
« Reply #40 on: July 23, 2019, 10:54:29 pm »
Hello wp,
I heard before, that its because of the case sensitive operating systems, but couldn't understand, as upper-case-files worked for me on Linux, before. So this is clear now. Still I don't feel very comfortable about beeing limited in the naming just because of that. I think I will change the setting, use upper cases and - earlier or later - fall into the trap~

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Formatting in FreePascal
« Reply #41 on: July 24, 2019, 01:43:37 am »
I think I will change the setting, use upper cases and - earlier or later - fall into the trap~

THe problem, really is with mixed-case files like your uFancyForm. The compiler/tools work really hard trying to find some file whose name "sounds" as if it might be "it" (IIRC, they look for an exact match, all lowercase, all upercase, initial upper- and rest lower-case, and I don't know what else--it's explained somewhere in the manuals or the wiki); I suspect that's from where all those "case-insensitive" file searching routines in the RTL come.

Even so there may arise the situation where there are two (or more) files with the same name differing only in case, say: "UFancyForm.pas" and "uFancyForm.pas" (and maybe a remnant "ufancyform.pas"). To avoid that, the easiest solution is to stabllish the policy that file names must be all lower (or upper) case and the IDE helps you enforce it ... but only if you want it to!

That's basically all there is to it. :)
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.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Formatting in FreePascal
« Reply #42 on: July 24, 2019, 02:34:21 am »
Oop! Forgot about this:

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.

I'm not sure (didn't test) but maybe these settings (see attached image) may help you with this issue. HTH!
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.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Formatting in FreePascal
« Reply #43 on: July 24, 2019, 04:14:21 am »
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".

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Formatting in FreePascal
« Reply #44 on: July 24, 2019, 09:34:03 am »
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 ?
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.

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:

Code: Pascal  [Select][+][-]
  1. with TBarSeries(Result) do begin
  2.   <snip>
  3.   if APlotType = ptBars then begin
  4.     AxisIndexX := AChart.LeftAxis.Index;
  5.     AxisIndexY := AChart.BottomAxis.Index;
  6.   end else if APlotType = ptWhatEver then begin
  7.     // whatever
  8.   end else begin
  9.     AxisIndexX := AChart.BottomAxis.Index;
  10.     AxisIndexY := AChart.LeftAxis.Index;
  11.   end;
  12. end;
  13.  

I've used this style for years and as long as I work on my own projects (no matter if they are private ones or code I publish as open source) I'll use this style and nothing can make me change my mind there.

 

TinyPortal © 2005-2018