CodingGuidelines: on comparison

There are arguments for writing a conditional as "a < b" rather than "b > a", or vice versa. Let's give guidance on which we prefer. See http://thread.gmane.org/gmane.comp.version-control.git/3903/focus=4126 for the original discussion. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Apr 30, 2014 at 14:26 UTC 5db9ab82b94fab16e69b0228aaf1e972520bd04a
1 file changed +27
Documentation/CodingGuidelines
+27
@@ -222,6 +222,33 @@ For C programs:
222 - Double negation is often harder to understand than no negation
223 at all.
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 +
252 - Some clever tricks, like using the !! operator with arithmetic
253 constructs, can be extremely confusing to others. Avoid them,
254 unless there is a compelling reason to use them.