1 All new code **must** follow the following coding guidelines.
2 If you make changes in a file that still uses another coding style, make sure that you follow these guidelines for your changes.
3 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.
5 **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.
6 **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.
7 **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.
11 * [1. New lines & curly braces](#1-new-lines--curly-braces)
12 * [a. Function blocks, class/struct definitions, namespaces](#a-function-blocks-classstruct-definitions-namespaces)
13 * [b. Other code blocks](#b-other-code-blocks)
14 * [c. Blocks in switch's case labels](#c-blocks-in-switchs-case-labels)
15 * [d. If-else statements](#d-if-else-statements)
16 * [e. Single statement if blocks](#e-single-statement-if-blocks)
17 * [f. Acceptable conditions to omit braces](#f-acceptable-conditions-to-omit-braces)
18 * [g. Brace enclosed initializers](#g-brace-enclosed-initializers)
19 * [2. Indentation](#2-indentation)
20 * [3. File encoding and line endings](#3-file-encoding-and-line-endings)
21 * [4. Initialization lists](#4-initialization-lists)
22 * [5. Enums](#5-enums)
23 * [6. Names](#6-names)
24 * [a. Type names and namespaces](#a-type-names-and-namespaces)
25 * [b. Variable names](#b-variable-names)
26 * [c. Private member variable names](#c-private-member-variable-names)
27 * [7. Header inclusion order](#7-header-inclusion-order)
28 * [8. Include guard](#8-include-guard)
30 * [10. Git commit message](#10-git-commit-message)
31 * [11. Not covered above](#11-not-covered-above)
34 ### 1. New lines & curly braces ###
36 #### a. Function blocks, class/struct definitions, namespaces ####
43 void myFunction() {} // empty body
45 MyClass::MyClass(int *parent)
51 int MyClass::myMethod(int a)
74 [](int arg1, int arg2) -> bool { return arg1 < arg2; }
82 #### b. Other code blocks ####
88 for (int a = 0; a < b; ++b) {
102 #### c. Blocks in switch's case labels ####
106 // declare local variables
111 // declare local variables
120 #### d. If-else statements ####
121 The `else if`/`else` must be on their own lines:
126 else if (condition) {
134 #### e. Single statement if blocks ####
135 Most single statement if blocks should look like this:
141 One acceptable exception to this can be `return`, `break` or `continue` statements,
142 provided that the test condition isn't very long and its body statement occupies only one line.
143 However you can still choose to use the first rule.
153 #### f. Acceptable conditions to omit braces ####
154 When the conditional statement in `if`/`else` has only one line and its body occupy only one line,
155 this also applies to loops statements.
156 Notice that for a series of `if - else` branches, if one branch needs braces then all branches must add braces.
158 if (a < b) // conditional statement
171 else if (a > b) { // curly braces required here, then all branches should also add them
180 #### g. Brace enclosed initializers ####
181 Unlike single-line functions, you must not insert spaces between the brackets and concluded expressions.<br/>
182 But you must insert a space between the variable name and initializer.
184 Class obj {}; // empty
186 Class obj {expr1, /*...,*/ exprN};
187 QVariantMap map {{"key1", 5}, {"key2", 10}};
190 ### 2. Indentation ###
193 ### 3. File encoding and line endings ###
195 UTF-8 and Unix-like line ending (LF). Unless some platform specific files need other encodings/line endings.
197 ### 4. Initialization lists ###
198 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.
200 myClass::myClass(int a, int b, int c, int d)
211 Enums should be vertical. This will allow for more easily readable diffs. The members should be indented.
226 All names should be camelCased.
228 #### a. Type names and namespaces ####
229 Type names and namespaces start with Upper case letter (except POD types).
233 struct StructName {};
237 typedef QList<ClassName> SomeList;
239 namespace NamespaceName
244 #### b. Variable names ####
245 Variable names start with lower case letter.
250 #### c. Private member variable names ####
251 Private member variable names start with lower case letter and should have ```m_``` prefix.
259 ### 7. Header inclusion order ###
260 The headers should be placed in the following group order:
261 1. Module header (in .cpp)
262 2. C++ Standard Library headers
264 4. Boost library headers
265 5. Libtorrent headers
267 7. qBittorrent's own headers, starting from the *base* headers.
269 The headers should be ordered alphabetically within each group.
270 If there are conditionals for the same header group, then put them at the bottom of the respective group.
271 If there are conditionals that contain headers from several different header groups, then put them above the "qBittorrent's own headers" group.
273 One exception is the header containing the library version (for example, QtGlobal), this particular header isn't constrained by the aforementioned order.
277 // file: examplewidget.cpp
280 #include "examplewidget.h"
282 // exceptions, headers containing version number
283 #include <boost/version.hpp>
284 #include <libtorrent/version.hpp>
287 // C++ Standard Library headers
290 #ifdef Q_OS_WIN // conditional
299 // Boost library headers
300 #include <boost/circular_buffer.hpp>
302 // Libtorrent headers
303 #include <libtorrent/session.hpp>
309 #ifdef Q_OS_MAC // conditional
313 // conditional that contains headers from several different header groups
314 #if LIBTORRENT_VERSION_NUM >= 10100
316 #include <QElapsedTimer>
319 // qBittorrent's own headers
320 #include "base/bittorrent/infohash.h"
321 #include "anothermodule.h"
322 #include "ui_examplewidget.h"
325 ### 8. Include guard ###
326 `#pragma once` should be used instead of "include guard" in new code:
334 class ExampleWidget : public QWidget
336 // (some code omitted)
343 * Line breaks for long lines with operation:
353 We allow the use of the **auto** keyword only where it is strictly necessary
354 (for example, to declare a lambda object, etc.), or where it **enhances** the readability of the code.
355 Declarations for which one can gather enough information about the object interface (type) from its name
356 or the usage pattern (an iterator or a loop variable are good examples of clear patterns)
357 or the right part of the expression nicely fit here.<br/>
359 When weighing whether to use an auto-typed variable please think about potential reviewers of your code,
360 who will read it as a plain diff (on github.com, for instance). Please make sure that such reviewers can
361 understand the code completely and without excessive effort.<br/>
363 Some valid use cases:
365 template <typename List>
366 void doSomethingWithList(const List &list)
368 foreach (const auto &item, list) {
369 // we don't know item type here so we use 'auto' keyword
370 // do something with item
374 for (auto it = container.begin(), end = container.end(); it != end; ++it) {
375 // we don't need to know the exact iterator type,
376 // because all iterators have the same interface
379 auto spinBox = static_cast<QSpinBox*>(sender());
380 // we know the variable type based on the right-hand expression
383 * Notice the spaces in the following specific situations:
385 // Before and after the assignment and other binary (and ternary) operators there should be a space
386 // There should not be a space between increment/decrement and its operand
388 a = (b <= MAX_B ? b : MAX_B);
392 for (int a = 0; a < b; ++b) {
395 // Range-based for loop, spaces before and after the colon
396 for (auto i : container) {
399 // Derived class, spaces before and after the colon
400 class Derived : public Base
405 * Prefer pre-increment, pre-decrement operators
411 * private/public/protected must not be indented
413 * Preprocessor commands must go at line start
415 * Method definitions aren't allowed in header files
417 ### 10. Git commit message ###
418 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)
419 2. Separate subject from body with a blank line
420 3. Capitalize the subject line
421 4. Do not end the subject line with a period
422 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")
423 6. Wrap the body at 72 characters
424 7. Use the body to explain what and why vs. how
425 8. If commit fixes a reported issue, mention it in the message body (e.g. `Closes #4134.`)
427 ### 11. Not covered above ###
428 If something isn't covered above, just follow the same style the file you are editing has.
429 *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.*