Revised 2006-09-05 DMB

Return to the Index


Coding Standards

This document describes the coding standards that should be followed when developing within Avida.

 

Coding Style Guidelines

Indentation

 

Braces

Function Definitions

Open and close braces should each be on their own line. Do not place the open brace on the same line as the function signature. The only exception to this rule is single line class methods, in which both braces are allowed to be on the same line as the code (but only in pairs). Examples:

Right:
void cFoo::Method()
{
  // code goes here
}

// ... in a class
inline void Method() { ; }

inline void Method()
{ // longer code in here }
Wrong:
void cFoo::Method() {
  // code goes here
}

// ... in a class
inline void Method() { 
  ; }

For, While, Do Loops and Switch Statements

The open brace should go on the same line as the control structure, the close brace on its own line. Examples:

Right:
while (foo) {
  // code goes here
}
Wrong:
while (foo)
{
}

If/Else Statements

The same formatting rules as for/while/etc., but if there is an else clause the close brace should go on the same line as the else statement. Single line if else statements should not get braces, unless they accompany a multi-line statement. Examples:

Right:
if (foo) {
  DoSomething();
  DoAnotherSomething();
} else {
  DoADifferentSomthing();
}

if (!foo) CallAFunction();
else CallBFunction();
Wrong:
if (foo)
{
  DoSomething();
  DoAnotherSomething();
} else 
  DoADifferentSomthing();

if (!foo) {
  CallAFunction();
}
else CallBFunction();

 

Parentheses

Function Declarations and Calls

Do not use any space between the name and the open parenthesis, inside the parentheses, or before commas that separate arguments. A single space should follow commas that separate arguments. Examples:

Right:
void cFoo::Method(int arg1, double arg2);
int aFunction();
Wrong:
void cFoo::Method( int arg1 , double arg2 );
void cFoo::Method (int arg1, double arg2);
int Function ( );

Control Structures

Control structures such as if, for, while, do, and switch statements use a single space before the open parenthesis, but spaces inside them.

 

Other Punctation

 

Include Guards and Statements

 

Naming Conventions

 

Class Dependency Guidelines

Some groupings of classes should maintain important limitations with respect to class dependency.

Tools Classes

All classes developed as general utility classes should be placed within the source/tools directory. These classes should have no dependencies that extend outside of this directory.

STL Usage

Usage of the Standard Template Library should be very limited. A number of these templates have unspecified orderings that can vary across platforms, affecting consistency. Where possible, it is greatly preferred to use the local tools classes.


Return to the Index