I stumbled across a nice essay on why Cleverness is bad and it bothered me.
I mean, it’s a good rule of thumb and all, and Uncle Bob’s advice is dead on in the situation he describes. Like this:
Trick #9 was the use of the and keyword to couple statements together to make “one liners”. It was billed as a trick that [cough] “more confident” Ruby programmers use.
So I got the pickaxe book out and looked up the and keyword. I showed Justin where it said that the second clause won’t be executed if the first clause is false (or nil!) So if handle_batch ever returned nil, then display_batch would never be called.
Yep. Bad. And in his story the code works at first and then goes bad later. It’s pitfalls of cleverness 101.
But suppose we truly carried this out to the extreme. What if we truly banned cleverness outright, retroactively, 50 years ago. What would be have saved the world from?
The basic diff algorithm is incredibly clever. Without that I’d be sunk. If we weren’t allowed to be clever at all then diffing two files might take minutes instead of under a second. The code that implemented the algorithm would be simple and maintainable, and almost too slow to be useful.
Ok. I totally cheated. That’s not a bad kind of cleverness. That’s important fundamental algorithm stuff.
I could come up with more examples. Atomic database commits we use hundreds of times every day, really fast web searches, clever clever clever. And it’s all good.
We need to precise about where we draw the line.
Could it be that only truly smart people, like Google engineers or Bell Labs people are allowed to be clever? I don’t buy it. How would you know when you were smart enough? You can’t know.
Could it be that when you’re practicing math it’s ok to be clever but not when coding? There’s some Haskell programmers out there who might agree. But there are enough Django and Rails users out there making clear every day that there’s plenty of good cleverness outside of the maths.
Here’s where we should draw the line. And it’s so simple that it’s not brilliant. It gets boring here,
Cleverness comes with a cost. Cost is the thing. It so boring I can barely write this.
I give up something when I come up with some clever way of making a system plan it’s own actions instead of making the user write long brittle configuration files. My planning algorithm could fail. I have to invest more in testing it up front to try and reassure myself and my users that it will actually work in the field.
But every once in awhile cleverness delivers big value. Maybe it gets things done faster by orders of magnitude. Maybe it saves humans from raking through minutia. When the value exceeds the cost, cleverness wins.
And Uncle Bob was dead on in that situation. The cost of (ab)using “and” in ruby code is that it sits as a landmine. Someday your code changes and you might not have tests to catch the failure. The benefit is almost nil.
But sometimes there’s real gold waiting outside of the usual box. Users tend to like the gold.