Browse Source

Merge pull request #7385 from Chocobo1/coding_style

Update coding guidelines (proposal)
adaptive-webui-19844
sledgehammer999 7 years ago committed by GitHub
parent
commit
915ec81f83
  1. 70
      CODING_GUIDELINES.md

70
CODING_GUIDELINES.md

@ -9,7 +9,7 @@ If you make changes in a file that still uses another coding style, make sure th
```c++ ```c++
int myFunction(int a) int myFunction(int a)
{ {
//code // code
} }
void myFunction() {} // empty body void myFunction() {} // empty body
@ -17,29 +17,29 @@ void myFunction() {} // empty body
MyClass::MyClass(int *parent) MyClass::MyClass(int *parent)
: m_parent(parent) : m_parent(parent)
{ {
//initialize // initialize
} }
int MyClass::myMethod(int a) int MyClass::myMethod(int a)
{ {
//code // code
} }
class MyOtherClass class MyOtherClass
{ {
public: public:
//code // code
protected: protected:
//code // code
private: private:
//code // code
}; };
namespace Name namespace Name
{ {
//code // code
} }
// Lambdas // Lambdas
@ -54,20 +54,20 @@ namespace Name
#### b. Other code blocks #### #### b. Other code blocks ####
```c++ ```c++
if (condition) { if (condition) {
//code // code
} }
for (int a = 0; a < b; ++b) { for (int a = 0; a < b; ++b) {
//code // code
} }
switch (a) { switch (a) {
case 1: case 1:
//blah // blah
case 2: case 2:
//blah // blah
default: default:
//blah // blah
} }
``` ```
@ -75,17 +75,17 @@ default:
```c++ ```c++
switch (var) { switch (var) {
case 1: { case 1: {
//declare local variables // declare local variables
//code // code
} }
break; break;
case 2: { case 2: {
//declare local variables // declare local variables
//code // code
} }
break; break;
default: default:
//code // code
} }
``` ```
@ -103,13 +103,13 @@ QVariantMap map {{"key1", 5}, {"key2", 10}};
#### a. Multiple tests #### #### a. Multiple tests ####
```c++ ```c++
if (condition) { if (condition) {
//code // code
} }
else if (condition) { else if (condition) {
//code // code
} }
else { else {
//code // code
} }
``` ```
The `else if`/`else` must be on their own lines. The `else if`/`else` must be on their own lines.
@ -154,7 +154,7 @@ myClass::myClass(int a, int b, int c, int d)
, m_c(c) , m_c(c)
, m_d(d) , m_d(d)
{ {
//code // code
} }
``` ```
@ -231,8 +231,8 @@ Example:
#include <libtorrent/version.hpp> #include <libtorrent/version.hpp>
#include "base/bittorrent/session.h"
#include "base/bittorrent/infohash.h" #include "base/bittorrent/infohash.h"
#include "base/bittorrent/session.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
#include "base/utils/misc.h" #include "base/utils/misc.h"
#include "base/utils/string.h" #include "base/utils/string.h"
@ -282,20 +282,32 @@ auto spinBox = static_cast<QSpinBox*>(sender());
// we know the variable type based on the right-hand expression // we know the variable type based on the right-hand expression
``` ```
* Space around operations eg `a = b + c` or `a=b+c`: * Notice the spaces in the following specific situations:
Before and after the assignment and other binary (and ternary) operators there should be a space.<br/>
There should not be a space between increment/decrement and its operand.<br/>
Some valid use cases:
```c++ ```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 += 20;
a = (b <= MAX_B ? b : MAX_B); a = (b <= MAX_B ? b : MAX_B);
++a; ++a;
b--; --b;
for (int a = 0; a < b; ++b) { for (int a = 0; a < b; ++b) {
// code
} }
// Range-based for loop, spaces before and after the colon
for (auto i : container) {
}
// Derived class, spaces before and after the colon
class Derived : public Base
{
};
```
* Prefer pre-increment, pre-decrement operators
```c++
++i, --j; // Yes
i++, j--; // No
``` ```
* private/public/protected must not be indented * private/public/protected must not be indented

Loading…
Cancel
Save