-
Notifications
You must be signed in to change notification settings - Fork 6
VantTec CPP Standard
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
Every cpp project must have the next file structure:
- include
- src
- test
- CMakeList.txt
- README
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_
- 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.
Do not use namespaces (Can be used for global functions)
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.
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 = ...
- Lower Case
- Separate names with underscore ( _ ) ** or dashes (-)
- Descriptive naming
Example:
sliding_mode_controller.cpp
- CapWord
- start with UPPERCASE
Example: typedef hash_map<referenceFrames*, std::string> ReferenceFrame;
- CapWord
- start with UPPERCASE
Example: class SpeedChallenge {};
- camelCase
- start with lowercase
Example: void decodificarXbee();
- lowercase
- separate word with underscore ( _ )
Example: int bouy_red
Example: int bouy_
- Starts with k and then CapWord
Example: const int kStatesNumber = 9;
Do not use MACROS !
Use instead:
- constants
- inline functions
- enum
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 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:
*/
Comment before class only if it not descriptive
/*
@brief:
@param a[in]: describe
b[out]: describe
@return
*/
Empty Lines between code blocks
Other conveniences and notes
Number of characters per line: 80
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