D0 global tracking C++ style guide

D. Adams
24jan00 1530


Introduction

A large number of people with diverse backgrounds contribute to the global tracking software project. This document contains a list of style recommendations. The goal is to produce code that is easily understood and consistent.

File names

Library components typically define one class and the base file names are same as the class name. The header file appends the extension .hpp, implementations (aka sources) add .cpp and component tests add _t.cpp. Thus, the header for class MyClass is MyClass.hpp, the source is MyClass.cpp and the test is MyClass_t.cpp.

TRF++ code follws the same convention except the header extension is .h (e.g. MyClass.h).

Template definitions which are not inlined are put in a separate template definition file with extension .tpp (MyClass.tpp). This file is included at the end of the header.

Class, function and variable names

Class names begin with uppercase as do any words within the name. A class describing an auotmatic teller might be AutoTellerMachine. Acronyms are capitalized like ordinary words. Thus the class describing a touch screen for an ATM might be named AtmTouchScreen.

Function and variable names are all lowercase with underscores separating words. Thus the name of the an ATM screen instance might be atm_screen and the function to update the screen could be named update_atm_screen().

Pointer names always begin with p. Object names may also begin with p if it is natural. An extra prefix p qould indicate a pointer. Thus an object of type Penguin might be named penguin1 and its pointer named ppenguin1.

Member attribute names begin with an underscore, e.g. _atm. Member pointers begin with _p, e.g. _patm.

Indentation

Any blocks of code such as those inside function or class definitions, for loops or if blocks should be indented in a consistent manner. The level of corresponds to the level of nesting. The standard is to use two spaces per level of indentation. Thus the code for an if block inside a for loop in a function definition would be indeted by six spaces.

The opening brace generally appears on the same line as its preceeding code and the closing brace appears by itself without the extra level of indentation.

Comments are indented to the same level. Here is an example:

int mysum(int i) {
  int sum = 0;
  if ( i > 0 ) {
    for ( j=0; j<i; ++j ) {
      sum += j;
    }
  } else {
    // For i < 0, set value to indicate error.
    sum = -1;
  }
  return sum;
}


adams@rice.edu