Don’t Use Greater-Than
So I was at a conference in Nashville recently, and I watched Llewellyn Falco do a lightning talk about, of all things, why you should never use the “greater than” operator while programming. And by the end of it, all I could think of was: I have *got* to get this guy on RubyTapas. Happily, he agreed to be on the show!
I’m not going to try to do any more setup for this one, other than to say that it’s a great example of how small choices in coding style can have big implications for comprehension. Enjoy!
Video transcript & code
Don't use the greater than sign in programming
I'm Llewellyn Falco and I have a simple message for you today: Don't use the greater than sign in programming.
In other words: the 'less than sign' is greater than the 'greater than sign'
Of course what I'm really advocating is the reverse of this: the 'greater than sign' is less than the 'less than sign'
Let's say we have this simple thing we just want to express we want the condition : X is greater than 5 in less than 10
There are eight different ways that you can express this concept in code and all these eight they're all the same. I mean they're identically the same. It's confusing because they're so made it...
Wait! Wait! Hold on, nope, this one is wrong (it was probably obvious to you) but let me fix it.
Okay now they're all the same. But this is confusing, so let's just remove all the ones that contain a 'greater than sign'.
Now it's much simpler but look at that one that's weird.
It's almost looks like 10 is greater than 5.
So let's put the numbers in the right order and now we're left with just one easy way to do it. Also, when you do this over and over, because you're human, your brain is going to start to take shortcuts and that means you're gonna start to see X's as one.
Even more importantly, remember the number-line from elementary school?
Things are gonna start to map to the number line...
See; there's our 5 and there's our 10. Here's our X in between.
If we had a different conditional. Maybe we wanted to check if: you are not a teenager? Well, this maps the same way; here's our 12 here's our 20 and the X's are on the outside
This is even simplier for really simple things, like: is the length bigger than zero? I'm dyslexic so it's really easy for me to flip that greater than less than sign, It's harder to flip the left and the right hand of the equation so it's easy to see that length is on the bigger side of zero
Finally, let's try a really complicated expression, like: should you wear diapers?
Now this has 32 ways that we could write this and these are all equivalent
Again if we remove the greater than sign and we put the numbers in order. Then it reduces to one way that map's nicely to the number line.
if you're between zero and three maybe you should be wearing diapers. And if you're over 90 maybe you should be wearing diapers, but if you're over 200 well... ...you're obviously a vampire so no need there!
In summary what I'm trying to say is if you simply remove the greater than sign from your programs:
- everything's going to be easier to read.
- your conditionals are going to match the number line
- you're going to have this one consistent way
it just makes something that could have been complicated simple.
So "don't use the greater than sign in programming"
happy hacking - Llewellyn Falco