Skip to content

Commit a852fe9

Browse files
committed
C++ Practices: Minor fixes
1 parent 7804717 commit a852fe9

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

bestpractices/c++practices.md

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static constexpr std::array<PrefixSpec, 7> sortedPrefixes {
107107
{'k', 1ULL << 10}, // 1 << 10 = 2^10 = 1024
108108
{'\0', 0}}};
109109

110-
const auto res = std::find_if(prefixes, [&](const auto& spec) {
110+
const auto res = std::find_if(prefixes.begin(), prefixes.end(), [&](const auto& spec) {
111111
return spec.factor <= size;
112112
});
113113

@@ -147,6 +147,38 @@ Can also apply to `switch` statements.
147147

148148
Complicated `if else` code might benefit from converting to a state machine.
149149

150+
### Ternary operator
151+
You can reduce six lines:
152+
```cpp
153+
int r; // can’t be const
154+
155+
if (x == 2) {
156+
r = 2;
157+
} else {
158+
r = 3;
159+
}
160+
```
161+
to one, with a single assignment, no curly braces, no repetition, and
162+
const option:
163+
```cpp
164+
const int r = x == 2 ? 2 : 3;
165+
```
166+
This works especially great for simplifying return statements.
167+
168+
If you have more than one ternary it might be worth to extract function that encapsulates the conditional logic:
169+
```
170+
auto someConditionalLogic = []() {
171+
if (condition1) {
172+
return 1;
173+
} else if (condition2) {
174+
return 2;
175+
} else {
176+
return 3;
177+
}
178+
}
179+
180+
const int r = someConditionalLogic();
181+
```
150182

151183
## Const correctness
152184

@@ -184,7 +216,7 @@ Required information can be injected via constructor or method parameters.
184216
185217
If it is necessary to introduce external code (e.g. a service object), do so by passing an interface, helper function or similar to avoid coupling.
186218
187-
Even in complex cases where singeltons are used we can avoid hard coupling and make unit test a breeze.
219+
Even in complex cases where singletons are used we can avoid hard coupling and make unit test a breeze.
188220
Example:
189221
```c++
190222
// For this example the implementation is included in the class definition
@@ -424,13 +456,13 @@ To document what the magic literal is, use a suitable named constant.
424456

425457
Instead of this:
426458
```cpp
427-
displayLines(25);
459+
displayLines(80);
428460
```
429461
430462
Do the following instead:
431463
```c++
432-
constexpr auto sandardScreenLength {25};
433-
displayLines(sandardScreenLength);
464+
constexpr auto standardScreenLength {80};
465+
displayLines(standardScreenLength);
434466
```
435467

436468
## Good naming
@@ -493,8 +525,7 @@ Change requires finding every usage (difficult) and replicating the change (erro
493525
494526
**Repetition is entirely avoidable.**
495527
496-
The variant part (the bit that is different between usages) of repeating
497-
code is often just one or two items in a long chain of statement.
528+
The variant part (the bit that is different between usages) of repeating code is often just one or two items in a long chain of statement.
498529
The variant parts can be extracted and passed as parameters to a function or lambda executing the common body. A sequence of repeated code likely indicates the underlying data is actually a set and hence should be defined in a container and dealt with accordingly.
499530
500531
## Static
@@ -504,24 +535,6 @@ initialisation in lambda capture.
504535
505536
`static` functions _may_ be better moved out of class into a named namespace or some utility library/file.
506537
507-
## Ternary operator
508-
509-
Reduce six lines:
510-
```cpp
511-
int r; // can’t be const
512-
513-
if (x == 2) {
514-
r = 2;
515-
} else {
516-
r = 3;
517-
}
518-
```
519-
to one, with a single assignment, no curly braces, no repetition, and
520-
const option:
521-
```cpp
522-
const int r = x == 2 ? 2 : 3;
523-
```
524-
Also great for simplifying return statements.
525538
526539
## Automated testing
527540

0 commit comments

Comments
 (0)