Coding Guidelines

= General = Much of the Mumble codebase does not yet follow these new code guidelines. However if your write new code or adjust existing code make sure to follow them so we can reach a consistent style as soon as possible.


 * The Qt coding conventions mostly apply
 * Every source file should contain a license header
 * Lines should generally be no longer than 100 characters
 * Use newlines for logical grouping
 * Try to be standards (C++03) compliant and const correct
 * When interfacing with system code feel free to locally use the coding style recommended for that system
 * Consistency is important but these are no laws. Use your own judgement.

= Indentation =
 * Use tabs for indenting and space for lining up code.
 * This means using tabs only at the beginning of lines. Otherwise users cannot set their own tabstop without breaking formatting
 * Never indent preprocessor commands controlling control flow (e.g. #ifdef, #endif,...)

= Headers = ...
 * Include guards should use MUMBLE_PATH_FILENAME_H_ to prevent ambiguity. E.g. for the file MainWindow.h in src/mumble:
 * 1) ifndef MUMBLE_MUMBLE_MAINWINDOW_H_
 * 2) define MUMBLE_MUMBLE_MAINWINDOW_H_
 * 1) endif
 * Don't use non-standard #pragma once
 * Try to use forward declarations where possible to reduce coupling

= Naming =
 * Don't abbreviate names.
 * Good naming is essential for code maintainability. Treat it accordingly.

Classes

 * Use CamelCase names.

Member functions
Type camelCase(Type camelCase); Type getVariable const; /// Or for boolean bool isVariable const; /// or bool hasVariable const; void setVariable(const Type& variable);
 * Arguments as well as function names should be camelCase
 * Getters should be prefixed with get/is/has, be constant and name the variable they return
 * Setters should be prefixed with set and efficiently handle their argument

Qt Slots

 * Use an "on" prefix onSomethingHappening for manually wired slots
 * Feel free to use Qt auto-wire where possible (e.g. on_qpbRemove_clicked)

Member variables
m_ and s_ prefix
 * m_camelCase for member variables
 * s_camelCase for static member variables

Constants
static const int GLOBAL_SVAR; void fu { static const int LOCAL_SVAR = 4; const int localVar = 5 + bla; } class bar { static const int MEMBER_SVAR; const int memberVar; };
 * Use ALL_CAPS with underscore as a seperator for global, file global, static const member variables and static const local variables.
 * Non-static but const local variables are not capitalized, but formatted just like normal variables.

Types
class MyClass {}; typedef MyClass AlternativeClassName; enum MyEnum {};
 * Use CamelCase for type names

Enums

 * Use CamelCase for enumeration types
 * For disambiguation and improved auto-completion use a unique prefix for the enum variables. Prefer the capital letters in the CamelCase name if possible.

enum CamelCase {CC_FIRST, CC_SECOND, CC_THIRD};


 * If you need an end marker for an enumeration prefer COUNT_PREFIX
 * If the enumeration values need explanation use multiple lines

enum CamelCaseToo { /// First camel in the case CCT_FIRST, COUNT_CCT };

= Code style =

if statements
if (a > b    || b > c     || c > d) { doSomething; } else { doSomethingElse; } if (thingy) return stuff;
 * Split up long if statements at operators. Place the operators at the beginning of the new line instead of at the end.
 * Try to keep the logic in if statements understandable. E.g. use additional named variables or split one if statement into multiple statements if it makes the code more readable.
 * Always use { } unless there is no chance of misinterpreting the statement (e.g. returns)

Classes (e.g. a QObject based class)

 * Access specifiers should always be ordered public, public slots, signals, protected, private
 * QObject derived classes must have a Q_OBJECT statement to ensure signal/slot is working
 * Constructors with only one argument must be marked explicit

/// Class for managing things by foo. class MyClass : public QObject { Q_OBJECT public: /// Constructs MyClass without initializing foo management explicit MyClass(QObject *parent = NULL); /// Constructs MyClass for managing initialFooCount of foos MyClass(int initialFooCount, QObject *parent = NULL); ...    public slots: ...    signals: ...    protected: ...    private: /// Stores the number of foos since the last reset int m_fooCount; };


 * Constructor implementations

MyClass::MyClass(int initialFooCount, QObject *parent) : QObject(parent) , m_fooCount(initialFooCount) { doStuff; };

Switch

 * Each case/default must have a break or a return. Fall throughs must be explicitly commented.

switch (foo) { case A:         ... break; case B:    case C:          ... // Fall through default: break; }

= Comments = Try to use comments in a sensible way. Don't comment the obvious (e.g. no setBla "Sets bla") and don't feel obligated to comment every single parameter and function if the variable naming and types already make the behavior obvious.


 * Comment intent over behavior
 * When in doubt err on the side of to much comments
 * Make sure you always keep related comments up to date when changing code

Classes / Functions

 * Comment in header / at point of declaration
 * Use doxygen style comments
 * We use auto-brief style so your first sentence will be used like it was prefixed with @brief. Be sure to finish the first sentence with a '.'.

/// Use this for single line comments int foo; /// Use this also for big comments. /// /// @param parameter Takes param in meters /// @return parameter in feet on success. A value < 0 on failure. int bar(int param);


 * Place comments directly above the code they are referring to. Don't place them in the same line.