Cleverness is Good

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.

Explore posts in the same categories: General

3 Comments on “Cleverness is Good”

  1. Ben Says:

    Thanks for posting this. I know where Uncle Bob was going, but clever programmers do not cause the most bugs. Most the bugs I see are caused by programmers who haven’t taken any time to think about the problems their solving. A clever approach would be a breath of fresh air compared to the straight ahead, brute force, special casing non-sense I usually see.

  2. “Everyone knows that debugging is twice as hard as writing a program in the first place. So if you are as clever as you can be when you write it, how will you ever debug it?” — Brian Kernighan

  3. (BTW, that quote isn’t supposed to be counter to what you are saying. Just more food for thought. No snark or trite response intended)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: