Skip to content

Code style guidelines

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

If using pair or tuple is not possible, we agreed with Google Convention:

  • Input objects are passed by const-reference
  • Returned objects are passed by pointer

Motivation by an example:

void copy_object( const Object& source, Object* destination);

Object source;
Object destination;
copy_object( source, &destination); // Mutating destination is explicitly demonstrated

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