Difference between revisions of "Coding style"

From Medusa: Coordinate Free Mehless Method implementation
Jump to: navigation, search
m (Using your IDE auto formatter)
 
(16 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This is a brief description of our coding style, roughtly following the [https://google.github.io/styleguide/cppguide.html Google C++ Style Guide]. You can also download our code style settings to import in CLion [[:File:mm.xml here]]
+
This is a brief description of our coding style, roughtly following the [https://google.github.io/styleguide/cppguide.html Google C++ Style Guide].  
 +
 
 +
You can also download the CLion settings: [[File:Settings.jar|thumb]], which you can import in <code>File/Import settings</code>.
 +
 
 +
You can also download only our code style settings to import in CLion: [[:File:mm.xml]]. It can be imported under <code>Settings/Editor/Code Style/Scheme/Import scheme</code>.
  
 
=General=
 
=General=
A soft $80$ characters line width limit.
+
We use a 100 characters line width limit in C++ code.
  
 
=Indentation=
 
=Indentation=
  
Indent using spaces. Indentation width is $4$ spaces.
+
Indent using spaces. Indentation width is 4 spaces.
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
 
for (int i = 0; i < 10; i++) {
 
for (int i = 0; i < 10; i++) {
Line 27: Line 31:
 
namespaces - one lowercase word, maybe shortened, eg. <syntaxhighlight lang="c++" inline> op </syntaxhighlight>. For internal implementation details use <syntaxhighlight lang="c++" inline> op_internal </syntaxhighlight>. Namespace closing brace should contain comment  `// namespace your_name`. There is no indentation within namespaces.
 
namespaces - one lowercase word, maybe shortened, eg. <syntaxhighlight lang="c++" inline> op </syntaxhighlight>. For internal implementation details use <syntaxhighlight lang="c++" inline> op_internal </syntaxhighlight>. Namespace closing brace should contain comment  `// namespace your_name`. There is no indentation within namespaces.
  
All standard abbreviations like [[Moving Least Squares (MLS)| Moving Least Squres (MLS)]] or [https://en.wikipedia.org/wiki/Finite_element_method Finite Elements Method (FEM)] -
+
All standard abbreviations like [[Weighted Least Squares (WLS)]] or [https://en.wikipedia.org/wiki/Finite_element_method Finite Elements Method (FEM)] -
 
<syntaxhighlight lang="c++" inline>UPPER_CASE</syntaxhighlight>
 
<syntaxhighlight lang="c++" inline>UPPER_CASE</syntaxhighlight>
  
Line 48: Line 52:
 
Comments are good. Use them to explain your code. Comments should have a space between the last
 
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
 
slash and the start of text.  Inline comments should have at least two spaces between end of code
and start of the comment.
+
and start of the comment. Comments before functions are used for documentation. Always use full
 +
sentences and end them with a punctuation mark.
  
 
<syntaxhighlight lang="c++">
 
<syntaxhighlight lang="c++">
// This function will change the world
+
/**
 +
* This function changes the world.
 +
* @param skynet If `true`, the world ends, otherwise nothing happens.
 +
*/
 
double change_the_world(bool skynet) {
 
double change_the_world(bool skynet) {
 
     if (skynet) {
 
     if (skynet) {
         return 0.0;  // Brace for the end of the world
+
         return 0.0;  // Brace for the end of the world.
 
     }
 
     }
 
     ...
 
     ...
Line 60: Line 68:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Use doxygen comments to generate [[documentation]].
+
Use doxygen comments to generate [http://e6.ijs.si/medusa/docs/html/ documentation].
  
 
=Headers =
 
=Headers =
Line 85: Line 93:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
For null pointer we use  <syntaxhighlight lang="c++" inline> nullptr </syntaxhighlight> instead of  <syntaxhighlight lang="c++" inline> NULL </syntaxhighlight> macro.
 
For null pointer we use  <syntaxhighlight lang="c++" inline> nullptr </syntaxhighlight> instead of  <syntaxhighlight lang="c++" inline> NULL </syntaxhighlight> macro.
 +
 +
=Using your IDE auto formatter=
 +
Most available IDEs support source code formatting using [https://clang.llvm.org/docs/ClangFormat.html clang-format]. We recommend using coding style based on Google, with minor modifications concerning indentation and pointer location.
 +
 +
<syntaxhighlight>
 +
# Use the Google style in this project.
 +
BasedOnStyle: Google
 +
 +
# Custom adjustments.
 +
AccessModifierOffset: -2
 +
IndentWidth: 4
 +
ColumnLimit: 100
 +
 +
# Some folks prefer to write "int& foo" while others prefer "int &foo".  The
 +
# Google Style Guide only asks for consistency within a project, we chose
 +
# "int& foo" for this project:
 +
DerivePointerAlignment: false
 +
PointerAlignment: Left
 +
 +
# Do not sort includes.
 +
SortIncludes: false
 +
</syntaxhighlight>
 +
 +
More information about auto formatting in:
 +
* [https://code.visualstudio.com/docs/cpp/cpp-ide Visual Studio Code],
 +
* [https://www.jetbrains.com/help/clion/clangformat-as-alternative-formatter.html Jetbrains Clion]

Latest revision as of 14:06, 30 November 2020

This is a brief description of our coding style, roughtly following the Google C++ Style Guide.

You can also download the CLion settings: File:Settings.jar, which you can import in File/Import settings.

You can also download only our code style settings to import in CLion: File:mm.xml. It can be imported under Settings/Editor/Code Style/Scheme/Import scheme.

General

We use a 100 characters line width limit in C++ code.

Indentation

Indent using spaces. Indentation width is 4 spaces.

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

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 Weighted Least Squares (WLS) or Finite Elements Method (FEM) - UPPER_CASE

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

#define MAX_VALUE 500
class MyClass {
  public:
    MyClass(first_var, second_var) {
        ...
    }
    int getSize();
}

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. Comments before functions are used for documentation. Always use full sentences and end them with a punctuation mark.

/**
 * This function changes the world.
 * @param skynet If `true`, the world ends, otherwise nothing happens.
 */
double change_the_world(bool skynet) {
    if (skynet) {
        return 0.0;  // Brace for the end of the world.
    }
    ...
}

Use doxygen comments to generate documentation.

Headers

All headers must contain a header guard of form PATH_TO_FILENAME_HPP_ as enforced by the linter.

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.

int sumMe(int var) {  // yes
    if (var == 1)
    {                 // no
        return 1;
    }
    return 0;
}

For null pointer we use nullptr instead of NULL macro.

Using your IDE auto formatter

Most available IDEs support source code formatting using clang-format. We recommend using coding style based on Google, with minor modifications concerning indentation and pointer location.

# Use the Google style in this project.
BasedOnStyle: Google

# Custom adjustments.
AccessModifierOffset: -2
IndentWidth: 4
ColumnLimit: 100

# Some folks prefer to write "int& foo" while others prefer "int &foo".  The
# Google Style Guide only asks for consistency within a project, we chose
# "int& foo" for this project:
DerivePointerAlignment: false
PointerAlignment: Left

# Do not sort includes.
SortIncludes: false

More information about auto formatting in: