Skip to content

Commit

Permalink
Improve coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Nov 17, 2020
1 parent acad35c commit c41df9f
Show file tree
Hide file tree
Showing 147 changed files with 4,454 additions and 2,227 deletions.
66 changes: 44 additions & 22 deletions CODING_GUIDELINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int myFunction(int a)
void myFunction() {} // empty body

MyClass::MyClass(int *parent)
: m_parent(parent)
: m_parent {parent}
{
// initialize
}
Expand Down Expand Up @@ -86,34 +86,44 @@ namespace Name
### b. Other code blocks
```c++
if (condition) {
if (condition)
{
// code
}
for (int a = 0; a < b; ++b) {
for (int a = 0; a < b; ++b)
{
// code
}
switch (a) {
switch (a)
{
case 1:
// blah
case 2:
// blah
default:
// blah
}
{
// code
}
```

### c. Blocks in switch's case labels

```c++
switch (var) {
case 1: {
switch (var)
{
case 1:
{
// declare local variables
// code
}
break;
case 2: {
case 2:
{
// declare local variables
// code
}
Expand All @@ -128,13 +138,16 @@ default:
The `else if`/`else` must be on their own lines:

```c++
if (condition) {
if (condition)
{
// code
}
else if (condition) {
else if (condition)
{
// code
}
else {
else
{
// code
}
```
Expand All @@ -155,7 +168,8 @@ However you can still choose to use the first rule.
```c++
if (a > 0) return;

while (p) {
while (p)
{
// ...
if (!b) continue;
}
Expand All @@ -178,14 +192,18 @@ else if (a > b)
else
do(c);

if (a < b) {
if (a < b)
{
do(a);
}
else if (a > b) { // curly braces required here, then all branches should also add them
else if (a > b)
{
// curly braces required here, then all branches should also add them
do(b);
do(d);
}
else {
else
{
do(c);
}
```
Expand Down Expand Up @@ -216,10 +234,10 @@ Initialization lists should be vertical. This will allow for more easily readabl
```c++
myClass::myClass(int a, int b, int c, int d)
: m_a(a)
, m_b(b)
, m_c(c)
, m_d(d)
: m_a {a}
, m_b {b}
, m_c {c}
, m_d {d}
{
// code
}
Expand Down Expand Up @@ -396,13 +414,15 @@ class ExampleWidget : public QWidget
template <typename List>
void doSomethingWithList(const List &list)
{
foreach (const auto &item, list) {
foreach (const auto &item, list)
{
// we don't know item type here so we use 'auto' keyword
// do something with item
}
}

for (auto it = container.begin(), end = container.end(); it != end; ++it) {
for (auto it = container.begin(), end = container.end(); it != end; ++it)
{
// we don't need to know the exact iterator type,
// because all iterators have the same interface
}
Expand All @@ -420,10 +440,12 @@ class ExampleWidget : public QWidget
a = (b <= MAX_B ? b : MAX_B);
++a;
--b;
for (int a = 0; a < b; ++b) {
for (int a = 0; a < b; ++b)
{
}
// Range-based for loop, spaces before and after the colon
for (auto i : container) {
for (auto i : container)
{
}
// Derived class, spaces before and after the colon
class Derived : public Base
Expand Down
Loading

0 comments on commit c41df9f

Please sign in to comment.