Coding conventions and C++-style

This document provides an overview of the general coding conventions that are used throughout oomph-lib. Knowledge of these conventions will greatly facilitate the use of the library. Contributors to the library are expected to adhere to these standards.



Naming conventions

File names

All C++ source files end with the standard extensions *.h and *.cc.

General variables

Classes

Private data and access functions to private data

Use a capital first letter for private data, and the all-lowercase equivalent for the access functions. Examples:

Note: Do not use public data – ever! Make it private and and provide an access function – even if it seems "perfectly obvious" at the time of writing the class that the internal storage for the data item is "never going to be changed".

Pointers

Access functions to containers

Many classes have member functions that provide access to data in private containers (e.g. vectors); they are usually accompanied by a member function that returns the number of entries in that container. Naming conventions:

Template parameters

Use descriptive function/variable names



Layout etc.

Position of include statements

Layout of blocks

Indentation

Layout of functions, classes, etc.

The oomph-lib namespace

Namespace pollution

Layout of class definitions and include guards.

Here is an example of a complete header file, including include guards and library includes.

#ifndef OOMPH_SOME_CLASS_HEADER // Assuming that the file is
#define OOMPH_SOME_CLASS_HEADER // called some_class.h
// Include generic oomph-lib library
#include "generic.h"
// Add to oomph-lib namespace
namespace oomph
{
// =============================================================
/// Waffle about what the class does etc.
///
// =============================================================
template<class T>
class SomeClass : public SomeBaseClass
{
public:
/// Constructor: Pass coefficients n1 and n2
SomeClass(const unsigned& n1, const T& n2) : N1(n1), N2(n2)
{}
/// Access function to coefficient
inline unsigned n1() const
{
return N1;
}
/// Access function to other coefficient
inline T& n2() const
{
return N2;
}
protected:
/// Coefficient
unsigned N1;
private:
/// Second coefficient
T N2;
};
}
#endif


Debugging etc.

The PARANOID flag and error handling

Range checking

Self test routines



Other conventions

Const-ness

Only use int if a variable can actually take negative values

Only use "pass by reference"

Provide fully-functional or deliberately-broken copy constructors and assignment operators

Order of arguments

Access to elements in containers

Boolean member data

Macros

Inlining



PDF file

A pdf version of this document is available.