Kernighan's lever
Brian Kernighan famously wrote:
Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it? — The Elements of Programming Style, 2nd edition, chapter 2
The following version also circulates on the net:
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
This second quote may or may not be by Kernighan — the questionable use of "by definition" makes me uncertain — but it is useful as a provocative sound bite conveying the same essential idea.
It is tempting to interpret Kernighan's aphorism as a warning: Stay away from clever techniques, it seems to say, because if you write clever code, you will never be able to get it to work. But this interpretation is unfortunate, and rests on the false assumption that cleverness is static.
While it is possible that Kernighan intended us to interpret the message in a specific way, he wisely restricted himself to merely presenting an observation, allowing us to draw our own conclusions from it.
Pay close attention to what is actually being said: Having written code as cleverly as you can, you will suddenly face a problem that you are not clever enough to solve. Certainly, "clever" in this context does not refer to some innate talent, because nobody is born with the ability to write clever code in the first place. The "cleverness" required to write and understand intricate code is an acquired mental skill.
If you are a programmer, you will be familiar with a sense of wonder, gradually transforming into utter stupor, as you stare at some perfectly reasonable code that couldn't possibly fail, and yet somehow it does. And since you are confident that you understand how the code works, having written it yourself, you feel that you must be able to figure out what is going on. Not only the desire to deliver working software on time, but other powerful forces such as pride, stubbornness and curiosity contribute to the motivation that pushes you onwards through the arduous task of tracking down the root cause of the error. Suddenly you see it, and you're blinded by a bright light as all the pieces fall into place. The inexperienced programmer may fall into the trap of self-degradation: "Oh, look at how stupid I was!" But that same sentiment is proof that your programming-related cleverness, or skill, has increased: "Oh, look at how clever I've become!" (Although I wouldn't recommend saying that out loud.)
... continue reading