Coding Style

SokoloffA edited this page Feb 9, 2013 · 11 revisions

This document describes the recommended coding style for Razor-qt project. Nobody is forced to use this style, but to have consistent formatting of the source code files it is recommended to make use of it.

Indentation

  • No tabs
  • 4 Spaces instead of one tab

Variable declaration

  • Each variable declaration on a new line
  • Each new word in a variable name starts with a capital letter (so-called camelCase)
  • Underlining is used only for special cases
  • All class members must be prefixed with 'm'
  • Avoid abbreviations
  • Take useful names. No short names, except:
  • Single character variable names can denote counters and temporary variables whose purpose is obvious
  • Variables and functions start with a lowercase letter

Example:

// Wrong
KProgressBar *prbar;
QString prtxt, errstr, file_Name;
class A 
{
private:
    int count;
};

// Correct
KProgressBar *downloadProgressBar;
QString progressText;
QString errorString;
DbusMounter busMounter_hal;
DbusMounter busMounter_udisk;
class A
{
private:
    int mCount;
};

Whitespace

  • Use blank lines to group statements
  • Use one space after each keyword
  • For pointers or references, use a single space before '*' or '&', but not after
  • No space after a cast

Example:

// Wrong
QString* myString;
if(true)
{
}
 
// Correct
QString *myString;
if (true) 
{
}

Braces

As a base rule, the left curly brace always goes on the separate line:

Example:

// Correct
static void foo(int g)
{
    if (g)
    {
        qDebug("foo: %i", g);
    }
    else
    {
        qDebug("foo: zero");
    }
}
 
class Moo
{
};

Switch statements

Case labels are on the same column as the switch

Example:

// Correct
switch (myEnum) 
{
case Value1:
    doSomething();
    break;
case Value2:
    doSomethingElse();
    // fall through
default:
    defaultHandling();
    break;
}

Line breaks

Try to keep lines shorter than 100 characters, inserting line breaks as necessary.

Including headers

  • In source files, include specialized headers first, then generic headers.

Example:

// right
#include <qstring.h> // Qt class
#include <new> // STL stuff
#include <limits.h> // system stuff
  • If you need to include qplatformdefs.h, always include it as the first header file.

  • If you need to include qt_x11_p.h, always include it as the last header file.

Unnecessary trailing whitespaces

  • Check for unnecessary whitespaces with "git diff --check" before committing.

Automatic code formatting script

A shell script can be found at scripts/astyle-razorqr.sh. It depends on astyle => 1.24.

It applies razor-qt coding style to all c, cpp and header files in and below the current directory.

Other helpful documents