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


Return to the Index