Skip to content

Commit 5db9ab8

Browse files
committed
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 <[email protected]>
1 parent 691d0dd commit 5db9ab8

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Documentation/CodingGuidelines

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,33 @@ For C programs:
222222
- Double negation is often harder to understand than no negation
223223
at all.
224224

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+
225252
- Some clever tricks, like using the !! operator with arithmetic
226253
constructs, can be extremely confusing to others. Avoid them,
227254
unless there is a compelling reason to use them.

0 commit comments

Comments
 (0)