Skip to content

Code style guidelines

Pavel I. Kryukov edited this page Mar 1, 2020 · 7 revisions

A common code style is very important aspect of a team code development. Our project uses the code style of MIPT-VIS project with some complements.

Main conventions

It is available on this external page. Please, read it carefully and follow these rules when you commit any code into the project repository.

Additional conventions

Note: All statements described it this paragraph have higher priority than rules of MIPT-VIS!

Pythonesque statements

We suggest to remove as much curly brackets as you can:

/* Prefer */
if ( condition)
    statement;

for ( const auto& e : array)
     std::cout << e;

if ( condition)
    statement;
else
    another_statement;

/* Less preferrable */
if ( condition)
{
    statement;
}

for ( const auto& e : array)
{
     std::cout << e;
}

Try/catch blocks

Try-block should be the function body whenever possible:

auto avoid() {
    try {
       something();
    }
    catch (...) {

    }
    return something_else();
}

```c++
auto foo() try {

}
catch (...) {

}

auto prefer() {
    foo();
    return something_else();
}

Exception to that rule

However, one-liners can't be mixed with multi-lines:

/* Avoid */
if ( condition)
    statement;
else
{
    statement1;
    statement2;
}

/* Prefer */
if ( condition)
{
    statement;
} else
{
    statement1;
    statement2;
}

#include's

Order

We stick to LLVM guidelines:

We prefer these #includes to be listed in this order:

  • Main Module Header
  • Local/Private Headers
  • Project/Subproject headers (infra/..., modules/..., ...)
  • System #includes

and each category should be sorted lexicographically by the full path.

Relative

We do not use relative includes containing ".." idiom due to code coverage issues.

// bad
#include "../../rf.h"

// good
#include <func_sim/rf/rf.h>

Ternary operator

Ternary operator must appear in two ways, depending on statement length:

auto variable = condition ? statement1 : statement2;
auto variable = condition
    ? extremely_long_statement_one
    : extremely_long_statement_two;

Nested ternary operators are not allowed.

Redundant else

We do not use else after return and throw.

// Avoid
if ( condition)
    return value;
else if ( other_condition)
    throw std::exception;
else
    do_work();

// Prefer
if ( condition)
    return value;

if ( other_condition)
    throw std::exception;

do_work();

Indentation

Every indentation blank spaces is multiplier of 4 spaces. We use only spaces for indentation. Tabs are not allowed!

Example:

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

We recommend you to configure your text editor for printing 4 spaces with Tab key.

  • In Notepad++, it can be set from menu:
Settings\Preferences\Language Menu/Tab Settings\Tab size>: 4, Replace by space — ON
  • In Vim, open .vimrc file and add following lines:
set tabstop=4
set shiftwidth=4
set expandtab
  • In Visual Studio 2010 from menu:
Tools\Options\Text Editor\C/C++\Tabs\Tab size: 4, Indent size 4:, Insert spaces — ON

You can always use find and replace feature of editors or regular expression: %s/\t/ /g

Clone this wiki locally