Recent

Author Topic: FPC and Lazarus coding style  (Read 1758 times)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12098
  • Debugger - SynEdit - and more
    • wiki
Re: FPC and Lazarus coding style
« Reply #15 on: February 03, 2026, 03:56:19 pm »

Zvoni

  • Hero Member
  • *****
  • Posts: 3246
Re: FPC and Lazarus coding style
« Reply #16 on: February 03, 2026, 04:29:41 pm »
The indent (e.g. the one "else") is indeed unfortunate. If that was code I would work on (which it isn't) then I would consider fixing it, if and likely only if, I had other functional changes in the same code block to make.
Which "else" would that be?
the only "fishy" formatting i see is Line 18 to Line 21, which indents to the same level of the "Begin" in Line 17 resp. the "end" in Line 22

Line 23.

The else has no indent at all. But all other code in the procedure's begin/end is indented.

About line 18 to 21: That is a common style. The "begin (and it's end) are themself indented. The code they enclose is not further indented, as the begin/end already is. I.e., the begin/end is part of the indented code.

This is logical if you have "if then" followed by an indented statement on the next line(s). The begin/end is one compound statement. It gets indented for the "if".
Would have to disagree.
The „else“ in line 23 is indented to the „if“ in line 16, which is correct, considering the begin/end in line 17/22 is, strictly speaking, not necessary
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dsiders

  • Hero Member
  • *****
  • Posts: 1522
Re: FPC and Lazarus coding style
« Reply #17 on: February 03, 2026, 05:24:51 pm »
I can not even follow the parenthesis.
Off topic, but the Lazarus 5.0 will have rainbow brackets.

Which I hope will not be enabled by default.

Okoba

  • Hero Member
  • *****
  • Posts: 648
Re: FPC and Lazarus coding style
« Reply #18 on: February 03, 2026, 05:26:12 pm »
Should have posted the link here: https://forum.lazarus.freepascal.org/index.php/topic,73393.0.html
Oh I was sure I asked the question already! I thought maybe I was sleep or loosing my mind  :D

PascalDragon

  • Hero Member
  • *****
  • Posts: 6315
  • Compiler Developer
Re: FPC and Lazarus coding style
« Reply #19 on: February 03, 2026, 10:57:40 pm »
Normally I do not look at the code of FPC or Lazarus, but today I was looking at some of the units, and it was hard for me to read the codes, especially the FPC. The code seems disturbing. It seems no spacing, format, or even case of keywords is written with a rule.

I wanted to ask how this is managed at the level of such big projects. Do professional developers not care about this stuff, or are these codes old and people have no time to clean them up or something I, as a normal developer, cannot understand?

The compiler itself does have a more or less fixed coding style (if unorthodox) that is tried to be adhered to. Especially with newly added code.

The RTL is much less strict not to mention the packages. They usually depend on whoever adds the code though the general idea is to at least adhere to the style of the surrounding code.

And there is indeed no intentation to fix this cause as Martin_fr mentioned this adds unnecessary noise to the commit log and that is more imporant than consistent style.

Warfley

  • Hero Member
  • *****
  • Posts: 2038
Re: FPC and Lazarus coding style
« Reply #20 on: February 06, 2026, 07:47:29 pm »
Normally I do not look at the code of FPC or Lazarus, but today I was looking at some of the units, and it was hard for me to read the codes, especially the FPC. The code seems disturbing. It seems no spacing, format, or even case of keywords is written with a rule.

I wanted to ask how this is managed at the level of such big projects. Do professional developers not care about this stuff, or are these codes old and people have no time to clean them up or something I, as a normal developer, cannot understand?
I've worked with multiple big projects and there is only one unbreakable rule about formatting: You never change a line just for formatting.
If you look at a merge request you want to see what they did, so you look at what they changed. If they formatted 1000 lines and added 2, good luck finding that in the diff. Additionally if two people work on the same file and format it, they will get merge conflicts for the 1000 lines.
Or most importantly, if you want to see why something was changed, you look into the history, if it is full of re-formats it's difficult to find the change that actually caused the behavior

It will happen over time that different code styles get into the code base. FPC is a 30 year old code base that went through many different developers. You have snake casing vs camel casing vs no casing in there. You have descriptive variable names, abbreviations and single char names, you have german and english identifiers. All mixed. Yeah it's a mess, but you cannot clean it up.

Also, readability does not really depend on such superficial metrics. When I first looked into the FPC code base I thought the same thing as you, but once you get used to it, which takes around 30 minutes, it's not an issue anymore. FPC is very well structured semantically and once you get over the superficial inconsistencies it is really easy to read, understand and modify the code. Something I cannot say about most (enterprise) Java or C++ code bases I have worked in
« Last Edit: February 06, 2026, 07:49:53 pm by Warfley »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12098
  • Debugger - SynEdit - and more
    • wiki
Re: FPC and Lazarus coding style
« Reply #21 on: February 06, 2026, 08:07:08 pm »
Semi off topic: As in not about the specific coding style, but how to deal with style one isn't used to.

The IDE has an IMHO excellent feature to help reading unfamiliar code, especially if style means that you find it hard to track identifiers, groups of them, .... => https://wiki.lazarus.freepascal.org/New_IDE_features_since#Multiple_user_defined_word_highlight/markup => Just (with the press of one key) color any identifier of interest. You can use as many colors as you need.

Wallaby

  • Full Member
  • ***
  • Posts: 130
Re: FPC and Lazarus coding style
« Reply #22 on: February 06, 2026, 11:10:19 pm »
The IDE has an IMHO excellent feature to help reading unfamiliar code... You can use as many colors as you need.

On a related note, there is a great feature called Outline that connects block boundaries with a line and more importantly - colour.

Makes it super easy to see where each block begins and ends.

Wondering why it's not enabled by default...?

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12098
  • Debugger - SynEdit - and more
    • wiki

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 707
Re: FPC and Lazarus coding style
« Reply #24 on: February 07, 2026, 03:01:47 am »
The IDE has an IMHO excellent feature to help reading unfamiliar code... You can use as many colors as you need.

On a related note, there is a great feature called Outline that connects block boundaries with a line and more importantly - colour.

Makes it super easy to see where each block begins and ends.

Wondering why it's not enabled by default...?

Would like to try it.  How do you turn it on?

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 707
Re: FPC and Lazarus coding style
« Reply #25 on: February 07, 2026, 03:08:22 am »
I've worked with multiple big projects and there is only one unbreakable rule about formatting: You never change a line just for formatting.

I can see that for team projects.   But even in that context, reformatting of a copy of a source can be a big aid to understanding.  Having a style standard is important in that context too (although, full disclosure, it's been decades since I work on a team project).

Wallaby

  • Full Member
  • ***
  • Posts: 130
Re: FPC and Lazarus coding style
« Reply #26 on: February 07, 2026, 04:39:45 am »
Would like to try it.  How do you turn it on?

Here

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12098
  • Debugger - SynEdit - and more
    • wiki
Re: FPC and Lazarus coding style
« Reply #27 on: February 07, 2026, 09:21:46 am »
Would like to try it.  How do you turn it on?
Here

And look at the list, and decide which ones you want on. E.g. if you have "if" then every "if" even with just one single line after the "then", and no "begin" will have an outline.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 707
Re: FPC and Lazarus coding style
« Reply #28 on: February 08, 2026, 04:10:03 pm »
Thank you.  Looks very helpful.

 

TinyPortal © 2005-2018