guidelines-coding-style/Style-Cpp.md

4.9 KiB

Coding Style for C/ C++

TODO: Update with less details/ more references to CppCoreGuidelines and auto-formatting.

OLD/ EXISTING STUFF

UPDATE: Take a look at http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines.

general

  • write in english
  • make your code beautiful to read! e.g. intend all #define values to the same column
  • use UNIX line breaks \n instead of DOS \r\n
  • indent code by 2 spaces for each level, no tabs!
  • every file ends with one blank line (newline at EOF)
  • no trailing white space should be committed! (except markdown of course)

variables, functions and classes naming

#define SOME_DEFINE
int someInteger = 0;
void functionName(int param) {}
class ClassName {
  void someMethod() {}
}
  • use camelCase, except preprocessor directives
  • underscores could be used for separating "prefixes" for organizing variables
  • use names describing the function of the variable/ function/ method.

preprocessor directives

  • all upper case, separating word by an underscore (SOME_DEFINE)

variables

  • camelCase, beginning with a lower case character (someVariable)

functions

  • camelCase, beginning with an lower case character (someFunction())

classes: class name, methods and class/ instance variables

  • classes: camelCase, beginning with an upper case character (SomeClass)
  • methods: camelCase, beginning with a lower case character (someClassMethod())
  • private variables: camelCase, ending with an underscore, beginning with a lower case character (somePrivateVariable_)

white space and braces in basic structures

For the eclipse IDE, please use the code style formatter file attached in this Gist. Import it by selecting Settings -> C/C++ -> Code Style -> Formatter.

int main() {
  int foo = 0;

  for (int i = 0; i < 10; i++) {
    foo = myFunction(i);
  }

  if (foo > 3) {
    // do a
  } else {
    printf("foobar");
  }

  while (i <= 2) {
    i++;
  }

  switch (foo) {
    case 1:
      // do 1
      break;
  
    case 2:
      // do 2
      break;
    
    default:
      // do nothing in default
  }
}

int myFunction(int param) {
  return param;
}

class MyRessource {
public:
  MyRessource() {}
  virtual ~MyRessource(){}
  MyRessource(const MyRessource&){} MyRessource& operator=(const MyRessource&){}
private:
  void* handle_;
};
  • no trailing white space!

functions

  • no white space between function name and argument

instructions and conditions

  • in conditions checking for equality (==), write the variable to the right!
  • separate the operator from the arguments by a white space to improve legibility, e.g. a <= b instead of a<=b.

curly braces

  • use them every time! Even if the C syntax allows if and while statements to have a single instruction without curly braces surrounding the instruction.
  • set the opening curly brace in the same row as the statement, close them in a new line after the last instruction

if, while, switch-case

  • add a white space between the keyword and the opening brace of the condition
  • add a white space between the closing brace of the condition and the opening curly brace
  • add a white space before an else, the else keyword is on the same line as the closing bracket of the if-part

other, non formatting specific, coding guidelines and hints

includes and header files

  • include most specific first, standard library last
  • internal classes (only for this module) should only be declared in the cpp file, not in the header, because they are not part of the interface. (The header file is what defines the interface of a C++ module)
  • if anotherclass is used as variable, it's header file must be included.
    If it's only used by reference only use a forward declaration (class FooBar; at the beginning of the header file).
    If necessary include the header file of the used class in the .cpp file, e.g. if some method of the class FooBar is used in the implementation.

C++ classes implementation hints

  • use RAII (resource aquisition is initialization)

  • use initialization at CTOR (init list) (since C++11)

class MyClass {
public:
  MyClass(int a, float b) : somePrivateVariable_(a), someOtherVar_(b) {}
private:
  int somePrivateVariable_;
  float someOtherVar_;
};
  • use rule of three: A class which binds a resource, must provide: CTOR and DTOR, copy CTOR, assignment operator.
    Define copy CTOR and assignment as private, if these operations should not be possible.
class MyRessource {
public:
  MyRessource() {}
  virtual ~MyRessource(){}
  MyRessource(const MyRessource&){} MyRessource& operator=(const MyRessource&){}
private:
  void* handle_;
};
  • use of const:
    • int getValue() const {return val;} ==> the corresponding class will not be changed
    • void foo(const BigClass& c, OtherClass& d); ==> only const functions of BigClass can be called
    • const int MAX_VALUE = 1234; is stored in ROM, but cannot be changed