Coding style

From Medusa: Coordinate Free Mehless Method implementation
Revision as of 17:15, 25 October 2016 by Anja (talk | contribs) (Created page with "=General= A soft $80$ characters line width limit. =Indentation= Indent using spaces. Indentation width is $4$ spaces. <syntaxhighlight lang="c++" line> for (int i = 0; i <...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

General

A soft $80$ characters line width limit.

Indentation

Indent using spaces. Indentation width is $4$ spaces.

1 for (int i = 0; i < 10; i++) {
2     cout << i << endl;
3 }

Naming convention

Constants - UPPER_CASE

classes - PascalCase

methods - camelCase

variables and stand-alone functions - underscore_separated

typedefs - lowercase underscore separated, usually one word with trailing _t, eg. vec_t.

namespaces - one lowercase word, maybe shortened, eg. op. For internal implementation details use op_internal. Namespace closing brace should contain comment // namespace your_name. There is no indentation within namespaces.

All standard abbreviations like Moving Least Squres (MLS) or Finite Elements Method (FEM) - UPPER_CASE

Floating point and integer literals use small suffixes, eg. 0.0f, -1e8l, 45ull .

1 #define MAX_VALUE 500
2 class MyClass {
3   public:
4     MyClass(first_var, second_var) {
5         ...
6     }
7     int getSize();
8 }

Comments

Comments are good. Use them to explain your code. Comments should have a space between the last slash and the start of text. Inline comments should have at least two spaces between end of code and start of the comment.

1 // This function will change the world
2 double change_the_world(bool skynet) {
3     if (skynet) {
4         return 0.0;  // Brace for the end of the world
5     }
6     ...
7 }

Use doxygen comments to generate documentation.

Headers

All headers must contain a header guard of form __FILENAME__.

Includes in header guards are separated in two groups, with intra-project includes on top and other includes on the bottom. The groups are separated by a blank line and includes are kept sorted within a group.

Misc

Avoid trailing whitespace. Curly opening brackets { should be inline with for loops, function definitions, class names, separated with a space. Outermost binary operators should have spaces around them.

1 int sumMe(int var) {  // yes
2     if (var == 1)
3     {                 // no
4         return 1;
5     }
6     return 0;
7 }

For null pointer we use nullptr instead of NULL macro.