Hacker News new | ask | show | jobs
by fl0wenol 2751 days 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.

3 comments

Yeah, 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)`!

In 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.

This. 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.