Skip to content

Commit

Permalink
Add member initializer lists and ternary operators to coding standards
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan Henderson <[email protected]>
  • Loading branch information
ThanHenderson committed Nov 4, 2024
1 parent 59c0eac commit b68daa3
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions doc/CodingStandard.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,37 @@ 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(uint32_t foo, uint32_t bar, uint32_t baz)
: foo(foo)
, bar(bar)
, baz(baz)
{ ... }
```
Wrong
```c
MyClass(uint32_t foo, uint32_t bar, uint32_t baz) : foo(foo), bar(bar), baz(baz)
{ ... }
```
Wrong
```c
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 +872,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 +900,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 +918,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 operators are aligned.

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

0 comments on commit b68daa3

Please sign in to comment.