Skip to content
Igor Smirnov edited this page Sep 30, 2015 · 2 revisions

Introduction

Proper usage of language syntax increases clarity in the code. It aids reading, makes developments easier and just looks better. Good formating exploits program structure by means of adjusting visual appearance of the code.

Line length

Lines of code should be not longer than 100 symbols INCLUDING comments. In addition it is highly recommended to avoid lines longer than 80 symbols. If it cannot be done - consider breaking the line and starting a new one.

Line breaks

Line breaks should try to maintain expression structure.

Example:

a = b + ( VeryLongRoutineName( d + VeryVeryLongNameForSomeFunc()) 
          * ( SomeOtherFunction() + AnotherFunc( my_var, his_var) 
              - HowDoYouLikeIt() * ThatReallyComplex( real, imaginary)
              - YouBetterFinishYourExpressionHereUseTemporaryVars()));
                                               

Indentation

Indentation unit is 4 spaces. Usage of tabs is bad style and should be avoided. Usage of tab key however can be recomended provided that a text editor is configured to substitute tabs by 4 spaces automatically. Example:

if ( cond)
{
    for ( int i = 0; i < 10; i++)
    {
        some_var+= some_array[ i];  
    }
}

Spaces

Usage of spaces should keep structure of expressions and statements clear. Space should be inserted after:

  • Opening brace '(' if it's not followed by another brace '('.
    Example: a = 1 + ( ( a + 1) + ( b + 2));
  • Semicolon ';'
  • Comma ','
  • Opening bracket '['
  • Binary operators and ternary operator '? : '
  • Postfix unary operators

Generally, spaces should separate expressions, operators, statements, function calls. Example:

a = atoi( ( char*)( my_ptr + ( offset * sizeof( myStruct))));

However space should NOT be used in:

  • Array element, or field selection like my_struct.field; my_ptr->field; my_arr[ 10];
  • Before semicolon ';' or comma ',' like for ( a = 0, b = 0; a == b; a++, b++);

Braces

Each curly brace is placed on a new line.

Example:

/* Wrong */
if ( condition){
    return true;
}
/* Correct */
if ( condition)
{
    return true;
}

Huge blocks should have commented curly braces where possible.

Example:

if ( condition == true) /** Common case */
{
    if ( a || ( b && c))
    {
        for ( ...)
        {
            ...
        }
        if ( ...)
        {
            for ( ...)
            { 
                ...
            }
        }
    }
} /** End of common case */    
Clone this wiki locally