Skip to content

VantTec CPP Standard

Alejandro Gonzalez edited this page Mar 7, 2020 · 9 revisions

VANTTEC C++ STYLE GUIDE

To create this code standard, we took in consideration the Google C++ Style Guide

For recommendations and modifications, please refer to Pedro Sánchez, Roberto Mendivil or Sebastian Martinez


Project Structure

Every cpp project must have the next file structure:

  • include
  • src
  • test
  • CMakeList.txt
  • README

Object Oriented Programming Code Structure

Class folder

  • class.h
  • class.cpp
  • CMakeLists.txt
  • README.md
  • readme folder *test_folder *class_test.cpp

The #define Guard All header files should have #define guards to prevent multiple inclusion. Always use the next format:

<PROJECT>_<PATH>_<FILE>_H_.

For Example, the file foo/src/bar/baz.h in project foo should have the following guard:

#ifndef FOO_BAR_BAZ_H_

#define FOO_BAR_BAZ_H_

...

#endif // FOO_BAR_BAZ_H_


Names and order of includes

  • Include headers in the following order:
  • C System headers (std)
  • C++ Standard Library headers
  • Other libraries’ headers (third-party)
  • Your project's headers.

Separate each non-empty group with one blank line and sort them in alphabetical order.


Namespaces

Do not use namespaces (Can be used for global functions)


Variables

Local Variables

Always initialize variables before using it Example:

int i = 0 std::vector<int> v={1,2,3}

Declare variable close to its use Example:

const char *p= temp

*p = foo();

If and only if variables are only used on loops (whiles), then Variables can be initialize on loops statements. Example:

while(char *p = foo() < other condition...)

Otherwise on nested loops variables must be declare before the loop Example:

int temp1=0;

int temp2 =0;

    while (temp < range ){

        while(temp2 < range2){

            temp2++;

            }

       temp1++;

    }

Initialize objects as variables, always before and close to is use.


Naming

Names must always describe the main purpose Example:

int speedChallengeState = ..

usv_perception.cpp . Avoid the use of abbreviations and incomplete words Example:

Good: int speedChallengeCounter= ..-

Wrong : int speedch_Cnt = ...

File Naming

  • Lower Case
  • Separate names with underscore ( _ ) ** or dashes (-)
  • Descriptive naming

Example:

sliding_mode_controller.cpp

Typedef naming

  • CapWord
  • start with UPPERCASE

Example: typedef hash_map<referenceFrames*, std::string> ReferenceFrame;

Class and Struct Naming

  • CapWord
  • start with UPPERCASE

Example: class SpeedChallenge {};

Function naming

  • camelCase
  • start with lowercase

Example: void decodificarXbee();

Variable Naming

  • lowercase
  • separate word with underscore ( _ )

Example: int bouy_red

For variables in classes end name with underscore

Example: int bouy_

Constant naming

  • Starts with k and then CapWord

Example: const int kStatesNumber = 9;


Macros

Do not use MACROS !

Use instead:

  • constants
  • inline functions
  • enum

White spaces in expressions and statements

Avoid extraneous white space in the following situations:

  • Avoid inside parenthesis, braces or brackets

YES Foo (a[0],(x+y))

NO Foo ( a[ 0 ] , ( x + y ) )


Comments

Comments at the beginning of files

/*

  @file :        file.cpp

  @date:         Thu Dec 26, 2019

  @date_modif:   Thu Dec 26, 2019

  @author:       name

  @e-mail:        

  @co-author:    (If multiple co-authors, write the name and e-mail of each one)

  @e-mail:

  @brief:

  @version:

  */

Class Comments

Comment before class only if it not descriptive

Functions Comments only on header file

  /*

  @brief:

  @param     a[in]:  describe 

             b[out]: describe

  @return

  */

Empty Lines between code blocks

Other conveniences and notes

Number of characters per line: 80


Suggestions

If you use Visual Studio Code as your code editor, you can add a vertical line into your screen, so you can see where your line should end. Just go to File >> Preferences >> Settings >> search for Editor:Rulers and in the json file just paste this:

"editor.rulers": [80]

Now you have a nice vertical line

Class vs Structs: Use a struct only for passive objects that carry data; everything else is a class