1 \input texinfo @c -*-texinfo-*-
3 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
5 @c GNAT DOCUMENTATION o
7 @c G N A T C O D I N G S T Y L E o
10 @c Copyright (C) 1992-2001 Ada Core Technologies, Inc. o
12 @c GNAT is free software; you can redistribute it and/or modify it under o
13 @c terms of the GNU General Public License as published by the Free Soft- o
14 @c ware Foundation; either version 2, or (at your option) any later ver- o
15 @c sion. GNAT is distributed in the hope that it will be useful, but WITH- o
16 @c OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY o
17 @c or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License o
18 @c for more details. You should have received a copy of the GNU General o
19 @c Public License distributed with GNAT; see file COPYING. If not, write o
20 @c to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, o
21 @c MA 02111-1307, USA. o
23 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
25 @setfilename gnat-style.info
26 @settitle GNAT Coding Style
27 @dircategory Programming
29 * gnat-style: (gnat-style). GNAT Coding Style
32 @macro syntax{element}
38 @center GNAT Coding Style
40 @center A guide for GNAT developers
41 Copyright (C) 1992-2001 Ada Core Technologies, Inc.
46 @title GNAT Coding Style
47 @subtitle A guide for GNAT developers
48 @author Ada Core Technologies, Inc.
52 @node Top, General, , (dir)
53 @comment node-name, next, previous, up
58 * Declarations and Types::
59 * Expressions and Names::
66 @c -------------------------------------------------------------------------
67 @node General, Lexical Elements, Top, Top
69 @c -------------------------------------------------------------------------
72 Most of GNAT is written in Ada using a consistent style to ensure
73 readability of the code. This document has been written to help
74 maintain this consistent style, while having a large group of developers
78 For the coding style in the C parts of the compiler and run time,
79 see the GNU Coding Guidelines.
82 This document is structured after the Ada Reference manual.
83 Those familiar with that document should be able to quickly
84 lookup style rules for particular constructs.
86 @c -------------------------------------------------------------------------
87 @node Lexical Elements, Declarations and Types, General, Top
88 @section Lexical Elements
89 @c -------------------------------------------------------------------------
91 @subsection Character Set and Separators
92 @c -------------------------------------------------------------------------
96 The character set used should be plain 7-bit ASCII@.
97 The only separators allowed are space and the end-of-line sequence.
98 No other control character or format effector (such as HT, VT, FF)
100 The normal end-of-line sequence is used, which may be LF, CR/LF or CR,
101 depending on the host system. An optional SUB (16#1A#) may be present as the
102 last character in the file on hosts using that character as file terminator.
105 Files that are checked in or distributed should be in host format.
108 A line should never be longer than 79 characters, not counting the line
112 Lines must not have trailing blanks.
115 Indentation is 3 characters per level for if statements, loops, case
116 statements. For exact information on required spacing between lexical
117 elements, see file @file{style.adb}.
121 @subsection Identifiers
122 @c -------------------------------------------------------------------------
125 Identifiers will start with an upper case letter, and each letter following
126 an underscore will be upper case. Short acronyms may be all upper case.
127 All other letters are lower case.
128 An exception is for identifiers matching a foreign language. In particular,
129 we use all lower case where appropriate for C@.
132 Use underscores to separate words in an identifier.
134 @item Try to limit your use of abbreviations in identifiers.
135 It is ok to make a few abbreviations, explain what they mean, and then
136 use them frequently, but don't use lots of obscure abbreviations. An
137 example is the @code{ALI} word which stands for Ada Library
138 Information and is by convention always written in upper-case when
139 used in entity names.
142 procedure Find_ALI_Files;
146 Don't use the variable @samp{I}, use @samp{J} instead, @samp{I} is too
147 easily mixed up with @samp{1} in some fonts. Similarly don't use the
148 variable @samp{O}, which is too easily mixed up with @samp{0}.
151 @subsection Numeric Literals
152 @c -------------------------------------------------------------------------
155 Numeric literals should include underscores where helpful for
161 3.14159_26535_89793_23846
165 @subsection Reserved Words
166 @c -------------------------------------------------------------------------
169 Reserved words use all lower case.
176 The words @samp{Access}, @samp{Delta} and @samp{Digits} are
177 capitalized when used as @syntax{attribute_designator}.
181 @c -------------------------------------------------------------------------
185 Comment start with @samp{-- } (i.e.@: @samp{--} followed by two spaces).
186 The only exception to this rule (i.e.@: one space is tolerated) is when the
187 comment ends with @samp{ --}.
188 It also accepted to have only one space between @samp{--} and the start
189 of the comment when the comment is at the end of a line,
193 Every sentence in a comment should start with an upper-case letter (including
194 the first letter of the comment).
197 When declarations are commented with ``hanging'' comments, i.e.@:
198 comments after the declaration, there is no blank line before the
199 comment, and if it is absolutely necessary to have blank lines within
200 the comments these blank lines @emph{do} have a @samp{--} (unlike the
201 normal rule, which is to use entirely blank lines for separating
202 comment paragraphs). The comment start at same level of indentation
203 as code they are commenting.
207 -- @r{Integer value for storing value of} z
209 -- @r{The previous line was a blank line.}
213 Comments that are dubious or incomplete or comment on possibly
214 wrong or incomplete code should be preceded or followed by @samp{???}@.
217 Comments in a subprogram body must generally be surrounded by blank lines,
218 except after a @samp{begin}:
222 -- @r{Comment for the next statement}
226 -- @r{Comment for the B statement}
232 In sequences of statements, comments at the end of the lines should be
236 My_Identifier := 5; -- @r{First comment}
237 Other_Id := 6; -- @r{Second comment}
241 Short comments that fit on a single line are @emph{not} ended with a
242 period. Comments taking more than a line are punctuated in the normal
246 Comments should focus on why instead of what.
247 Descriptions of what subprograms do go with the specification.
250 Comments describing a subprogram spec should specifically mention the
251 formal argument names. General rule: write a comment that does not
252 depend on the names of things. The names are supplementary, not
253 sufficient, as comments.
256 Do NOT put two spaces after periods in comments.
259 @c -------------------------------------------------------------------------
260 @node Declarations and Types, Expressions and Names, Lexical Elements,Top
261 @section Declarations and Types
262 @c -------------------------------------------------------------------------
266 In entity declarations, colons must be surrounded by spaces. Colons
275 Declarations should be grouped in a logical order.
276 Related groups of declarations may be preceded by a header comment.
279 All local subprograms in a subprogram or package body should be declared
280 before the first local subprogram body.
283 Don't declare local entities that hide global entities.
286 Don't declare multiple variables in one declaration that spans lines.
287 Start a new declaration on each line, instead.
290 The @syntax{defining_identifier}s of global declarations serve as
291 comments of a sort. So don't choose terse names, but look for names
292 that give useful information instead.
295 Local names can be shorter, because they are used only within
296 one context, where comments explain their purpose.
301 @c -------------------------------------------------------------------------
302 @node Expressions and Names, Statements, Declarations and Types, Top
303 @section Expressions and Names
304 @c -------------------------------------------------------------------------
309 Every operator must be surrounded by spaces, except for the
310 exponentiation operator.
313 E := A * B**2 + 3 * (C - D);
317 When folding a long line, fold before an operator, not after.
320 Use parentheses where they make the intended order of evaluation clearer:
326 @c -------------------------------------------------------------------------
327 @node Statements, Subprograms, Expressions and Names, Top
329 @c -------------------------------------------------------------------------
331 @subsection Simple and Compound Statements
332 @c -------------------------------------------------------------------------
335 Use only one statement or label per line.
337 A longer @syntax{sequence_of_statements} may be divided in logical
338 groups or separated from surrounding code using a blank line.
341 @subsection If Statements
342 @c -------------------------------------------------------------------------
345 When the @samp{if}, @samp{elsif} or @samp{else} keywords fit on the
346 same line with the condition and the @samp{then} keyword, then the
347 statement is formatted as follows:
350 if @var{condition} then
352 elsif @var{condition} then
360 When the above layout is not possible, @samp{then} should be aligned
361 with @samp{if}, and conditions should preferably be split before an
362 @samp{and} or @samp{or} keyword a follows:
365 if @var{long_condition_that_has_to_be_split}
366 and then @var{continued_on_the_next_line}
373 The @samp{elsif}, @samp{else} and @samp{end if} always line up with
374 the @samp{if} keyword. The preferred location for splitting the line
375 is before @samp{and} or @samp{or}. The continuation of a condition is
376 indented with two spaces or as many as needed to make nesting clear.
377 As exception, if conditions are closely related either of the
378 following is allowed:
381 if x = lakdsjfhlkashfdlkflkdsalkhfsalkdhflkjdsahf
383 x = asldkjhalkdsjfhhfd
388 if x = lakdsjfhlkashfdlkflkdsalkhfsalkdhflkjdsahf or else
389 x = asldkjhalkdsjfhhfd or else
395 Conditions should use short-circuit forms (@samp{and then},
399 Complex conditions in if statements are indented two characters:
402 if @var{this_complex_condition}
403 and then @var{that_other_one}
404 and then @var{one_last_one}
410 Every @samp{if} block is preceded and followed by a blank line, except
411 where it begins or ends a @syntax{sequence_of_statements}.
424 @subsection Case Statements
428 Layout is as below. For long case statements, the extra indentation
429 can be saved by aligning the when clauses with the opening case.
432 case @var{expression} is
433 when @var{condition} =>
435 when @var{condition} =>
441 @subsection Loop Statements
445 When possible, have @samp{for} or @samp{while} on one line with the
446 condition and the @samp{loop} keyword.
449 for J in S'Range loop
455 If the condition is too long, split the condition (see ``If
456 statements'' above) and align @samp{loop} with the @samp{for} or
457 @samp{while} keyword.
460 while @var{long_condition_that_has_to_be_split}
461 and then @var{continued_on_the_next_line}
468 If the @syntax{loop_statement} has an identifier, it is laid out as follows:
471 Outer : while not @var{condition} loop
477 @subsection Block Statements
481 The @samp{declare} (optional), @samp{begin} and @samp{end} statements
482 are aligned, except when the @syntax{block_statement} is named. There
483 is a blank line before the @samp{begin} keyword:
496 @c -------------------------------------------------------------------------
497 @node Subprograms, Packages, Statements, Top
499 @c -------------------------------------------------------------------------
502 @subsection Subprogram Declarations
503 @c -------------------------------------------------------------------------
507 Do not write the @samp{in} for parameters, especially in functions:
510 function Length (S : String) return Integer;
514 When the declaration line for a procedure or a function is too long, fold it.
515 In this case, align the colons, and, for functions, the result type.
521 Pad : Character := Space)
526 The parameter list for a subprogram is preceded by a space:
529 procedure Func (A : Integer);
534 @subsection Subprogram Bodies
535 @c -------------------------------------------------------------------------
539 The functions and procedures should always be sorted alphabetically in
543 All subprograms have a header giving the function name, with the following
551 procedure My_Function is
555 Note that the name in the header is preceded by a single space,
556 not two spaces as for other comments.
559 Every subprogram body must have a preceding @syntax{subprogram_declaration}.
562 If there any declarations in a subprogram, the @samp{begin} keyword is
563 preceded by a blank line.
566 If the declarations in a subprogram contain at least one nested
567 subprogram body, then just before the of the enclosing subprogram
568 @samp{begin}, there is a line:
571 -- @r{Start of processing for @var{Enclosing_Subprogram}}
578 @c -------------------------------------------------------------------------
579 @node Packages, Program Structure, Subprograms, Top
580 @section Packages and Visibility Rules
581 @c -------------------------------------------------------------------------
586 All program units and subprograms have their name at the end:
595 We will use the style of @samp{use}-ing @samp{with}-ed packages, with
596 the context clauses looking like:
604 Names declared in the visible part of packages should be
605 unique, to prevent name clashes when the packages are @samp{use}d.
609 type Entity_Kind is ...;
615 After the file header comment, the context clause and unit specification
616 should be the first thing in a @syntax{program_unit}.
619 @c -------------------------------------------------------------------------
620 @node Program Structure,, Packages, Top
621 @section Program Structure and Compilation Issues
622 @c -------------------------------------------------------------------------
626 Every GNAT source file must be compiled with the @option{-gnatg}
627 switch to check the coding style (Note that you should look at
628 @file{style.adb} to see the lexical rules enforced by
632 Each source file should contain only one compilation unit.
635 Filenames should be 8 characters or less followed by the @samp{.adb}
636 extension for a body or @samp{.ads} for a spec.
639 Unit names should be distinct when krunched to 8 characters
640 (see @file{krunch.ads}) and the filenames should match the unit name,
641 except that they are all lower case.