Bisto granules, once dissolved in water, made a gloopy faux-cheese suspension, that coated your entire mouth with delicious chemicals and left a horrible, but strangely satisfying, cheesy film in it's wake.
One day, I tucked into a big bowl of cheesy pasta and realised that something was missing. They'd improved the recipe. The cheesy film was no more. Someone had decided that it was a bug, not a feature, and had got rid of it. I disagreed. As I pushed my plate of pasta away, I decided it was different, and I hated it.
A lot of what we prefer is subjective. It's not necessarily better, it's just what we're used to or how we've always done things.
Last week whilst pairing, Jeremy and I were refactoring some code and discussing whether to use a ternary operator or an if else statement.
I wholeheartedly preferred the ternary operator, I'd learnt about ternary operators recently, so it was new, shiny, concise and writing it made me feel clever. But when Jeremy reframed the question and asked which I found more readable, I had to concede that I preferred the if else statement.
if ( condition ) {
value if true;
} else {
value if false;
}
But why did I prefer reading the if else
statement? Was it actually more readable, or was it just familiar? When I broke it down, I liked that it was laid out in the same way as we read, top to bottom, left to right. If the condition is true, do this, else, do that.
condition ? value if true : value if false
When I read the same code written as a ternary operator, my eyes darted around a bit. I had to look for the "if" in the form of a question mark, then look backwards to find the condition, then follow the line forwards to find out what the result of the condition will evaluate to.
Additionally, I found when the condition or the values were lengthy, having them all on the same line was confusing.
But are these feelings objective, or are they just shaped by my own experience? Do other people feel the same way? Are some things universally more readable than others?
Daniel Berzon is currently trying to answer this with howreadable.com. Considering that developers spend more time reading code than writing it, at an estimated ratio of 10:1, it's definitely an important topic, and a project to keep an eye on.