Skip to content

Coding Style

bioboy edited this page Nov 4, 2012 · 8 revisions

C++ Standard:

No use of C++11 features only available in version of gcc higher than 4.6 as listed at: GCC C++11 Support

Namespaces:

All namespaces should also have a corresponding directory under src/. The util namespace is reserved for code the is deemed reusable, and not necessarily specific to this project.

Filenames:

  • .hpp extension for header files
  • .cpp extension for source / implementation files

Header Inclusion:

Use forward declarations, include headers in implementation only where possible. When including project specific headers use the full path relative to the root of the source tree and include non project specific headers first as per the example below:

#include <sstream>
#include <string>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include "cmd/site/chmod.hpp"
#include "fs/chmod.hpp"
#include "fs/dircontainer.hpp"
#include "util/string.hpp"
#include "cfg/get.hpp"

Header Guards:

#ifndef __<NAMESPACE1>_<NAMESPACE..>_<NAME>_HPP
#define __<NAMESPACE1>_<NAMESPACE..>_<NAME>_HPP
...
#endif

Test Blocks:

#ifdef <NAMESPACE1>_<NAMESPACE..>_<NAME>_TEST
...
#endif

Classes:

class MyClass
{
  std::string something;
  std::string somethingElse; // camelCase with first letter lower

public:
  std::string Foo(int args, const std::string& here);

  const std::string& Something() const { return something; }
  const std::string& SomethingElse() const { return somethingElse; }
};

Functions:

std::string Foo(int args, std::string here)
{
  if (expr) DoSomething();
  else SomethingElse();

  if (expr) 
  {
    Multiple();
    Statements();
  }
  else
    SomethingElse();
    
  while (expr)
  {
      Statement();
  }
  
  // same for for, do loops
  
  switch (var)
  {
    case constant:
      Wow();
      break;

    default:
    {
      Uhoh();
      break;
    }
  }
}

Clone this wiki locally