Skip to content

Coding Style

Felix Weilbach edited this page Jan 14, 2021 · 6 revisions

Coding Style

General

The coding style of the project is based on the coding style of QT. An overview of this coding style can be found here:

Variable names

Member variables should be prefixed with a underscore. For example write _myClassVariable instead of myClassVariable.

STL

When possible use data structures from QT. For example use QVector instead of std::vector.

Algorithms

When possible use algorithms from the STL or QT. For example use std::copy_if instead of coding it by hand.

Smart Pointers

QSharedPointer should be used instead of std::shared_ptr. However, it is okay to use a STL smart pointer if there is no equivalent in QT, like std::unique_ptr.

Unit Tests

Names of unit tests should follow the scheme proposed by Roy Osherove in his book The Art of Unit Testing. The general scheme is:

void testUnitOfWork_stateUnderTest_expectedBehaviour()

It is explained here.

Example

The following shows how a class that inherits from QObject should be declared:

class Foo : public QObject
{
    Q_OBJECT
    Q_PROPERTY(....)
public:
    enum Bar {
    };
    Q_ENUM(Bar)

    static void staticMethods();

    explicit Foo(QObject *parent = nullptr);

    bool publicMethod() const;

signals:
   void mySignal();

protected slots:
    void protectedSlot();

protected:
    void protectedMethod();

private slots:
    void privateSlot();

private:
    void privateMethod();

    bool _member;
};