Difference between revisions of "Coding style"

From Lazarus wiki
Jump to navigationJump to search
Line 1: Line 1:
== Introduction ==
+
== FPC compiler ==
 +
 
 +
=== Introduction ===
 
Some people might think that the coding style used by the FPC compiler sources and base rtl is a little bit strange but it has been used for a lot of years and isn't subject to be discussed. So take the following as a standard to be used.  
 
Some people might think that the coding style used by the FPC compiler sources and base rtl is a little bit strange but it has been used for a lot of years and isn't subject to be discussed. So take the following as a standard to be used.  
  
== Keywords ==
+
=== Keywords ===
 
Write all keywords in ''lower 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.
 
Write all keywords in ''lower 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.
  
== Spaces ==
+
No "begin" on the same line as while..do/if..then/..., and indent such constructs like this
 +
if x then
 +
  begin
 +
    ..
 +
  end
 +
else if y then
 +
  begin
 +
    ...
 +
  end;
 +
 
 +
 
 +
=== Spaces ===
 
Don't use spaces around operators, colons, parentheses etc. e.g. write <tt>p:=p+i;</tt> instead of <tt>p := p + i ;</tt>.
 
Don't use spaces around operators, colons, parentheses etc. e.g. write <tt>p:=p+i;</tt> instead of <tt>p := p + i ;</tt>.
  
== TAB characters ==
+
=== TAB characters ===
 
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.
 
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.
 
This may result in a chaotic view of source files.
 
Align by space characters (also see ''Indentation'').
 
Align by space characters (also see ''Indentation'').
  
== Indentation ==
+
=== Indentation ===
 
Indentation size is always 2 space characters per level.
 
Indentation size is always 2 space characters per level.
  
== Newlines ==
+
=== Newlines ===
  
 
Newlines are set as it is done by most Object Pascal programs (what does this mean? advice: avoid passive voice).
 
Newlines are set as it is done by most Object Pascal programs (what does this mean? advice: avoid passive voice).
Line 22: Line 35:
 
that is, put ''two'' blank lines between them.
 
that is, put ''two'' blank lines between them.
  
== Misc ==
+
=== Misc ===
  
 
Please note that the <tt>else</tt> in consecutive <tt>if</tt>s is not indented:
 
Please note that the <tt>else</tt> in consecutive <tt>if</tt>s is not indented:
Line 30: Line 43:
 
  else if ... then
 
  else if ... then
  
== Examples ==
+
Split all composite if-conditions over multiple lines, so no "if (x) and (y) then" but
 +
if x and
 +
    y then
 +
  ..
 +
(except possibly if x and y are simply boolean variables)
 +
 
 +
 
 +
=== Examples ===
 
How it looks like can be easily checked by having a look at the [http://www.freepascal.org/cgi-bin/viewcvs.cgi/?root=fpc FPC sources].
 
How it looks like can be easily checked by having a look at the [http://www.freepascal.org/cgi-bin/viewcvs.cgi/?root=fpc FPC sources].
 +
 +
 +
== Lazarus ==
 +
 +
Since Lazarus and LCL follows delphi compatibility, the similar code style is used.
 +
 +
If you're making patch or an extension for LCL you should follow its code style.
 +
 +
If you're developing your own component, you're free to use anystyle you like, but it's suggested to follow LCL too.
 +
 +
 +
You can find Delphi coding style [http://edn.embarcadero.com/article/10280#8.2.3 here]

Revision as of 15:04, 25 November 2009

FPC compiler

Introduction

Some people might think that the coding style used by the FPC compiler sources and base rtl is a little bit strange but it has been used for a lot of years and isn't subject to be discussed. So take the following as a standard to be used.

Keywords

Write all keywords in lower 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.

No "begin" on the same line as while..do/if..then/..., and indent such constructs like this

if x then
  begin
    ..
  end
else if y then
  begin
    ...
  end;


Spaces

Don't use spaces around operators, colons, parentheses etc. e.g. write p:=p+i; instead of p := p + i ;.

TAB characters

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

Indentation

Indentation size is always 2 space characters per level.

Newlines

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.

Misc

Please note that the else in consecutive ifs is not indented:

if ... then
else if ... then
else if ... then

Split all composite if-conditions over multiple lines, so no "if (x) and (y) then" but

if x and
   y then
 ..

(except possibly if x and y are simply boolean variables)


Examples

How it looks like can be easily checked by having a look at the FPC sources.


Lazarus

Since Lazarus and LCL follows delphi compatibility, the similar code style is used.

If you're making patch or an extension for LCL you should follow its code style.

If you're developing your own component, you're free to use anystyle you like, but it's suggested to follow LCL too.


You can find Delphi coding style here