Sign in Join
Wiki copytemporary

Coding style: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Coding style

OldNewLine
1+## FPC compiler and RTL
2+
3+### Introduction
4+
5+Some people might think that the coding style used in the FPC compiler and base RTL (run-time library) source code is a little bit strange. But it has been in use for a lot of years and is not subject to discussion. So take the following as a standard to be used.
6+
7+### Keywords
8+
9+Write all keywords in all lower *snake_case*. There is no need to make them unreadble by writing them in upper case. Modern IDEs support syntax highlighting, so keywords will be easily recognizable.
10+
11+### Spaces
12+
13+Don't use spaces around operators, colons, parentheses etc.
14+
15+``` pascal
16+{ correct }
17+p:=p+i;
18+
19+{ incorrect }
20+p := p + i ;
21+```
22+
23+Spaces in parameter lists. TODO: ???
24+
25+``` pascal
26+function do_this(a: tdef; var b: tdef): boolean;
27+```
28+
29+Spaces in variable lists. TODO: ???
30+
31+``` pascal
32+var
33+ a,b,c : tprocdef;
34+```
35+
36+### TAB characters
37+
38+Do not use TAB characters ([[ASCII]] HT, 0x09). There is no standard default TAB setting, so the look of source files using TAB characters will depend on client settings. This may result in a chaotic view of source files. Align by space characters (also see *Indentation*).
39+
40+### Indentation
41+
42+Indentation size is always 2 space characters per level.
43+
44+Never place a *begin* on the same line as while..do/if..then/..., but always on its own line (and indent it compared to the while/if/...).
45+
46+``` pascal
47+{ correct }
48+if true then
49+ begin
50+ end;
51+
52+{ incorrect }
53+if true then begin
54+ end;
55+
56+{ incorrect }
57+if true then begin {...} end;
58+```
59+
60+### Newlines
61+
62+Newlines are set as it is done by most Object Pascal programs (what does this mean? advice: avoid passive voice). Separate subroutines by three newlines, that is, put *two* blank lines between them.
63+
64+### Comments
65+
66+Comments should use curly brackets, be lower case and contain a single space between the {}.
67+
68+``` pascal
69+{ this is an example of a comment }
70+```
71+
72+### Routines
73+
74+All sections (var, const, begin etc...) after routine header are indented one level.
75+
76+``` pascal
77+procedure do_this;
78+ var
79+ sym: tsym;
80+ begin
81+ end;
82+```
83+
84+No spaces after name and parenthesis for parameters.
85+
86+``` pascal
87+procedure do_this(param: integer);
88+ begin
89+ end;
90+```
91+
92+Nested functions are indented one level (the same level as other sections like var, const etc...)
93+
94+``` pascal
95+procedure do_this;
96+ var
97+ i: integer;
98+
99+ { nested function }
100+ function getter: boolean;
101+ begin
102+ end;
103+
104+ var
105+ x: integer;
106+ begin
107+ end;
108+```
109+
110+Use result instead of function name for return values.
111+
112+``` pascal
113+function getter: boolean;
114+ begin
115+ result:=true;
116+ // wrong!
117+ //getter:=true;
118+ end;
119+```
120+
121+### Classes/Records
122+
123+Class sections should appear on the same indention level as the class name.
124+
125+``` pascal
126+tloadnode = class(tunarynode)
127+protected
128+ fprocdef : tprocdef;
129+public
130+ constructor create(v : tsym;st : TSymtable);virtual;
131+end;
132+```
133+
134+### Misc
135+
136+Please note that the `else` in consecutive `if`s is not indented:
137+
138+``` pascal
139+ if x then
140+ begin
141+ ..
142+ end
143+ else if y then
144+ begin
145+ ...
146+ end;
147+```
148+
149+Split all composite if-conditions over multiple lines, so no "if (x) and (y) then" but
150+
151+``` pascal
152+if p1.nodetype=ordconstn and
153+ not is_boolean(p1.resultdef) and
154+ not is_enum(p1.resultdef) then
155+ begin
156+ ...
157+```
158+
159+(except possibly if x and y are simply boolean variables)
160+
161+Extraneous parenthesis in if statements: TODO: ???
162+
163+``` pascal
164+if (pd.maxparacount>0) then
165+ ;
166+```
167+
168+### Examples
169+
170+How it looks like can be easily checked by having a look at the [FPC sources](http://www.freepascal.org/cgi-bin/viewcvs.cgi/?root=fpc).
171+
172+## FCL
173+
174+The FCL follows the same rules as above, with 1 exception: routines are not indented, and there are empty lines between the \`\`var\`\` section and surrounding code.
175+
176+So do not write
177+
178+``` pascal
179+procedure do_this;
180+ var
181+ sym: tsym;
182+ begin
183+ end;
184+```
185+
186+But write
187+
188+``` pascal
189+procedure do_this;
190+
191+var
192+ sym: tsym;
193+
194+begin
195+end;
196+```
197+
198+## Other packages distributed with FPC
199+
200+There are no formal standards. **To do: write me.**
201+
202+## Lazarus
203+
204+Since Lazarus and LCL follow Delphi compatibility, a code style similar to the one used in Delphi is used.
205+
206+If you're writing a patch or an extension for LCL you should follow its code style.
207+
208+If you're developing your own component, you're free to use any style you like, but it's suggested to use the LCL style, too.
209+
210+See the Lazarus coding guidelines: [[DesignGuidelines]]
211+
212+For reference, you can find the description of Delphi coding style [here](http://edn.embarcadero.com/article/10280#8.2.3)
213+
214+## See also
215+
216+- [[DesignGuidelines|Coding Guidelines for Lazarus]]
217+
218+---
219+*From the Free Pascal wiki, page "Coding style", as it stood on 2022-01-07 after a change by Michael, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
220+Category: Coding style
221+Category: FPC
222+Category: FPC development
223+Category: Lazarus

Back to history