Skip to content

Coding style

Egor Mikhaylov edited this page Feb 6, 2024 · 2 revisions

Tabs vs Spaces

Indentation must be made with spaces, not tabs. If you program in Visual Studio then please configure it to insert 4 spaces as an indentation in Options/Text Editor/C++/Tabs.

Spaces inside brackets

A space must be inserted after each opening bracket and before each closing bracket:

Wrong:

foo(1,x,"z");

Right:

foo( 1, x, "z" );

Location of braces

Braces must be put on the new line, not at the end of the current line:

Wrong:

void foo() {
}

Right:

void foo() 
{
}

Code statements

Do not put many statements in one code line. The only exception could be inline function definition in a class, to make class definition more compact and easily observable.

Wrong:

ProgressBar::nextTask( "Converting object A" ); auto gridA = convert( *objA_->objectMesh );

Right:

ProgressBar::nextTask( "Converting object A" ); 
auto gridA = convert( *objA_->objectMesh );

Enum flags

Use the MR_MAKE_FLAG_OPERATORS macro:

#include "MRMesh/MRFlagOperators.h"

enum class MyFlags
{
    Foo = 1 << 0,
    Bar = 1 << 1,
};
MR_MAKE_FLAG_OPERATORS( MyFlags )

The macro generates all common operators for flags (&, |, ~, multiplication by bool).

Don't use a plain enum, prefer enum class for type safety.

Note that one operator we can't generate is the conversion to bool, so you must do if ( bool( x & y ) ).