Skip to content
Simon Fuhrmann edited this page May 22, 2017 · 5 revisions

Warning: This page is a work in progress. It is incomplete and may be incorrect.

The goal of this style guide is to achieve written code with visual consistency across the project. Another goal is to give advice which specific C++ features to avoid, to preserve the code's self-documenting properties and to avoid surprises.

File Names and Include Guards

All files use lowercase names with underscores to separate words. Header files have .h extensions, implementation files have the .cc extension. Include guards should contain the path, the file name and HEADER in the name. For example for file libs/sfm/ba_types.h, the include guard would be SFM_BA_TYPES_HEADER (omit libs/ and apps/ path component).

Includes

State C and C++ library includes first, then other libraries, then MVE libraries. Order includes in each section be name. Exmpale:

#include <algorithm>
#include <cstdlib>
#include <string>

#include "math/defines.h"
#include "sfm/defines.h"

Formatting Conventions

  • Types: Use CamelCase for all types, classes, structs, enums, template parameters, typedefs.
  • Variables: Use lowercase and underscores to separate words: int my_variable = 10;
  • Constants: Use uppercase and underscores for compile time constants constexpr int MY_CONSTANT = 10;
  • Functions decl and defs: Use lowercase with underscores: void my_member_fn (int value) const;
  • Function calls: Do not use spaces before the bracket when calling: int value = my_function();
  • Scoping: Curly brackets {} for scoping (function scopes, etc) go to their own line.

Line Wrapping

Keep your lines in the 80 character limit. Longer lines have to be wrapped. Use four spaces to indent wrapped lines. Do NOT align arguments. Put the return value on its own line.

std::vector<std::string>
this_function_just_returns_an_empty_vector (int some_argument,
    int another_argument)
{
    return std::vector();
}

Conversions

Issue literals with the correct type. Avoid implicit conversions, and perform explicit comparisons. Some conversions will issue warnings. Use std::size_t when looping over vectors.

// Issue the correct literal types.
double value = 0.0;
float another_value = 1.0f;
bool my_bool = false;
int* pointer = &value;

// Explicit comparisons (do not rely on implicit boolean conversion)
if (my_bool == false && pointer != nullptr) ...

// Do not implicitly convert to int.
for (std::size_t i = 0; i < my_vector.size(); ++i) ...