1. General information
If you want to propose a code to the project, please be sure that it compiles. Which mean it doesn't contain any errors or warnings. In the most cases, GNAT
and gnatpp
programs will help you fix most of the style problems. To format the code, in the main project directory, where steamsky.gpr file is type in terminal:
gnatpp -P steamsky.gpr
The code proposition has to pass AdaControl static analysis and all the project unit tests. The current test procedure is described in separated documentation.
2. Coding standard
These rules are enforced by GNAT itself and its syntax checking.
- Keep line length below 80 characters. It may look as an ancient requirement, but 80 characters is still an industry standard, and let's try to keep with standards here. You may bypass this limit only with comments lines, but please don't break then max 100 characters limit.
- Indentation in the code must be done with space and must have 3 characters length. This is the Ada programming language standard. Ada uses a lot of indentations, thus this value have to be small.
All attributes, keywords, or variables names must be written in that same casing like declared. Examples:
Good:
declare begin Put_Line(Item => "Hello World");
Bad:
Declare beGin put_line("hello world");
If using array attributes like
First
,Last
,Range
on multidimensional arrays, it is required to use index. For example, two-dimensional array:Good:
'First(1) 'Range(2)
Bad:
'First 'Range
No blanks characters (spaces) at the end of the code lines.
Each comment must starts with
--
and one space after. Examples:Good:
-- Proper comment
Bad:
-- Bad comment
All lines must end with Unix style
LF
notCR/LF
.End for subprogram and exists from loops must be labeled also. Examples:
Good:
procedure Main is begin null; end Main;
Bad:
procedure Main is begin null; end;
No vertical or horizontal tabs allowed in the source code.
If a subprogram has parameters, don't write
in
if they are only in default in mode. Usein
only forin out
parameters. Example:Good:
function Test(My_Param: String) return Boolean;
Bad:
function Test(My_Param: in String) return Boolean;
The layout of statements and declarations must be that same like in Ada Reference Manual. Example:
type Q is record A : Integer; B : Integer; end record; Block : declare A: Integer := 3; begin Proc (A, A); end Block;
All overriding subprograms must be marked with the word
overriding
. Example:Good:
overriding procedure My_Proc(My_Param: String);
Bad:
procedure My_Proc(My_Param: String);
No statements allowed in this same line after
then
orelse
. Example:Good:
if A = 1 then A := A + 1; end if;
Bad:
if A = 1 then A := A + 1 end if;
No unnecessary blank lines (last line in the file or more than one blank line one after another).
No unnecessary extra parenthesis allowed (C-style). Example:
Good:
if A = 3 and B > 2 then
Bad:
if (A = 3 and B > 2) then
These rules are enforced by AdaControl.
- All global variables in packages bodies should be accessed only via dedicated programs (getters and setters). No direct access to them from the other subprograms.
- All packages specifications and bodies must have a legal header (GPL exactly) at the top.
All variables must be initialized before use. Example:
Good:
My_Var: Positive := 1;
Bad:
My_Var: Positive;
All local variables must have a different name than any global variables (no hiding).
All elements in enumerations must be written in capital letters. Example:
Good:
type Week_Days is (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY);
Bad:
type Week_Days is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
All names of loops must end with
_Loop
. Example:Good:
My_Loop:
Bad:
My_Something:
All names of the blocks must end with
_Block
. Example:Good:
My_Code_Block:
Bad:
My_Code:
All other names (subprograms, variables, constant, types, etc.) must use capitalized snake case (standard for the Ada programming language). Example:
Good:
procedure My_Procedure;
Bad:
procedure Myprocedure;
All code blocks, loops and exists from loops must be named. Example:
Good:
My_For_Loop: for I in 1 .. 2 loop Put(I); end My_For_Loop;
Bad:
for I in 1 .. 2 loop Put(I); end;
Always use named parameters for subprograms, never positional. Example:
Good:
Length(Source => My_String);
Bad:
Length(My_String);
Always use numbers when initializing values for arrays. Example:
Good:
My_Array: constant array (1 .. 2) of Positive := (1 => 1, 2 => 2);
Bad:
My_Array: constant array (1 .. 2) of Positive := (1, 2);