Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add member initializer lists and ternary operators to coding standards #7524

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions doc/CodingStandard.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,43 @@ if ((logLevelValue >= 0) && (logLevelValue <= 4)) {
* C order of operation rules are complicated; avoid confusion and mistakes by using parentheses to make the order explicit.
* Combining assignment and tests violates the "Try to do only one thing on each line of code" principle.

### Constructor member-initializer lists
* Each member should be initialized on a separate line.
* Each member initialization should be preceded by the colon or comma and a space.
* Each member initialization should be indented at least one tab.
* The `:` and `,` should be aligned.

Correct
```c
MyClass::MyClass(uint32_t foo, uint32_t bar, uint32_t baz)
: foo(foo)
, bar(bar)
, baz(baz)
{
...
}
```
Wrong
```c
MyClass::MyClass(uint32_t foo, uint32_t bar, uint32_t baz) : foo(foo), bar(bar), baz(baz)
{
...
}
```
Wrong
```c
MyClass::MyClass(uint32_t foo, uint32_t bar, uint32_t baz) :
foo(foo),
bar(bar),
baz(baz)
{
...
}
```
#### Rationale
* Requires only a single-line change when adding and removing members.
* Reduces overly long constructor declaration lines.
* Improves readability.

### Parameter passing

Expand Down Expand Up @@ -841,6 +878,8 @@ helper (0, count);
* The preferred idiom for infinite loops is ```for (;;)```.

### Operators

#### Increment and decrement
* The increment and decrement operators (++ and --) are discouraged. Use += or -= instead.

Correct
Expand All @@ -867,6 +906,7 @@ for (index = 0; index < size; index++) {
}
```

#### Order of operations
* Use parentheses to specify order of operation, unless the order is completely unambiguous.
* If in doubt, use more parentheses.

Expand All @@ -884,6 +924,39 @@ if (0 == a && 0 != b) { ...
end = (uint8_t*)start + size;
```

#### Continuing expessions containing binary operators
* When folding overly long lines, begin the next line with the binary operator.
* Ensure the second and subsequent lines are consistently indented.

Correct
```c
uint32_t result = longVariableNameOrCalculation1
+ longVariableNameOrCalculation2
+ longVariableNameOrCalculation3;
```
Wrong
```c
uint32_t result = longVariableNameOrCalculation1 +
longVariableNameOrCalculation2 +
longVariableNameOrCalculation3;
```

#### Conditional (ternary) operator
* When folding overly long lines, align the `?` and the `:`.

Correct
```c
uint32_t result = someCondition
? trueCase
: falseCase;
```
Wrong
```c
uint32_t result = someCondition ?
trueCase :
falseCase;
```

### Enums
* Enum definitions exposed outside of a single file must include a dummy member with a large value to force the enum to be represented using at least 4 bytes. (If there is doubt about the visibility of the enum definition, include the dummy member.)
* The dummy member must be the last member of the enum. It must be named with the suffix EnsureWideEnum, and documented with the comment /* force 4-byte enum */, as shown in the following example.
Expand Down