Tool Development Coding Style Guidelines

Indentation

  • Use only spaces; don't use tabs.
  • Use four spaces for indentation.
#include <cstring>

namespace DataStructures
{
    template <typename T>
    class DynamicArray
    {
    public:
        DynamicArray(int size, T initValue)
            : mSize(size),
              mData(new T[mSize]);
        {
            for (int i = 0; i < mSize; ++i)
            {
                mData[i] = initValue;
            }
        }

    private:
        size_t mSize;
        T *mData;
    };
}

Declarations

  • Variables
    • Declare each variable on a separate line.
    • Variable names start with lowercase letters.
    • UseCamelCaseForVariable names.
    • Avoid short meaningless names.
    • Private or protected class members are prefixed with 'm'.
    • Single character variable names only on temporary counters.
    • Declare variables until they are needed, a.k.a. avoid C style variable declaration.
    •  

Declarations (Variables)

  • Start variable names with lowercase letters.
  • Declare each variable on a separate line.
  • Avoid short meaningless names.
  • Use camel case for multiple word variables.
// Avoid.
int a, b, area_of_rectangle;
char *c, *d;

// Prefer.
int height;
int width;
char *name = nullptr;
char *title = nullptr;
int areaOfRectangle = height * width;

Declarations (Variables)

  • Declare variables until they are needed.
  • If possible avoid declaring global variables.
  • When declaring global variables prefix them with a 'g'.
  • If Qt is available use the Q_GLOBAL_STATIC macro.
  • Declare global string literals as const char[].
/* Avoid. Might fail because static initialization order is undefined. */
Database db;
QueryProcessor qp(db); 

/* If inevitable and available. Guarantees thread-safe initialization on first use. */
Q_GLOBAL_STATIC(Database, gDb);
Q_GLOBAL_STATIC_WITH_ARGS(QueryProcessor, gQp, gDb);  

// Avoid.
const  string gStrAppName = "Code Convention";

// Prefer.
const char gCharAppName[] = "Code Convention";

int main() 
{
    // Avoid.
    int h, w, area;
    h = 10;
    w = 10;
    a = h * w;

    // Prefer.
    int height = 10;
    int width = 10;
    int area = height * width;

    return 0;
}

Tool Development Coding Style Guideline

By Victor Romero

Tool Development Coding Style Guideline

  • 621