Code Comments: To be Read or Scanned?


Slashdot is running a story about the interview with Joel Spolsky. Avid readers of this journal (me) will remember that I just finished reading Spolsky’s book on UI Design. Always one to share his mind, Spolsky’s comments have stirred up a tempest in the Slashdot teapot. Much of his comments seemed geared to explaining why Microsoft’s competitors failed. He cites Microsoft’s coding policy of “never rewrite,” which sounds a little hyperbolic to me. What the readers of Slashdot failed to see, apparently, is that the site that interviewed him is a “resource for software marketing, software sales, and software product marketing.” Is this the place to get cogent coding tips? No.

However, my attention was drawn to this slashdot comment from one “StaticEngine” who is arguing vehemently for better comments in code. For instance:

Any programmer should be able to look at code by another programmer and pick up on it very quickly, without shaking their head and saying “What the hell were they thinking?”

A bit later:

The best comment I heard was from a friend about a former coworkers code: “It’s English with some C++ thrown inbetween the comments.”

This all sounds reasonable, right? Who could be against better, more copious comments? Aren’t comments the key to understanding someone else’s code?

Let me take this point to absurdity, why bother with the code at all? Why not write a paper about the code that explains what’s going on. The assumption is that English is easier to understand than a programming language. So once you understand the theory of the code, then modifying it should be easy, right?

There are two reasons why liberal comments aren’t effective. The first, oddly enough, comes from Spolsky’s book. Users (or programmers here) don’t read, they scan. Writing comments to be easily scanned is tricky. After all, scanning works best when the reader has some idea what to look for and the assumption with code comments is that the reader has no clue at all what’s going on. If coders are scanning rather than reading, keeping comments small and to the point will be more effective than embedding Moby Dick (or even The Art of Programming) into your module (although quotes from Futurama and LoTR are acceptable).

The second point is probably going to sound elitist, but what the hell: if you can’t cook, stay out of the kitchen. Understanding a programming language is the primary job of a programmer. If there’s a feature of the language (and not a compiler quirk) you don’t understand, get a book and learn. Of course, I still recommend coding in common idioms and avoiding fancy tricks, but code is the definitive guide to understanding the program. You debug code, not comments. I haven’t seen a “comment debugger” yet. Comments can lead maintainers to not reading code. This is a sin. You always have to read the code. That last point is so important, I’ll say it again:

You always have to read the code.

So comments are fine if used and maintained along with the code. Otherwise, just delete old, inaccurate comments or stick them in your revision control system’s log. You are using version control, right?

I suspect there are folks on this system that will disagree with this notion, so flame away!

[Original post and comments.]