Coding Style Guidelines

EPICS v4 Working Group, First Working Draft, 26-Oct-2011

This version:
Coding_Style_Guidelines_20111026.html
Latest version:
Coding_Style_Guidelines.html
Editors:
Matej Sekoranja, Cosylab

Abstract

This document defines EPICS v4 coding guidelines for C++

Status of this Document

This is the 26 October 2011 version of the coding style guidelines.

The present code does not completely follow guidelines described in this document.

The terms MUST, MUST NOT, SHOULD, SHOULD NOT, REQUIRED, and MAY when highlighted (through style sheets, and in uppercase in the source) are used in accordance with RFC 2119 [RFC2119]. The term NOT REQUIRED (not defined in RFC 2119) indicates exemption.

Table of Contents

  1. General rules
  2. Comments
  3. Identifiers
  4. Code blocks
  5. Operators
  6. Other coding rules

General rules

Comments

Both C and C++ style comments are allowed.

// Compute the factorial.
int factorial = 1;
for (int i = 2; i != 11; i++)
    factorial *= i;

// Present the result to the user.
cout << "Factorial of 10 is " << factorial << "." << endl;

Identifiers

Identifiers follow Java naming convention using mixed case, e.g. bufferSize.

Identifiers should be meaningful rather than arbitrary. The only exception are control variables of short cycles where short names like i, iter etc. can be used.

int sum = 0;
for (int i = 0; i != 100; i++)
     sum += i;

The type of the variable should not be explicitly marked in the identifier. I.e. no hungarian notation.

Code blocks

For functions, structures, classes, enums and namespaces both opening bracket and closing bracket are placed on separate lines:

void fx()
{
    // Code goes here.
}

With for, while, if and else blocks opening bracket is on the same line as the control statement:

if (sum > 1000) {
    // Code goes here.
}

Even if for, while, if or else block contains only a single statement, the statement should be placed on a separate line:

if (end)
    exit(1);

If the indenting of the controlled block incidentally collides with the control statement that's broken into two lines, second line of the control statement should be indented by 6 spaces rather than 4 to prevent confusion:

if (veryLongVariableName == 10000 &&
      anotherHorrendousVariableName == 1000000 &&
      ludicrouslyLongVariableName == 1000000000)
    counter++;

Operators

Unary operators are not separated from the expression in any way:

counter++;

Binary operators should be separated from adjacent expression by spaces:

z = x + y;

Method and function calls are not separated from the expression by space(s):

someFunction(myArray[0]);

Other coding rules