Menu

HOWTO Coding Style

Jens Nissen

Coding Standards

The ChessX project has a set of coding standards to help improve the consistency and readability of the code. The standards are based on the Qt coding style. If in doubt try to follow the Qt way of doing things. The guidelines are as follows:

Class names

Each word in a class name should begin with an uppercase letter, eg. Board, PlayerDatabase.

Class methods

Method names should start with a lower case letter for the first word, with any following words capitalised, eg. count(), doStandardMove(). For methods that represent common actions try to use Qt conventions, eg. if your class is a collection, use count() to return the number of elements.

Class properties

Class properties should always be private or protected. A pair of accessor functions should be created for each property that should be visible. The name of the property itself should start with "m_". For example, this is a code snippet for a property "foo" of "fooType":

public:
  fooType foo() const;
  void setFoo(const fooType& f);
private:
  fooType m_foo;

If the property is a boolean, use the form "isSomething()" for the "getting" function.

Consts

Methods that do not alter the state of a class should be made const. Non-primitive arguments should be passed as const references, eg. foo(const ClassName& c).

Enumerated types

Enumerated values should start with an uppercase letter, eg. enum Piece {King, Queen, Rook, Bishop, Knight, Pawn};

Bools and Flags

It is recommended to use flags rather than bools for APIs, as this aids readability. For example:

With a bool parameter:

findPlayers(prefix, 1000, false)

With an enumerated type:

findPlayers(prefix, 1000, PlayerDatabase::IgnoreCase)

When the parameter is a bool, it is unclear what the false means. However, when an enumerated type is used, its meaning is obvious.

Code formatting

Tabs should be used for indentation. Opening parenthesis after if or while blocks should be placed in the next new line and closing one should be put in a new line.

if (x == 0) 
{
  doSomethingElse();
}

Easy way to format files accordingly to our formatting policy is to use astyle with following parameters:

 astyle --recursive --suffix=none --style=allman --mode=c --indent=spaces=4 --add-brackets --unpad-paren --pad-oper ./*.cpp ./*.h