Skip to content

Coding style

Szilard Ledan edited this page Jun 26, 2016 · 7 revisions

File naming convention

gepard-<spec-name>.h/cpp

Header and Source files

The gepard-example.h:

/* TODO: Include contents of ./LICENSE
 * and update the contributors.
 */

#ifndef GEPARD_EXAMPLE_H
#define GEPARD_EXAMPLE_H

namespace gepard {
namespace example {
namespace {

...

} // anonymous
} // namespace example
} // namespace gepard

#endif // GEPARD_EXAMPLE_H

The gepard-example.cpp:

/* TODO: Include contents of ./LICENSE
 * and update the contributors.
 */

#include "config.h"
#include "gepard-example.h"

#include "other-header.h"
#include <system-header.h>

Class definitions

class FloatPoint {
public:
    FloatPoint(Float x, Float y)
        : x(x)
        , y(y)
    {}

    double modulus() const
    {
        return sqrt(this->x * this->x + this->y * this->y);
    }

    double length() const { return _length; }
    double area() const;

    double x;
    double y;

protected:
    double _length;

private:
    double _area;
};

If conditional statements

In case of one line early return:

if (cond)
    return value;
// or
if (cond) {
    return value;
}

If there is an else case, then always use brackets ({...}):

if (cond) {
    ...
} else {
    ...
}

The switch statement

switch (cond) {
case 1:
case 2:
    ...
    break;
case 3:
    ...
    // Fall through.
case 4: {
    ...
    break;
}
default:
    ...
    break;
}

Loops

for (int i = 0; i < n; i++) {
    ...
}

for (auto& element : container) {
    ...
}

while (cond) {
    ...
}

do {
    ...
} while (cond);

Function declarations

static inline double min(double& a, double& b) const
{
    ...
}

double FloatPoint::area()
{
    ...
}

Defines

Please use GD_ (GeparD) prefix for every own defines.

#ifdef GD_CRASH
#undef GD_CRASH
#endif
#define GD_CRASH(ARG) ...