Skip to content

Code Style Guide

Aman Sahil edited this page Mar 23, 2020 · 1 revision

Code Templates

Model

Naming: example_model, with first section sharing the same name as the controller class name

class ExampleModel : public QObject
{

    Q_OBJECT

public:
    explicit ExampleModel(QObject *parent = 0);
}

Controller

Naming: example

class Example : public QMainWindow
{
    Q_OBJECT;

    // Listeners & UI Methods

public:
    explicit Example(QWidget *parent = nullptr);
    ~Example();

    // Controller Methods
}

Custom container

Naming: string_containertype.cpp

class ContainerTypeNode {};

class StringContainerType {};

Code Standards

Follow google guidelines unless explicitly mentioned below

Code can be kept neat with linters and analysis tool. Below are some recommendations:

cppcheck --enable=all --inconclusive --std=posix example.cpp

Code Philosiphy

"SOLID principles sounded to me like extremely bureaucratic programming that came from the mind of somebody that has not written a lot of code, frankly" - Stack Overflow chairman (Joel Spolsky)

Note : We think SOLID is great but know when not to use it.

General guidelines

  • Don't add any classes to the codebase unless absolutely needed. Best way to go about this is, don't add empty abstract classes before you implement certain functionality, create classes if necessary as you go along

  • Use QT containers instead of CPP STL containers unless it's being used in a custom container

  • Check if QT has existing functionality for the problem before implementing custom functionality or adding external libraries

  • Composition over inheritance

General style guidelines

  • Use proper tabulation of 4 spaces, do not to tabulate access identifiers

Eg:

class someclass
{
public:
    void someFunction();
};
  • Attach asterisk or ampersand to the variable name instead of the container name

Eg:

// Use this
    int *bar;

// Instead of
    int* bar;
  • Use inline functions for code that is only one line

  • Curly braces should be on a new line unless it's an inline function

  • If variables aren't being reassigned use const

  • If methods that aren't manipulating any state use const

  • Use CPP foreach loop instead of an incrementor unless necessary

Eg:

// Use this
for (const auto &e: someList )
{

}

// Intstead of
for (int i = 0; i < someList.length(); i++)
{

}
  • Do not use declarations in a header file

  • Use include guards

  • Avoid magic numbers

  • Use the define directive when declaring a global variable where possible

Naming guidelines

File Names: Snake case

Classes: Initial capital and then camel case

Enums: Initial capital and then camel case

EnumValues: Initial capital and then camel case

Struct: Initial capital and then camel case

Functions: Camel case

Variables: Camel case

Constants: All caps and snake case

Header file guidelines

  • Only add includes to header files instead of CPP files unless necessary

  • Use double quotes when including custom files and angled brackets when including library files

  • The order is custom includes first then library includes

Eg:

#include "example.h"
#include "someotherexample.h"


#include <vector>
#include <QObject>
  • Have sensible for ordering members in header files

  • The order priority for header file is sub-classes, public, protected and then finally private

  • The order for members is enums, structs, data members and finally member functions

Eg:

class exampleClass
{
private:
    class subClass
    {
        //...
    };

public:
    void somePublicFunc();

protected:
    int someVariable;

    void someProtectedFunc();

private:
    void somePrivateFunc();
}

CPP file guidelines

  • Order function in CPP files according to the order in the header file

Post-increment vs Pre-increment

  • This codebase uses post-increment instead of pre-increment solely for styling and consistency purposes

Exceptions guidelines

  • Avoid using asserts as they take a toll on performance, remove all asserts before submitting a pull request

  • Generally, try to write exceptionless code and only throw an exception in case of a disaster

Clone this wiki locally