@@ -222,6 +222,33 @@ For C programs:
222
222
- Double negation is often harder to understand than no negation
223
223
at all.
224
224
225
+ - There are two schools of thought when it comes to comparison,
226
+ especially inside a loop. Some people prefer to have the less stable
227
+ value on the left hand side and the more stable value on the right hand
228
+ side, e.g. if you have a loop that counts variable i down to the
229
+ lower bound,
230
+
231
+ while (i > lower_bound) {
232
+ do something;
233
+ i--;
234
+ }
235
+
236
+ Other people prefer to have the textual order of values match the
237
+ actual order of values in their comparison, so that they can
238
+ mentally draw a number line from left to right and place these
239
+ values in order, i.e.
240
+
241
+ while (lower_bound < i) {
242
+ do something;
243
+ i--;
244
+ }
245
+
246
+ Both are valid, and we use both. However, the more "stable" the
247
+ stable side becomes, the more we tend to prefer the former
248
+ (comparison with a constant, "i > 0", is an extreme example).
249
+ Just do not mix styles in the same part of the code and mimic
250
+ existing styles in the neighbourhood.
251
+
225
252
- Some clever tricks, like using the !! operator with arithmetic
226
253
constructs, can be extremely confusing to others. Avoid them,
227
254
unless there is a compelling reason to use them.
0 commit comments