2020-08-05 18:05:24 +03:00
# Coding Guidelines
All new code **must** follow the following coding guidelines. \
If you make changes in a file that still uses another coding style, make sure that you follow these guidelines for your changes. \
2018-03-23 10:00:08 +03:00
For programming languages other than C++ (e.g. JavaScript) used in this repository and submodules, unless otherwise specified, coding guidelines listed here applies as much as possible.
2020-08-05 18:05:24 +03:00
**Note 1:** I will not take your head if you forget and use another style. However, most probably the request will be delayed until you fix your coding style. \
**Note 2:** You can use the `uncrustify` program/tool to clean up any source file. Use it with the `uncrustify.cfg` configuration file found in the root folder. \
**Note 3:** There is also a style for QtCreator but it doesn't cover all cases. In QtCreator `Tools->Options...->C++->Code Style->Import...` and choose the `codingStyleQtCreator.xml` file found in the root folder.
2014-11-16 18:25:50 +03:00
2020-08-05 18:05:24 +03:00
## Table Of Contents
2018-07-16 23:33:11 +03:00
* [1. New lines & curly braces ](#1-new-lines--curly-braces )
* [a. Function blocks, class/struct definitions, namespaces ](#a-function-blocks-classstruct-definitions-namespaces )
* [b. Other code blocks ](#b-other-code-blocks )
* [c. Blocks in switch's case labels ](#c-blocks-in-switchs-case-labels )
* [d. If-else statements ](#d-if-else-statements )
* [e. Single statement if blocks ](#e-single-statement-if-blocks )
* [f. Acceptable conditions to omit braces ](#f-acceptable-conditions-to-omit-braces )
* [g. Brace enclosed initializers ](#g-brace-enclosed-initializers )
* [2. Indentation ](#2-indentation )
2018-07-16 23:39:40 +03:00
* [3. File encoding and line endings ](#3-file-encoding-and-line-endings )
* [4. Initialization lists ](#4-initialization-lists )
* [5. Enums ](#5-enums )
* [6. Names ](#6-names )
2018-07-16 23:33:11 +03:00
* [a. Type names and namespaces ](#a-type-names-and-namespaces )
* [b. Variable names ](#b-variable-names )
* [c. Private member variable names ](#c-private-member-variable-names )
2018-07-16 23:39:40 +03:00
* [7. Header inclusion order ](#7-header-inclusion-order )
* [8. Include guard ](#8-include-guard )
* [9. Misc ](#9-misc )
2018-07-16 23:33:11 +03:00
* [10. Git commit message ](#10-git-commit-message )
* [11. Not covered above ](#11-not-covered-above )
2020-08-05 18:05:24 +03:00
2018-07-16 23:33:11 +03:00
---
2020-08-05 18:05:24 +03:00
## 1. New lines & curly braces
### a. Function blocks, class/struct definitions, namespaces
2018-03-26 19:03:08 +03:00
2014-11-16 18:25:50 +03:00
```c++
int myFunction(int a)
{
2017-09-04 18:55:37 +03:00
// code
2014-11-16 18:25:50 +03:00
}
2015-11-21 11:25:14 +03:00
void myFunction() {} // empty body
MyClass::MyClass(int *parent)
2020-11-16 10:02:11 +03:00
: m_parent {parent}
2014-11-16 18:25:50 +03:00
{
2017-09-04 18:55:37 +03:00
// initialize
2014-11-16 18:25:50 +03:00
}
2015-11-21 11:25:14 +03:00
int MyClass::myMethod(int a)
2014-11-16 18:25:50 +03:00
{
2017-09-04 18:55:37 +03:00
// code
2014-11-16 18:25:50 +03:00
}
2015-11-21 11:25:14 +03:00
class MyOtherClass
2014-11-16 18:25:50 +03:00
{
public:
2017-09-04 18:55:37 +03:00
// code
2014-11-16 18:25:50 +03:00
protected:
2017-09-04 18:55:37 +03:00
// code
2014-11-16 18:25:50 +03:00
private:
2017-09-04 18:55:37 +03:00
// code
2014-11-16 18:25:50 +03:00
};
2015-11-21 11:25:14 +03:00
namespace Name
2014-11-16 18:25:50 +03:00
{
2017-09-04 18:55:37 +03:00
// code
2014-11-16 18:25:50 +03:00
}
2015-11-21 11:25:14 +03:00
// Lambdas
[](int arg1, int arg2) -> bool { return arg1 < arg2 ; }
[this ](int arg )
{
this->acc += arg;
}
2014-11-16 18:25:50 +03:00
```
2020-08-05 18:05:24 +03:00
### b. Other code blocks
2014-11-16 18:25:50 +03:00
```c++
2020-11-16 10:02:11 +03:00
if (condition)
{
2017-09-04 18:55:37 +03:00
// code
2014-11-16 18:25:50 +03:00
}
2020-11-16 10:02:11 +03:00
for (int a = 0; a < b ; + + b )
{
2017-09-04 18:55:37 +03:00
// code
2014-11-16 18:25:50 +03:00
}
2020-11-16 10:02:11 +03:00
switch (a)
{
2014-11-16 18:25:50 +03:00
case 1:
2017-09-04 18:55:37 +03:00
// blah
2014-11-16 18:25:50 +03:00
case 2:
2017-09-04 18:55:37 +03:00
// blah
2014-11-16 18:25:50 +03:00
default:
2017-09-04 18:55:37 +03:00
// blah
2014-11-16 18:25:50 +03:00
}
2020-11-16 10:02:11 +03:00
{
// code
}
2014-11-16 18:25:50 +03:00
```
2020-08-05 18:05:24 +03:00
### c. Blocks in switch's case labels
2014-11-16 18:25:50 +03:00
```c++
2020-11-16 10:02:11 +03:00
switch (var)
{
case 1:
{
2017-09-04 18:55:37 +03:00
// declare local variables
// code
2015-01-13 22:30:12 +03:00
}
break;
2020-11-16 10:02:11 +03:00
case 2:
{
2017-09-04 18:55:37 +03:00
// declare local variables
// code
2015-01-13 22:30:12 +03:00
}
break;
2014-11-16 18:25:50 +03:00
default:
2017-09-04 18:55:37 +03:00
// code
2014-11-16 18:25:50 +03:00
}
```
2020-08-05 18:05:24 +03:00
### d. If-else statements
2018-03-26 19:03:08 +03:00
The `else if` /`else` must be on their own lines:
2020-08-05 18:05:24 +03:00
2014-11-16 18:25:50 +03:00
```c++
2020-11-16 10:02:11 +03:00
if (condition)
{
2017-09-04 18:55:37 +03:00
// code
2014-11-16 18:25:50 +03:00
}
2020-11-16 10:02:11 +03:00
else if (condition)
{
2017-09-04 18:55:37 +03:00
// code
2014-11-16 18:25:50 +03:00
}
2020-11-16 10:02:11 +03:00
else
{
2017-09-04 18:55:37 +03:00
// code
2014-11-16 18:25:50 +03:00
}
```
2020-08-05 18:05:24 +03:00
### e. Single statement if blocks
2018-03-26 19:03:08 +03:00
Most single statement if blocks should look like this:
2020-08-05 18:05:24 +03:00
2014-11-16 18:25:50 +03:00
```c++
if (condition)
2015-01-13 22:30:12 +03:00
a = a + b;
2014-11-16 18:25:50 +03:00
```
2018-03-26 19:03:08 +03:00
One acceptable exception to this can be `return` , `break` or `continue` statements,
provided that the test condition isn't very long and its body statement occupies only one line.
However you can still choose to use the first rule.
2020-08-05 18:05:24 +03:00
2014-11-16 18:25:50 +03:00
```c++
2018-03-26 19:03:08 +03:00
if (a > 0) return;
2014-11-16 18:25:50 +03:00
2020-11-16 10:02:11 +03:00
while (p)
{
2018-03-26 19:03:08 +03:00
// ...
if (!b) continue;
}
2014-11-16 18:25:50 +03:00
```
2020-08-05 18:05:24 +03:00
### f. Acceptable conditions to omit braces
2018-03-26 19:03:08 +03:00
When the conditional statement in `if` /`else` has only one line and its body occupy only one line,
2020-08-05 18:05:24 +03:00
this also applies to loops statements. \
2018-03-26 19:03:08 +03:00
Notice that for a series of `if - else` branches, if one branch needs braces then all branches must add braces.
2020-08-05 18:05:24 +03:00
2018-03-26 19:03:08 +03:00
```c++
if (a < b ) / / conditional statement
do(a); // body
if (a < b )
do(a);
else if (a > b)
do(b);
else
do(c);
2020-11-16 10:02:11 +03:00
if (a < b )
{
2018-03-26 19:03:08 +03:00
do(a);
}
2020-11-16 10:02:11 +03:00
else if (a > b)
{
// curly braces required here, then all branches should also add them
2018-03-26 19:03:08 +03:00
do(b);
do(d);
}
2020-11-16 10:02:11 +03:00
else
{
2018-03-26 19:03:08 +03:00
do(c);
}
```
2014-11-16 18:25:50 +03:00
2020-08-05 18:05:24 +03:00
### g. Brace enclosed initializers
Unlike single-line functions, you must not insert spaces between the brackets and concluded expressions. \
2018-03-26 19:03:08 +03:00
But you must insert a space between the variable name and initializer.
2020-08-05 18:05:24 +03:00
2018-03-26 19:03:08 +03:00
```c++
Class obj {}; // empty
Class obj {expr};
Class obj {expr1, /*...,*/ exprN};
QVariantMap map {{"key1", 5}, {"key2", 10}};
```
2014-11-16 18:25:50 +03:00
2020-08-05 18:05:24 +03:00
## 2. Indentation
2014-11-16 18:25:50 +03:00
4 spaces.
2020-08-05 18:05:24 +03:00
## 3. File encoding and line endings
2014-11-16 18:25:50 +03:00
2015-05-04 02:09:30 +03:00
UTF-8 and Unix-like line ending (LF). Unless some platform specific files need other encodings/line endings.
2014-11-16 18:25:50 +03:00
2020-08-05 18:05:24 +03:00
## 4. Initialization lists
2015-05-04 02:09:30 +03:00
Initialization lists should be vertical. This will allow for more easily readable diffs. The initialization colon should be indented and in its own line along with first argument. The rest of the arguments should be indented too and have the comma prepended.
2020-08-05 18:05:24 +03:00
2014-11-26 16:10:43 +03:00
```c++
myClass::myClass(int a, int b, int c, int d)
2020-11-16 10:02:11 +03:00
: m_a {a}
, m_b {b}
, m_c {c}
, m_d {d}
2014-11-26 16:10:43 +03:00
{
2017-09-04 18:55:37 +03:00
// code
2014-11-26 16:10:43 +03:00
}
```
2020-08-05 18:05:24 +03:00
## 5. Enums
2014-11-26 16:10:43 +03:00
Enums should be vertical. This will allow for more easily readable diffs. The members should be indented.
2020-08-05 18:05:24 +03:00
2014-11-26 16:10:43 +03:00
```c++
2015-11-21 11:25:14 +03:00
enum Days
2014-11-26 16:10:43 +03:00
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
```
2020-08-05 18:05:24 +03:00
## 6. Names
2015-11-21 11:25:14 +03:00
All names should be camelCased.
2020-08-05 18:05:24 +03:00
### a. Type names and namespaces
2015-11-21 11:25:14 +03:00
Type names and namespaces start with Upper case letter (except POD types).
2020-08-05 18:05:24 +03:00
2015-11-21 11:25:14 +03:00
```c++
2016-04-05 22:47:05 +03:00
class ClassName {};
2015-11-21 11:25:14 +03:00
2016-04-05 22:47:05 +03:00
struct StructName {};
2015-11-21 11:25:14 +03:00
2016-04-05 22:47:05 +03:00
enum EnumName {};
2015-11-21 11:25:14 +03:00
typedef QList< ClassName > SomeList;
namespace NamespaceName
{
}
```
2020-08-05 18:05:24 +03:00
### b. Variable names
2015-11-21 11:25:14 +03:00
Variable names start with lower case letter.
2020-08-05 18:05:24 +03:00
2015-11-21 11:25:14 +03:00
```c++
int myVar;
```
2020-08-05 18:05:24 +03:00
### c. Private member variable names
2015-11-21 11:25:14 +03:00
Private member variable names start with lower case letter and should have ```m_``` prefix.
2020-08-05 18:05:24 +03:00
2015-11-21 11:25:14 +03:00
```c++
class MyClass
{
int m_myVar;
}
```
2020-08-05 18:05:24 +03:00
## 7. Header inclusion order
2018-04-10 11:14:06 +03:00
The headers should be placed in the following group order:
2020-08-05 18:05:24 +03:00
1. Module header (in .cpp)
2. C++ Standard Library headers
3. System headers
4. Boost library headers
5. Libtorrent headers
6. Qt headers
7. qBittorrent's own headers, starting from the *base* headers.
The headers should be ordered alphabetically within each group. \
If there are conditionals for the same header group, then put them at the bottom of the respective group. \
2018-05-07 20:31:18 +03:00
If there are conditionals that contain headers from several different header groups, then put them above the "qBittorrent's own headers" group.
2018-04-10 11:14:06 +03:00
One exception is the header containing the library version (for example, QtGlobal), this particular header isn't constrained by the aforementioned order.
2016-04-18 17:52:07 +03:00
Example:
2020-08-05 18:05:24 +03:00
2016-04-18 17:52:07 +03:00
```c++
2018-04-10 11:14:06 +03:00
// file: examplewidget.cpp
2016-04-18 17:52:07 +03:00
2018-04-10 11:14:06 +03:00
// Module header
2016-04-18 17:52:07 +03:00
#include "examplewidget.h"
2018-04-10 11:14:06 +03:00
// exceptions, headers containing version number
#include <boost/version.hpp>
#include <libtorrent/version.hpp>
#include <QtGlobal>
// C++ Standard Library headers
2016-04-18 17:52:07 +03:00
#include <cstdio>
2018-04-10 11:14:06 +03:00
#ifdef Q_OS_WIN // conditional
#include <cmath>
#endif
// System headers
#ifdef Q_OS_WIN
#include <Windows.h>
#endif
// Boost library headers
#include <boost/circular_buffer.hpp>
// Libtorrent headers
#include <libtorrent/session.hpp>
// Qt headers
2016-04-18 17:52:07 +03:00
#include <QString>
#include <QUrl>
2019-09-05 15:11:33 +03:00
#ifdef Q_OS_MACOS // conditional
2018-04-10 11:14:06 +03:00
#include <QFont>
#endif
2018-05-07 20:31:18 +03:00
// conditional that contains headers from several different header groups
2018-04-10 11:14:06 +03:00
#if LIBTORRENT_VERSION_NUM >= 10100
#include <memory>
#include <QElapsedTimer>
#endif
2016-04-18 17:52:07 +03:00
2018-05-07 20:31:18 +03:00
// qBittorrent's own headers
2016-04-18 17:52:07 +03:00
#include "base/bittorrent/infohash.h"
2018-04-10 11:14:06 +03:00
#include "anothermodule.h"
2016-04-18 17:52:07 +03:00
#include "ui_examplewidget.h"
```
2020-08-05 18:05:24 +03:00
## 8. Include guard
2018-01-20 12:10:21 +03:00
`#pragma once` should be used instead of "include guard" in new code:
2020-08-05 18:05:24 +03:00
2018-01-20 12:10:21 +03:00
```c++
// examplewidget.h
#pragma once
#include <QWidget>
class ExampleWidget : public QWidget
{
// (some code omitted)
};
```
2020-08-05 18:05:24 +03:00
## 9. Misc
2014-11-16 18:25:50 +03:00
* Line breaks for long lines with operation:
2020-08-05 18:05:24 +03:00
```c++
a += "b"
+ "c"
+ "d";
```
2014-11-16 18:25:50 +03:00
2016-03-15 20:33:35 +03:00
* **auto** keyword
2020-08-05 18:05:24 +03:00
We allow the use of the **auto** keyword only where it is strictly necessary (for example, to declare a lambda object, etc.), or where it **enhances** the readability of the code. \
Declarations for which one can gather enough information about the object interface (type) from its name or the usage pattern (an iterator or a loop variable are good examples of clear patterns) or the right part of the expression nicely fit here.
2016-03-15 20:33:35 +03:00
2020-08-05 18:05:24 +03:00
When weighing whether to use an auto-typed variable please think about potential reviewers of your code, who will read it as a plain diff (on github.com, for instance). \
Please make sure that such reviewers can understand the code completely and without excessive effort.
2016-03-15 20:33:35 +03:00
2020-08-05 18:05:24 +03:00
Some valid use cases:
2016-03-15 20:33:35 +03:00
2020-08-05 18:05:24 +03:00
* Container iteration and casts:
2016-04-05 22:47:05 +03:00
2020-08-05 18:05:24 +03:00
```c++
template < typename List >
void doSomethingWithList(const List & list)
{
2020-11-16 10:02:11 +03:00
foreach (const auto & item, list)
{
2020-08-05 18:05:24 +03:00
// we don't know item type here so we use 'auto' keyword
// do something with item
}
}
2017-09-04 18:55:37 +03:00
2020-11-16 10:02:11 +03:00
for (auto it = container.begin(), end = container.end(); it != end; ++it)
{
2020-08-05 18:05:24 +03:00
// we don't need to know the exact iterator type,
// because all iterators have the same interface
}
2017-09-04 18:55:37 +03:00
2020-08-05 18:05:24 +03:00
auto spinBox = static_cast< QSpinBox * > (sender());
// we know the variable type based on the right-hand expression
```
* Notice the spaces in the following specific situations:
```c++
// Before and after the assignment and other binary (and ternary) operators there should be a space
// There should not be a space between increment/decrement and its operand
a += 20;
a = (b < = MAX_B ? b : MAX_B);
++a;
--b;
2020-11-16 10:02:11 +03:00
for (int a = 0; a < b ; + + b )
{
2020-08-05 18:05:24 +03:00
}
// Range-based for loop, spaces before and after the colon
2020-11-16 10:02:11 +03:00
for (auto i : container)
{
2020-08-05 18:05:24 +03:00
}
// Derived class, spaces before and after the colon
class Derived : public Base
{
};
```
2017-09-04 18:55:37 +03:00
* Prefer pre-increment, pre-decrement operators
2020-08-05 18:05:24 +03:00
```c++
++i, --j; // Yes
i++, j--; // No
```
2014-11-16 18:25:50 +03:00
* private/public/protected must not be indented
* Preprocessor commands must go at line start
* Method definitions aren't allowed in header files
2020-08-05 18:05:24 +03:00
## 10. Git commit message
2018-02-07 18:09:48 +03:00
1. Limit the subject line to 50 characters. Subject should contain only the very essence of the changes (you should avoid extra details and internals)
2. Separate subject from body with a blank line
3. Capitalize the subject line
4. Do not end the subject line with a period
5. Use the imperative mood in the subject line (it's like you're ordering the program to do something (e.g. "Don't create temporary substrings")
6. Wrap the body at 72 characters
7. Use the body to explain what and why vs. how
8. If commit fixes a reported issue, mention it in the message body (e.g. `Closes #4134.` )
2020-08-05 18:05:24 +03:00
## 11. Not covered above
If something isn't covered above, just follow the same style the file you are editing has. \
2018-03-26 19:03:08 +03:00
*This guide is not exhaustive and the style for a particular piece of code not specified here will be determined by project members on code review.*