Coding style: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | ## FPC compiler and RTL | |
| 2 | 2 | ||
| 3 | 3 | ### Introduction | |
| 4 | 4 | ||
| 5 | - | ||
| 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 | 6 | ||
| 7 | 7 | ### Keywords | |
| 8 | 8 | ||
| 9 | - | ||
| 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 | 10 | ||
| 11 | 11 | ### Spaces | |
| 12 | 12 | ||
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 13 | + | Don't use spaces around operators, colons, parentheses etc. | |
| 14 | + | ``` | |
| 16 | 15 | { correct } | |
| 17 | - | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 16 | + | p:=p+i; | |
| 17 | + | ||
| 18 | + | { incorrect } | |
| 19 | + | p := p + i ; | |
| 21 | 20 | ``` | |
| 22 | 21 | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 22 | + | Spaces in parameter lists. TODO: ??? | |
| 23 | + | ``` | |
| 26 | 24 | function do_this(a: tdef; var b: tdef): boolean; | |
| 27 | 25 | ``` | |
| 28 | 26 | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - | ||
| 27 | + | Spaces in variable lists. TODO: ??? | |
| 28 | + | ``` | |
| 32 | 29 | var | |
| 33 | - | ||
| 30 | + | a,b,c : tprocdef; | |
| 34 | 31 | ``` | |
| 35 | 32 | ||
| 36 | 33 | ### TAB characters | |
| 37 | 34 | ||
| 38 | - | ||
| 35 | + | 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 | 36 | ||
| 40 | 37 | ### Indentation | |
| 41 | 38 | ||
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 39 | + | Indentation size is always 2 space characters per level. | |
| 45 | 40 | ||
| 46 | - | ||
| 41 | + | 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/...). | |
| 42 | + | ``` | |
| 47 | 43 | { correct } | |
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 51 | - | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 44 | + | if true then | |
| 45 | + | begin | |
| 46 | + | end; | |
| 47 | + | ||
| 48 | + | { incorrect } | |
| 49 | + | if true then begin | |
| 50 | + | end; | |
| 51 | + | ||
| 52 | + | { incorrect } | |
| 53 | + | if true then begin {...} end; | |
| 58 | 54 | ``` | |
| 59 | 55 | ||
| 60 | 56 | ### Newlines | |
| 61 | 57 | ||
| 62 | - | ||
| 58 | + | 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 | 59 | ||
| 64 | 60 | ### Comments | |
| 65 | 61 | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 62 | + | Comments should use curly brackets, be lower case and contain a single space between the {}. | |
| 63 | + | ``` | |
| 69 | 64 | { this is an example of a comment } | |
| 70 | 65 | ``` | |
| 71 | 66 | ||
| 72 | 67 | ### Routines | |
| 73 | 68 | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 69 | + | All sections (var, const, begin etc...) after routine header are indented one level. | |
| 70 | + | ``` | |
| 77 | 71 | procedure do_this; | |
| 78 | - | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 72 | + | var | |
| 73 | + | sym: tsym; | |
| 74 | + | begin | |
| 75 | + | end; | |
| 82 | 76 | ``` | |
| 83 | 77 | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 78 | + | No spaces after name and parenthesis for parameters. | |
| 79 | + | ``` | |
| 87 | 80 | procedure do_this(param: integer); | |
| 88 | - | ||
| 89 | - | ||
| 81 | + | begin | |
| 82 | + | end; | |
| 90 | 83 | ``` | |
| 91 | 84 | ||
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 85 | + | Nested functions are indented one level (the same level as other sections like var, const etc...) | |
| 86 | + | ``` | |
| 95 | 87 | procedure do_this; | |
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 99 | - | ||
| 100 | - | ||
| 101 | - | ||
| 102 | - | ||
| 103 | - | ||
| 104 | - | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 88 | + | var | |
| 89 | + | i: integer; | |
| 90 | + | ||
| 91 | + | { nested function } | |
| 92 | + | function getter: boolean; | |
| 93 | + | begin | |
| 94 | + | end; | |
| 95 | + | ||
| 96 | + | var | |
| 97 | + | x: integer; | |
| 98 | + | begin | |
| 99 | + | end; | |
| 108 | 100 | ``` | |
| 109 | 101 | ||
| 110 | - | ||
| 111 | - | ||
| 112 | - | ||
| 102 | + | Use result instead of function name for return values. | |
| 103 | + | ``` | |
| 113 | 104 | function getter: boolean; | |
| 114 | - | ||
| 115 | - | ||
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 105 | + | begin | |
| 106 | + | result:=true; | |
| 107 | + | // wrong! | |
| 108 | + | //getter:=true; | |
| 109 | + | end; | |
| 119 | 110 | ``` | |
| 120 | 111 | ||
| 121 | 112 | ### Classes/Records | |
| 122 | 113 | ||
| 123 | - | ||
| 124 | - | ||
| 125 | - | ||
| 114 | + | Class sections should appear on the same indention level as the class name. | |
| 115 | + | ``` | |
| 126 | 116 | tloadnode = class(tunarynode) | |
| 127 | - | ||
| 128 | - | ||
| 129 | - | ||
| 130 | - | ||
| 131 | - | ||
| 117 | + | protected | |
| 118 | + | fprocdef : tprocdef; | |
| 119 | + | public | |
| 120 | + | constructor create(v : tsym;st : TSymtable);virtual; | |
| 121 | + | end; | |
| 132 | 122 | ``` | |
| 133 | 123 | ||
| 134 | 124 | ### Misc | |
| 135 | 125 | ||
| 136 | - | ||
| 137 | - | ||
| 138 | - | ||
| 139 | - | ||
| 140 | - | ||
| 141 | - | ||
| 142 | - | ||
| 143 | - | ||
| 144 | - | ||
| 145 | - | ||
| 146 | - | ||
| 126 | + | Please note that the `else` in consecutive `if`s is not indented: | |
| 147 | 127 | ``` | |
| 148 | - | ||
| 149 | - | ||
| 128 | + | if x then | |
| 129 | + | begin | |
| 130 | + | .. | |
| 131 | + | end | |
| 132 | + | else if y then | |
| 133 | + | begin | |
| 134 | + | ... | |
| 135 | + | end; | |
| 136 | + | ``` | |
| 150 | 137 | ||
| 151 | - | ||
| 138 | + | Split all composite if-conditions over multiple lines, so no "if (x) and (y) then" but | |
| 139 | + | ``` | |
| 152 | 140 | if p1.nodetype=ordconstn and | |
| 153 | - | ||
| 154 | - | ||
| 155 | - | ||
| 156 | - | ||
| 141 | + | not is_boolean(p1.resultdef) and | |
| 142 | + | not is_enum(p1.resultdef) then | |
| 143 | + | begin | |
| 144 | + | ... | |
| 157 | 145 | ``` | |
| 158 | 146 | ||
| 159 | - | ||
| 160 | - | ||
| 161 | - | ||
| 147 | + | (except possibly if x and y are simply boolean variables) | |
| 162 | 148 | ||
| 163 | - | ||
| 149 | + | Extraneous parenthesis in if statements: TODO: ??? | |
| 150 | + | ``` | |
| 164 | 151 | if (pd.maxparacount>0) then | |
| 165 | - | ||
| 152 | + | ; | |
| 166 | 153 | ``` | |
| 167 | 154 | ||
| 168 | 155 | ### Examples | |
| 169 | 156 | ||
| 170 | - | ||
| 157 | + | 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 | 158 | ||
| 172 | 159 | ## FCL | |
| 173 | 160 | ||
| 174 | - | ||
| 175 | - | ||
| 176 | - | ||
| 161 | + | 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. | |
| 177 | 162 | ||
| 178 | - | ||
| 163 | + | So do not write | |
| 164 | + | ``` | |
| 179 | 165 | procedure do_this; | |
| 180 | - | ||
| 181 | - | ||
| 182 | - | ||
| 183 | - | ||
| 166 | + | var | |
| 167 | + | sym: tsym; | |
| 168 | + | begin | |
| 169 | + | end; | |
| 184 | 170 | ``` | |
| 185 | 171 | ||
| 186 | - | ||
| 187 | - | ||
| 188 | - | ||
| 172 | + | But write | |
| 173 | + | ``` | |
| 189 | 174 | procedure do_this; | |
| 190 | - | ||
| 191 | - | ||
| 192 | - | ||
| 193 | - | ||
| 194 | - | ||
| 195 | - | ||
| 175 | + | ||
| 176 | + | var | |
| 177 | + | sym: tsym; | |
| 178 | + | ||
| 179 | + | begin | |
| 180 | + | end; | |
| 196 | 181 | ``` | |
| 197 | 182 | ||
| 198 | 183 | ## Other packages distributed with FPC | |
| 199 | 184 | ||
| 200 | 185 | There are no formal standards. **To do: write me.** | |
| 201 | 186 | ||
| 202 | 187 | ## Lazarus | |
| 203 | 188 | ||
| 204 | - | ||
| 189 | + | Since Lazarus and LCL follow Delphi compatibility, a code style similar to the one used in Delphi is used. | |
| 205 | 190 | ||
| 206 | - | ||
| 191 | + | If you're writing a patch or an extension for LCL you should follow its code style. | |
| 207 | 192 | ||
| 208 | - | ||
| 193 | + | 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 | 194 | ||
| 210 | 195 | See the Lazarus coding guidelines: [[DesignGuidelines]] | |
| 211 | 196 | ||
| 212 | - | ||
| 197 | + | For reference, you can find the description of Delphi coding style [here](<https://docwiki.embarcadero.com/RADStudio/Athens/en/Delphi%E2%80%99s_Object_Pascal_Style_Guide>) | |
| 213 | 198 | ||
| 214 | 199 | ## See also | |
| 215 | 200 | ||
| 216 | - | ||
| 201 | + | * [[DesignGuidelines|Coding Guidelines for Lazarus]] | |
| 217 | 202 | ||
| 218 | 203 | --- | |
| 219 | - | ||
| 220 | - | ||
| 221 | - | ||
| 222 | - | ||
| 223 | - | ||
| 204 | + | *From the Free Pascal wiki, page "Coding style", as the Internet Archive kept it on 2025-12-24, last changed on 2025-09-13. 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).* |
