Wiki

Clone wiki

gtsam / C++ Coding Conventions

Lexical Conventions

  • Classes are Uppercase, methods and functions lowerMixedCase
  • We use a modified K&R Style, with 2-space tabs, inserting spaces for tabs - Download style file for eclipse. Simply import it into Eclipse by going to Eclipse Preferences, then C/C++ --> Code Style --> Formatter -->Import.
  • Use meaningful variable names, e.g., measurement not msm

C++, Functional Style!

We can have many of the advantages of languages like Haskell and ML if we try to be as functional as possible:

  • All objects instances are const from the moment they are constructed
  • All non-constructor methods are const, e.g.
#!c++
     int Object::myMethod() const
  • All object arguments should be const references, e.g.,
#!c++
     int myFunction(const Object& object)
  • Make a habit to return references, which is cheap (no copy), but the have to be const!
#!c++
     const Variable& Object::myPreciousInnards() const
  • None of this applies to primitive types like char, int, double: just copy them.

const Correctness, Tips and Tricks

  • stl:🗺:operator[] is evil ! If the specified key does not exist, it creates an empty object at that key's location. This is evil, because
    • there is no version that returns a const reference
    • you need an accessible default constructor
    • The solution is: use stl:🗺:find()

Avoid Naming Redundancy

Instead of a method

#!c++

       const double getmarkerSize( void ){
               return markerSize_;
       }

called as markerObj.getmarkerSize()

rename the instance variable and method name so you have

#!c++

       double size(void) const {
               return size_;
       }

called as marker.size()

Updated