Number-line comparisons are good for code

Thursday December 26, 2013

You have a lot of flexibility in how you write comparisons with most programming languages. Typically, for example, you can use either of x < 5 and 5 > x.

I recommend the convention of writing comparisons as if they come from a number line. That is, always use "<" and "<=", and never use ">" and ">=".

This convention reduces cognitive load - it makes code easier to read and write. This is particularly true when testing ranges, and I find it  to make an exceptionally large difference in the common case of testing date ranges. Compare the following two lines of pseudo-code:

date > 2010-04-23 & date < 2010-08-11 # bad
2010-04-23 < date & date < 2010-08-11 # good

The second line is much more readable than the first. Indeed, even less clear versions are possible. It becomes easy to introduce errors, and even not unlikely that you'll be testing for impossible ranges.

In some languages (notably Python) you can write chained comparisons like 3 < x < 5 and it will work as expected. In some languages you can write chained comparisons like that and it will evaluate but probably not as you intended. In JavaScript, 3 < 4 < 2 is true. (wat) In Ruby and R, chained comparisons like these will give you an error. So I prefer the style already shown, with the variable being tested close to the "and" operator joining the two comparisons.

Many languages use "=" for assignment and "==" for testing equality, so it has been noted that 5 == x is safer than x == 5 in the sense that if you mistakenly write x = 5 then you've broken something, but 5 = x is just an error that will get caught. I'm not terribly concerned about this. In both Python and R, the equivalent of "if x = 5" is an error.

Since R uses "<-" for assignment, there is a similar possible problem:

x < -2 # compares x to -2
x <-2  # assigns -2 to x
This is indeed annoying. (Thanks Tommy for pointing it out.) I think the advantages of writing nice number-line comparisons outweigh this risk in R, but it is the most compelling argument I've seen for not using "<-" for assignment.

This post was originally hosted elsewhere.