4 Code formatting style discussion
Michele Calgaro edited this page 3 years ago

Proposal for a common TDE code style (from older discussions on etherpad)

NOTE: four spaces equal one hard tab in the examples shown below


  1. Every conditional expression consisting of more than one line of code must use a set of curly braces to enclose its contents

    Right:

    if (foo == bar)
    {
        a = 1;
    }
    

    Wrong:

    if (foo == bar)
        a = 1;
    

    One liners are not allowed and should be properly indented and braces added.
    Example of code not allowed:

    if (foo == bar) a = 1;
    
  2. Hard tabs will be used at all times to indent from the beginning of a line till the brace level. From there, spaces will be used.
    In case a line of code has to be splitted into multiple lines, fixed indentation of 8 spaces will be used for continuation (this may change in future)

  3. Indentation style is Allman.

    Example:

    if (foo)
    {
        a = 0;
    }
    else if (bar)
    {
        a = 1;
    }
    else
    {
        a = 2;
    }
    
  4. Switch statement styling
    Indented case lines, indented blocks, indented break, curly braces around blocks, break statement within braces.
    One line of whitespace must be inserted between each case block.

    Example:

    do_something();
    switch(foo)
    {
        case bar:
        {
            a = 1;
            break;
        }
    
        case baz: 
        {
            a = 2;
            ...long case block...
            c = 4;
            break;
        }
    
        case asd:
        {
            a = 3;
            break;
        }
    
        default:
        {
            a = 0;
        }
    }
    do_something_else();
    
  5. Within conditional expressions, no spaces are allowed between consecutive parenthesis or between parenthesis and their inner expressions. Spaces are required between parenthesis and logic operators, with exception of binary not (!).

    Right:

    if ((dirfi->fileName() != ".") && (dirfi->fileName() != ".."))
    if (!(a == 2))
    

    Wrong

    if ( (dirfi->fileName() != ".") && (dirfi->fileName() != "..") )
    if ( ( dirfi->fileName() != "." ) && ( dirfi->fileName() != ".." ) )
    if (( dirfi->fileName() != "." ) && ( dirfi->fileName() != ".." ))
    if ((dirfi->fileName() != ".")&&(dirfi->fileName() != ".."))
    
    if (! (a == 2))
    if ( !(a == 2))
    
  6. A single space must be placed between an operator and its operands.
    In loop control statements, a single space after each semicolon (if present) is required.

    Right:

    a = 1;
    a = a + 1;
    for (i = 0; i < 1; i = i + 2) 
    

    Wrong

    for (i=0;i<1;i=i+2)
    for (i=0; i<1; i=i+2)
    a=1;
    a=a+1
    for (i=0;i<1;i = i + 2)
    
  7. All function calls must use a single space after the comma of each of the call's arguments except the last.

    Right:

    foo(par1, par2, par3, par4)
    

    Wrong

    foo(par1,par2,par3,par4)
    
  8. All class member variables must be prefixed with "m_", a.k.a. Hungarian scope notation. See also next point for further info on function, class and member names.

    NOTE
    This is a nice to have but will require lot of manual rework. Initially this rule will not be enforced.

  9. Variable, function and class names: use convention of existing language.

    C:
    functions: void this_is_my_function()
    structs: struct this_is_my_struct

    C++:
    methods (use lowerCamelCase): void mThisIsMyMethod()
    classes (use UpperCamelCase): class ThisIsMyClass

    NOTE
    This is a nice to have but will require lot of manual rework. Initially these rules will not be enforced.

  10. All conditionals with space separated components (e.g. operand, operator, operand triplets) must be enclosed in parenthesis.

    Right:

    if ((a == 1) &&
        ((b == 2) || (c == 3)) ||
        !d ||
        ((e == 4) && !f))
    

    Wrong

    if (a == 1 &&
        (b == 2 || c == 3) ||
        !d ||
        (e == 4 && !f))
    
    if ((a == 1) &&
        (b == 2 || c == 3) ||
        !d ||
        (e == 4) && !f)
    
    if ((a == 1) &&
        (b == 2 || c == 3) ||
        (!d) ||
        (e == 4) && !f)
    
  11. Spaces are required between control statement, function name and opening parenthesis.

    A. A single space character should be used between a control statement and the opening parenthesis.

    Right:

    for (i = 0; i < 1; i = i + 2)
    if (a == 1)
    switch (a)
    while (a < 3)
    

    Wrong

    for(i = 0; i < 1; i = i + 2)
    if(a == 1)
    switch  (a)
    while    (a < 3)
    

    B. No space should be inserted between the name of a function and its opening parenthesis. Right:

    foo(a, b, c);
    void bar(int a, char c);
    

    Wrong

    foo (a, b, c);
    void bar   (int a, char c);
    
  12. Each line of code needs to be trimmed at the end, e.g. no separator characters are allowed between the last meaningful character of a line and end-of-line character

  13. public/protected/private/signals/slots/namespaces count as one level of indentation.

    Right:

    class MyClass
    {
        public:
            MyClass();
            ...
    };
    

    Wrong:

    class MyClass
    {
    public:
        MyClass();
        ...
    };
    
    class MyClass
    {
        public:
        MyClass();
        ...
    };
    
  14. If conditionals extend over more than one lane, the logical operator should be placed at the end of a lane rather than at the beginning of the next lane.

    Right:

    if ((a == 1) &&
        ((b == 2) || (c == 3)) ||
        !d ||
        ((e == 4) && !f))
    

    Wrong:

    if (a == 1
        && (b == 2 || c == 3)
        || !d
        || (e == 4 && !f))
    
  15. Two new line (0x0A) characters need to follow the last line of code in a file, resulting in an empty line displayed at the end of the file.

  16. Once the code of a package has been converted to the above guidelines, a git hook should be put in place and an auto linting/checking process executed prior to allow a commit to be pushed to TGW. A commit shall be rejected if the linting process fails and an error code shall be returned to the user.
    Template for linters needs to be provided to users/devs.
    A linting section on main TDE wiki (eventually pointing to this page) needs to be setup.