Login
Coding Standard
Login
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.

  1. 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.
  2. 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.
  3. 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");
    
  4. 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
    
  5. No blanks characters (spaces) at the end of the code lines.

  6. Each comment must starts with -- and one space after. Examples:

    Good:

    -- Proper comment
    

    Bad:

    --      Bad comment
    
  7. All lines must end with Unix style LF not CR/LF.

  8. 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;
    
  9. No vertical or horizontal tabs allowed in the source code.

  10. If a subprogram has parameters, don't write in if they are only in default in mode. Use in only for in out parameters. Example:

    Good:

    function Test(My_Param: String) return Boolean;
    

    Bad:

    function Test(My_Param: in String) return Boolean;
    
  11. 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;
    
  12. 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);
    
  13. No statements allowed in this same line after then or else. Example:

    Good:

    if A = 1 then
       A := A + 1;
    end if;
    

    Bad:

    if A = 1 then A := A + 1 end if;
    
  14. No unnecessary blank lines (last line in the file or more than one blank line one after another).

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

  1. 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.
  2. All packages specifications and bodies must have a legal header (GPL exactly) at the top.
  3. All variables must be initialized before use. Example:

    Good:

    My_Var: Positive := 1;
    

    Bad:

    My_Var: Positive;
    
  4. All local variables must have a different name than any global variables (no hiding).

  5. 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);
    
  6. All names of loops must end with _Loop. Example:

    Good:

    My_Loop:
    

    Bad:

    My_Something:
    
  7. All names of the blocks must end with _Block. Example:

    Good:

    My_Code_Block:
    

    Bad:

    My_Code:
    
  8. 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;
    
  9. 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;
    
  10. Always use named parameters for subprograms, never positional. Example:

    Good:

    Length(Source => My_String);
    

    Bad:

    Length(My_String);
    
  11. 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);