You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
4.8 KiB
4.8 KiB
Coding style
Every project should have some consistent coding style in which all contributors write. Bellow you can find our conventions on which we agreed on and which we try to keep.
C++
In C++ is written worker and queue manager. Generally its used underscore style with all small letters. Inspired by Google C++ style guide. If something is not defined than naming/formatting can be arbitrary, but should be similar to bellow-defined behaviour.
Naming convention
- For source codes use all lower case with underscores not dashes. Header files should end with
.h
and C++ files with.cpp
. - Typenames are all in lower case with underscores between words. This is applicable to classes, structs, typedefs, enums and type template parameters.
- Variable names can be divided on local variables and class members. Local variables are all lower case with underscores between words. Class members have in addition trailing underscore on the end (struct data members do not have underscore on the end).
- Constants are just like any other variables and do not have any specifics.
- All function names are again all lower case with underscores between words.
- Namespaces if there are ones they should have lower case and underscores.
- Macros are classical and should have all capitals and underscores.
- Comments can be two types documentational and ordinery ones in code. Documentation should start with
/**
and end with*/
, convention inside them is javadoc documentation format. Classical comments in code are one liners which starts with//
and end with the end of the line.
Formatting convention
- Line length is not explicitly defined, but should be reasonable.
- All files should use UTF-8 character set.
- For code indentation tabs (
\t
) are used. - Function declaration/definition: return type should be on the same line as the rest of the declaration, if line is too long, than particular parameters are placed on new line. Opening parenthesis of function should be placed on new line bellow declaration. Its possible to write small function which can be on only one line. Between parameter and comma should be one space.
int run(int id, string msg);
void print_hello_world()
{
std::cout << "Hello world" << std::endl;
return;
}
int get_five() { return 5; }
- Lambda expressions: same formatting as classical functions
auto hello = [](int x) { std::cout << "hello_" << x << std::endl; }
- Function calls: basically same as function header definition.
- Condition: after if, or else there always have to be one space in front of opening bracket and again one space after closing condition bracket (and in front of opening parenthesis). If and else always should be on separate lines. Inside condition there should not be any pointless spaces.
if (x == 5) {
std::cout << "Exactly five!" << std::endl;
} else if (x < 5 && y > 5) {
std::cout << "Whoa, that is weird format!" << std::endl;
} else {
std::cout << "I dont know what is this!" << std::endl;
}
- For and while cycles: basically same rules as for if condition.
- Try-catch blocks: again same rules as for if conditions. Closing parentheses of try block should be on the same line as catch block.
try {
int a = 5 / 0;
} catch (exception ex) {
std::cout << "Division by zero" << std::endl;
}
- Switch: again basics are the same as for if condition. Case statements should not be indented and case body should be intended with 1 tab.
switch (switched) {
case 0: // no tab indent
... // 1 tab indent
break;
case 1:
...
break;
default:
exit(1);
}
- Pointers and references: no spaces between period or arrow in accessing type member. No spaces after asterisk or ampersand. In declaration of pointer or reference format should be that asterisk or ampersand is adjacent to name of the variable not type.
number = *ptr;
ptr = &val;
number = ptr->number;
number = val_ref.number;
int *i;
int &j;
// bad format bellow
int* i;
int * i;
- Boolean expression: long boolean expression should be divided into more lines. The division point should always be after logical operators.
if (i > 10 &&
j < 10 &&
k > 20) {
std::cout << "Were here!" << std::endl;
}
- Return values should not be generally wrapped with parentheses, only if needed.
- Preprocessor directives start with
#
and always should start at the beginning of the line. - Classes: sections aka. public, protected, private should have same indentation as the class start itself. Opening parenthesis of class should be on the same line as class name.
class my_class {
public:
void class_function();
private:
int class_member_;
};
- Operators: around all binary operators there always should be spaces.
int x = 5;
x = x * 5 / 5;
x = x + 5 * (10 - 5);