Documentation

Every function and class should be documented and thoroughly commented.

Use Doxygen standards, which CLion should automatically register and autocomplete.

https://www.doxygen.nl/index.html

Bracket usage

When using brackets, always do

void foo(params) {
}
//NOT
void foo(params)
{
}

Class file ordering

  • Libraries on top and interfaces to other classes below, group these by type

  • Public goes above private in the .h

  • Function ordering in the .cpp should match the .h

#include <iostream>

#include "ExampleClass.h"

class DateClass {
    public:
        int mYear{};
        int mMonth{};
        int mDay{};
    private:
        int mTime{};
};

Structs and enums

Do not use typedefs for structs.

Structs must also be used for data only.

struct Date {
    int year{};
    int month{};
    int day{};
};
//NOT
typedef struct {
    int year{};
    int month{};
    int day{};
} Date;

Use enum class, not enum.

enum class Color {
    red,
    blue
};
//NOT
enum Color {
    red,
    blue
};

using, not typedef       

Use the using type alias rather than typedef:

using distance_t = double; // define distance_t as an alias for type double
//NOT
typedef double distance_t; // define distance_t as an alias for type double

const, not define

Avoid using #define to create symbolic constants macros.

const double GRAVITY{ 9.8 }; // preferred use of const before type

Naming conventions

  • Variables are camelCase.

  • Methods are camelCase.

  • Classes are PascalCase.

  • Static constants are UPPER_SNAKE_CASE.

class EncodedPacket {
    public:
        EncodedPacket(uint8_t *data, uint8_t len);
        const uint8_t *getData() const;
        uint8_t getLen() const;
        DecodedPacket *decode();

    private:
        const uint8_t * const data;
        const uint8_t len;
        Static const 
        void print(){
            std::cout << "This is a function!";
        }
};

 

Constants

 

Everything should be a constant reference if possible. By default, functions will make a copy of what is passed in, which we do not want.

uint8_t const &dataRef = data;
//NOT
uint8_t dataRef = data;

When initializing a function:

EncodedPacket(uint8_t *dataRef);
//NOT
EncodedPacket(uint8_t dataRef);

Constexpr, not Macros

Use constant expressions (constexpr) for small helper functions.

constexpr int square(int x) { return x*x; }
//NOT
#define SQUARE(r) ((r)*(r))
double square_macro = AREA(4.6);

Assertions

Occasionally use assert statements to double-check any condition. If the statement inside the assert is false, the program will terminate.

bool value = 8;
assert(value != NULL);
assert(value == 5); //Program will terminate

 


  • No labels