Skip to content

Code style guidelines

Pavel I. Kryukov edited this page Jul 26, 2018 · 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;
}

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

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

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

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.

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