Overview

There are many different coding styles (indentation, formating, naming conventions) used in the yts code base and can sometimes distracting. It is best to agree on one standard even if having to compromise on a style you have become to accustomed to.

This document will describe the indentation, formating, doxygen comments, class naming, and naming conventions to use.

Sample Code

Instead of describing how to format to begin with, here is a sample below that demonstrates most of these rules.

#include <iostream>

using namespace std;

namespace yts
{
  /**
* @brief Demonstrates how to format code
* This class demonstrates to developers how we should structure code
   */
  class Counter
  {
  public:
    Counter():_count(0), _totalCount(0) { }

    /**
  * @brief Increments the count by one or an optional value
  * @param delta The amount to increment the count, default is 1
     */
    void increment(int delta = 1) {
      _count += delta;
    }
    /**
  * @brief Returns the count value
  * @return Count value
     */
    int getCount() const {      // try to use const as much as possible
      return _count;
    }

  private:			// try to hide data
    int _count;			/// if there is something special to say
    int _totalCount;
  };
}


/**
 * @brief This is the main function for the program
 */
int main()
{
  yts::Counter x;
  if (x.getCount() == 0) {
    x.increment();
  } else {
    // make sure to always use braces for conditionals
  }

  // increment the counter 10 times
  for (int i = 0; i < 10; ++i) {
    x.increment();
  }

  cout << x.getCount() << endl;
}

Indentation and Formating

This style is mostly K&R. Using the GNU indent command line tool as a definition of the K&R style, below is described in detail what options make up the -kr option for the tool and how this style differs. It is also worth noting that most of these formating rules are mainly defaults from emacs, except for the indentation, so hitting TAB to indent the code in emacs will work well.

Using indent

# use indent with the following commands
$ alias indent='indent -nut -nbad -bap -nbbo -nbc -br -bls -ce -ci2 -cli0 -cs -d0 -di2 -nfc1 -nfca -hnl -i2 -ip0 -l120 -lp -npcs -nprs -psl -saf -sai -saw -nsc -nsob -nss'
$ indent foo.cc

The -kr option or "common style" is described as:
-nbad -bap -bbo -nbc -br -brs -c33 -cd33 -ncdb -ce -ci4 -cli0
-cp33 -cs -d0 -di1 -nfc1 -nfca -hnl -i4 -ip0 -l75 -lp -npcs
-nprs -npsl -saf -sai -saw -nsc -nsob -ns

The code sample above can be closely formated by using the following options. Because of the number of variations of how code can be structured, indent will give a close and workable solution.
-nbad -bap -nbbo -nbc -br -bls -ce -ci2 -cli0
-cs -d0 -di2 -nfc1 -nfca -hnl -i2 -ip0 -l120 -lp -npcs
-nprs -npsl -saf -sai -saw -nsc -nsob -nss

Below describes each option of K&R and how the modified list of options differ from it. To sum up the major differences, comment structure is ignored mostly to work well with doxygen and indentation is changed from 4 spaces to 2 spaces.

Naming conventions

Classes and Structures

Member Variables

Comments

TODO comments

Example of comment TODO:

<verbatim class="example">
// TODO handle case for negative config value
</verbatim>

Notice the format: