a thoughtful web.
Good ideas and conversation. No ads, no tracking.   Login or Take a Tour!
comment by cpm
cpm  ·  3179 days ago  ·  link  ·    ·  parent  ·  post: Kernighan's lever

I think the appropriate thing is to write the program as a whole cleverly, and to make each portion understandable, either through detailed commenting, or by making the code (and naming!) easily interpretable. Cleverly doesn't necessarily mean inscrutable, as briandmyers pointed out. I believe people mean several different things when they talk about clever code:

1. Written with the best abstractions to avoid duplication of code and make future extension easy

2. Optimized for efficiency

3. Optimized for size

4. Written in an "interesting", complex way

When most people hear this quote, I suspect they immediately think of the 4th definition and conclude that code should be written in a straightforward, stupid fashion. In this article, though, I think the author is emphasizing us to concentrate on the first three situations. For example, if you're implementing a file system today, you need to be clever about it. You could go and create something like a straightforward S5FS, but unless you start taking into consideration how the physical disk works and the best ways to place your blocks, you're not going to get good performance. The same goes for many other applications: anything crypto requires cleverness and intelligence; anything related to performance requires a deep understanding of the problem and its contexts.

Also, the clever solution isn't always the complex solution. Compare Raft and Paxos, two protocols for distributed consensus: Raft is very straightforward, clever, and easily (compared to Paxos) implementable. Paxos, on the other hand, is a beast unto itself.

I think the key issue when this quote comes up is people interpret the word clever as solely meaning accomplishing the same thing as the straightforward solution in a way that requires a lot of thinking. But sometimes this is done because fully solving the problem actually requires some hard thinking. And this is the key part: does your problem actually require much intelligence? Is it of a large enough size that you need to be that concerned about performance? For most people and most problems, I suspect that the answer is no. But this doesn't mean you shouldn't think about it first. After all, sometimes the clever solution is the better solution, when the clever solution makes your life easier.