Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Coding Style

Overview

The code indentation and formatting was completely standardized before prior to open sourcing the code. The command line tool indent did most of the heavy lifting.

...

For 2.2 we would like to change the prefix INK_/ink_/ink to TS_/ts_/ts.
We are also looking to cleanup filenames, directory structure and other
global style issues. See the page above for details.

Sample Code

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

This document relies on code samples which are below to demonstrate most of the rules used on the Apache Traffic Server project.

Code Block
titlesample.c
borderStylesolid

Code Block
#include <iostream>

using namespace std;

namespace ts
{
  /**
   * @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
    void _helpers() { _foo++; }

    int _count;			/// if there is something special to say
    int _totalCount;
  protected:
    void _anotherHelper() { _foo++; }
    int _foo;
  };
}


/**
 * @brief This is the main function for the program
 */
int
main()
{
  ts::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;
}

...