3 All new code **must** follow the following coding guidelines.
5 If you make changes in a file that still uses another coding style, make sure that you follow these guidelines for your changes. \
6 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.
8 **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. \
9 **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. \
10 **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.
14 * [1. New lines & curly braces](#1-new-lines--curly-braces)
15 * [a. Function blocks, class/struct definitions, namespaces](#a-function-blocks-classstruct-definitions-namespaces)
16 * [b. Other code blocks](#b-other-code-blocks)
17 * [c. Blocks in switch's case labels](#c-blocks-in-switchs-case-labels)
18 * [d. If-else statements](#d-if-else-statements)
19 * [e. Single statement if blocks](#e-single-statement-if-blocks)
20 * [f. Acceptable conditions to omit braces](#f-acceptable-conditions-to-omit-braces)
21 * [g. Brace enclosed initializers](#g-brace-enclosed-initializers)
22 * [2. Indentation](#2-indentation)
23 * [3. File encoding and line endings](#3-file-encoding-and-line-endings)
24 * [4. Initialization lists](#4-initialization-lists)
25 * [5. Enums](#5-enums)
26 * [6. Names](#6-names)
27 * [a. Type names and namespaces](#a-type-names-and-namespaces)
28 * [b. Variable names](#b-variable-names)
29 * [c. Private member variable names](#c-private-member-variable-names)
30 * [7. Header inclusion order](#7-header-inclusion-order)
31 * [8. Include guard](#8-include-guard)
33 * [10. Git commit message](#10-git-commit-message)
34 * [11. Not covered above](#11-not-covered-above)
38 ## 1. New lines & curly braces
40 ### a. Function blocks, class/struct definitions, namespaces
48 void myFunction() {} // empty body
50 MyClass::MyClass(int *parent)
56 int MyClass::myMethod(int a)
79 [](int arg1, int arg2) -> bool { return arg1 < arg2; }
87 ### b. Other code blocks
95 for (int a = 0; a < b; ++b)
115 ### c. Blocks in switch's case labels
122 // declare local variables
128 // declare local variables
137 ### d. If-else statements
139 The `else if`/`else` must be on their own lines:
156 ### e. Single statement if blocks
158 Most single statement if blocks should look like this:
165 One acceptable exception to this can be `return`, `break` or `continue` statements,
166 provided that the test condition isn't very long and its body statement occupies only one line.
167 However you can still choose to use the first rule.
179 ### f. Acceptable conditions to omit braces
181 When the conditional statement in `if`/`else` has only one line and its body occupy only one line,
182 this also applies to loops statements. \
183 Notice that for a series of `if - else` branches, if one branch needs braces then all branches must add braces.
186 if (a < b) // conditional statement
202 // curly braces required here, then all branches should also add them
212 ### g. Brace enclosed initializers
214 Unlike single-line functions, you must not insert spaces between the brackets and concluded expressions. \
215 But you must insert a space between the variable name and initializer.
218 Class obj {}; // empty
220 Class obj {expr1, /*...,*/ exprN};
221 QVariantMap map {{"key1", 5}, {"key2", 10}};
228 ## 3. File encoding and line endings
230 UTF-8 and Unix-like line ending (LF). Unless some platform specific files need other encodings/line endings.
232 ## 4. Initialization lists
234 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.
237 myClass::myClass(int a, int b, int c, int d)
249 Enums should be vertical. This will allow for more easily readable diffs. The members should be indented.
266 All names should be camelCased.
268 ### a. Type names and namespaces
270 Type names and namespaces start with Upper case letter (except POD types).
275 struct StructName {};
279 typedef QList<ClassName> SomeList;
281 namespace NamespaceName
286 ### b. Variable names
288 Variable names start with lower case letter.
294 ### c. Private member variable names
296 Private member variable names start with lower case letter and should have ```m_``` prefix.
305 ## 7. Header inclusion order
307 The headers should be placed in the following group order:
309 1. Module header (in .cpp)
310 2. C++ Standard Library headers
312 4. Boost library headers
313 5. Libtorrent headers
315 7. qBittorrent's own headers, starting from the *base* headers.
317 The headers should be ordered alphabetically within each group. \
318 If there are conditionals for the same header group, then put them at the bottom of the respective group. \
319 If there are conditionals that contain headers from several different header groups, then put them above the "qBittorrent's own headers" group.
321 One exception is the header containing the library version (for example, QtGlobal), this particular header isn't constrained by the aforementioned order.
326 // file: examplewidget.cpp
329 #include "examplewidget.h"
331 // exceptions, headers containing version number
332 #include <boost/version.hpp>
333 #include <libtorrent/version.hpp>
336 // C++ Standard Library headers
339 #ifdef Q_OS_WIN // conditional
348 // Boost library headers
349 #include <boost/circular_buffer.hpp>
351 // Libtorrent headers
352 #include <libtorrent/session.hpp>
358 #ifdef Q_OS_MACOS // conditional
362 // conditional that contains headers from several different header groups
363 #if LIBTORRENT_VERSION_NUM >= 10100
365 #include <QElapsedTimer>
368 // qBittorrent's own headers
369 #include "base/bittorrent/infohash.h"
370 #include "anothermodule.h"
371 #include "ui_examplewidget.h"
376 `#pragma once` must be used instead of a "classic include guard":
385 class ExampleWidget : public QWidget
387 // (some code omitted)
394 * Line breaks for long lines with operation:
404 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. \
405 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.
407 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). \
408 Please make sure that such reviewers can understand the code completely and without excessive effort.
410 Some valid use cases:
412 * Container iteration and casts:
415 template <typename List>
416 void doSomethingWithList(const List &list)
418 foreach (const auto &item, list)
420 // we don't know item type here so we use 'auto' keyword
421 // do something with item
425 for (auto it = container.begin(), end = container.end(); it != end; ++it)
427 // we don't need to know the exact iterator type,
428 // because all iterators have the same interface
431 auto spinBox = static_cast<QSpinBox*>(sender());
432 // we know the variable type based on the right-hand expression
435 * Notice the spaces in the following specific situations:
438 // Before and after the assignment and other binary (and ternary) operators there should be a space
439 // There should not be a space between increment/decrement and its operand
441 a = (b <= MAX_B ? b : MAX_B);
444 for (int a = 0; a < b; ++b)
447 // Range-based for loop, spaces before and after the colon
448 for (auto i : container)
451 // Derived class, spaces before and after the colon
452 class Derived : public Base
457 * Prefer pre-increment, pre-decrement operators
464 * private/public/protected must not be indented
466 * Preprocessor commands must go at line start
468 * Method definitions aren't allowed in header files
470 ## 10. Git commit message
472 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)
473 2. Separate subject from body with a blank line
474 3. Capitalize the subject line
475 4. Do not end the subject line with a period
476 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")
477 6. Wrap the body at 72 characters
478 7. Use the body to explain what and why vs. how
479 8. If commit fixes a reported issue, mention it in the message body (e.g. `Closes #4134.`)
481 ## 11. Not covered above
483 If something isn't covered above, just follow the same style the file you are editing has. \
484 *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.*