You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const auto res = std::find_if(prefixes, [&](const auto& spec) {
110
+
const auto res = std::find_if(prefixes.begin(), prefixes.end(), [&](const auto& spec) {
111
111
return spec.factor <= size;
112
112
});
113
113
@@ -147,6 +147,38 @@ Can also apply to `switch` statements.
147
147
148
148
Complicated `if else` code might benefit from converting to a state machine.
149
149
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
+
constint 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
+
```
150
182
151
183
## Const correctness
152
184
@@ -184,7 +216,7 @@ Required information can be injected via constructor or method parameters.
184
216
185
217
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.
186
218
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.
188
220
Example:
189
221
```c++
190
222
// 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.
424
456
425
457
Instead of this:
426
458
```cpp
427
-
displayLines(25);
459
+
displayLines(80);
428
460
```
429
461
430
462
Do the following instead:
431
463
```c++
432
-
constexpr auto sandardScreenLength {25};
433
-
displayLines(sandardScreenLength);
464
+
constexpr auto standardScreenLength {80};
465
+
displayLines(standardScreenLength);
434
466
```
435
467
436
468
## Good naming
@@ -493,8 +525,7 @@ Change requires finding every usage (difficult) and replicating the change (erro
493
525
494
526
**Repetition is entirely avoidable.**
495
527
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.
498
529
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.
499
530
500
531
## Static
@@ -504,24 +535,6 @@ initialisation in lambda capture.
504
535
505
536
`static` functions _may_ be better moved out of class into a named namespace or some utility library/file.
506
537
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
0 commit comments