Wrong:
foo > 0
Right:
0 < foo
Why? Doesn’t this break the yoda rule? Yes it does, but this one’s better.
Readability
It’s important that code is readable. To humans. Humans are much better at spatial and visual reasoning than abstract reasoning. It’s why this post has a BIG header that says READABILITY. The shape and size give a hint that it’s the BIG idea.
So how can we apply this to something as abstract as value comparison? The number line:
The number line reads small to big, left to right. So, when doing a comparison, we should also have our data go small to big when reading left to right.
Therefore instead of
foo > 0
we will do
0 < foo
That way, we can easily transform code logic to a number line in our minds and see where 0 and foo are spatially relative to each other and therefore mathematically their relation.
Another example is ranges:
Wrong:
foo > 0 && foo < 100
Right:
0 < foo && foo < 100