Skip to content

Commit f89819b

Browse files
committed
Fix coding style rules
Closes qbittorrent#5075
1 parent 364c22b commit f89819b

1 file changed

Lines changed: 32 additions & 47 deletions

File tree

CODING_GUIDELINES.md

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ class MyOtherClass
2929
{
3030
public:
3131
//code
32+
3233
protected:
3334
//code
35+
3436
private:
3537
//code
3638
};
@@ -87,10 +89,14 @@ default:
8789
}
8890
```
8991

90-
#### d. single-line blocks (lambdas, initializer lists etc.) ####
92+
#### d. Brace enclosed initializers ####
93+
Unlike single-line functions, you must not insert spaces between the brackets and concluded expressions.<br/>
94+
But you must insert a space between the variable name and initializer.
9195
```c++
92-
{} // empty - space before {
93-
{ body } // spaces around { and before }
96+
Class obj {}; // empty
97+
Class obj {expr};
98+
Class obj {expr1, /*...,*/ exprN};
99+
QVariantMap map {{"key1", 5}, {"key2", 10}};
94100
```
95101
96102
### 2. If blocks ###
@@ -173,11 +179,11 @@ All names should be camelCased.
173179
#### a. Type names and namespaces ####
174180
Type names and namespaces start with Upper case letter (except POD types).
175181
```c++
176-
class ClassName {}
182+
class ClassName {};
177183

178-
struct StructName {}
184+
struct StructName {};
179185

180-
enum EnumName {}
186+
enum EnumName {};
181187

182188
typedef QList<ClassName> SomeList;
183189

@@ -211,47 +217,18 @@ a += "b"
211217
+ "d";
212218
```
213219

214-
* Initializers
215-
216-
We allow brace enclosed initializers only for aggregates and arrays/containers.<br />
217-
Brace enclosed initializer MUST be used with equality sign if it follows the variable declaration.<br />
218-
Brace enclosed initializer MUST be additionally enclosed in parentheses if it is used in constructor initialization list.<br />
219-
Some valid use cases:
220-
```c++
221-
// aggregate
222-
Person john = { "John", "Smith", 21 };
223-
Person *john = new Person { "John", "Smith", 21 };
224-
225-
// array
226-
int array[] = { 1, 2, 3, 4 };
227-
228-
// container
229-
QHash<QString, QString> map = {
230-
{ "key1", "value1" },
231-
{ "key2", "value2" }
232-
);
233-
234-
// member array
235-
SomeClass::SomeClass(BaseClass *parent)
236-
: BaseClass(parent)
237-
, m_someArrayMember({ 1, 2, 3, 4 })
238-
{
239-
}
240-
241-
// return from function
242-
Person getPersonByName(const QString &name)
243-
{
244-
// do something
245-
return { name, surname, age };
246-
}
247-
248-
// function argument
249-
doSomething({ name, surname, age }, someOtherArg);
250-
```
251-
252220
* **auto** keyword
253221

254-
We allow the use of the **auto** keyword only where it doesn't break the readability of the code (i.e. either we can gather enough information about the type from the right part of the expression, or we do not need to know the exact type), or where it is strictly necessary (for example, to compute the type of a lambda, etc.).<br />
222+
We allow the use of the **auto** keyword only where it is strictly necessary
223+
(for example, to declare a lambda object, etc.), or where it **enhances** the readability of the code.
224+
Declarations for which one can gather enough information about the object interface (type) from its name
225+
or the usage pattern (an iterator or a loop variable are good examples of clear patterns)
226+
or the right part of the expression nicely fit here.<br/>
227+
<br/>
228+
When weighing whether to use an auto-typed variable please think about potential reviewers of your code,
229+
who will read it as a plain diff (on github.com, for instance). Please make sure that such reviewers can
230+
understand the code completely and without excessive effort.<br/>
231+
<br/>
255232
Some valid use cases:
256233
```c++
257234
template <typename List>
@@ -274,9 +251,17 @@ auto spinBox = static_cast<QSpinBox*>(sender());
274251
275252
* Space around operations eg `a = b + c` or `a=b+c`:
276253
277-
Before and after the assignment there should be a space. One exception could be: for loops.
254+
Before and after the assignment and other binary (and ternary) operators there should be a space.<br/>
255+
There should not be a space between increment/decrement and its operand.<br/>
256+
Some valid use cases:
278257
```c++
279-
for (int a=0; a<b; ++b) {
258+
a += 20;
259+
a = (b <= MAX_B ? b : MAX_B);
260+
++a;
261+
b--;
262+
263+
for (int a = 0; a < b; ++b) {
264+
// code
280265
}
281266
```
282267

0 commit comments

Comments
 (0)