Skip to content

CPP Writing Guidelines

Nick Charron edited this page Jul 15, 2021 · 21 revisions

Our general rules:

Headers

  • All include files must have a single-inclusion guard using either #pragma once or #ifndef MYPROJECT_FOO_BAR_H at the top of the header file (the former is preferred)

  • For the #ifndef approach, the name of the identifier to test on should be derived from the library/component name and the filename to ensure it is unique. E.g., libbeam/beam_cv/raycast.h should be #ifndef BEAM_CV_RAYCAST_H

  • Put as many #includes as possible in the .cpp file and as little as possible in the .h file. The includes in the .cpp are only included when that one file is compiled, but the includes for the .h have to be included by every file that uses it.
    Via https://stackoverflow.com/questions/3002110/include-in-h-or-c-cpp.

  • Header include order:

    1. Header file corresponding to the .cpp file (if applicable). This will ensure that your header doesn't depend on the order in which is was included (use <file.h> not "file.h")
    2. All C standard library and UNIX includes: #include <sys/time.h>
    3. All C++ standard lib includes: #include
    4. All third-party open source includes: #include <pcl/point_types.h>
    5. All our software includes (use <file.h> not "file.h")
    6. All above categories should be ordered alphabetically for readibility
    7. All using and using namespace declerations

Naming Conventions

  • All C++ variables are named with lower case, and underscores between words (e.g., variable_name)
  • For private member variables, add an extra underscore at the end of the variable (e.g., private_variable_name_)
  • We generally like to access member variables using functions (e.g., GetVariableA(){return A_}). However, you may want to be able to access the member variables for your specific application. For example, point clouds may contain many variables that you often need to access, so in this case you can use public member variables (similar to PCL). For public member variables, do not use an underscore at the end of the variable
  • Functions should be named using uppercase letters for each word (e.g., PerformAction())

More Information

If we don't have our own rule, try to stick to the google C++ guide which can be found here:

Clone this wiki locally