3 ms·
Qualification: Don't use the greater sign when doing range checks. Counter-point: When checking if a variable is greater than a constant, using the less than i
by fl0wenol 8y ago
Qualification: Don't use the greater sign when doing range checks.
Counter-point: When checking if a variable is greater than a constant, using the less than is very confusing since we read left to right.
if(x > LIMIT) {
Read "If x exceeds LIMIT, then"
The opposite reads like "If the LIMIT is less than x", which sounds strange and emphasizes the wrong thing, implying LIMIT changed, not x.
- everyone 8y agoThis. How often do I ever check if a number if within a range? Very rarely. But checking if an index is not too high for an array, is something I do very frequently.
- thaumasiotes 8y agoIn certain branches of mathematics, it is routine to work with very long inequality chains. For example: a < b <= c < d <= f <= g < h You would then conclude that a < h holds. I haven't looked at the article, but based on the comments it sounds like the author has this kind of thing in mind. I got the vibe that he was arguing that you shouldn't mix < with >, not that he was arguing that comparisons should only ever be made low-to-high.
- BurningFrog 8y agoYeah, I think the article has a point for complex/chained expressions. But if the way you think about the domain is "if x is larger than LIMIT..." you should write it `if (x > LIMIT)`!