⇥ The myth of the myth of self-commenting code

April 30, 2009
5 comments
 
⇥ Permalink

On Code Commenting And Technical Debt | BrandonSavage.net

Interesting post by Brandon—I disagree only on one main point:

This myth of self-documenting code needs to be squashed once and for all. Commenting is important, and unless your code does nothing other than display “Hello World!” it’s going to need some explanation.

I find this view of coding a little misguided, to be honest—in fact, I would go as far as saying that code doesn’t need any comments.

Wait, did I just say that? Yes, I did, and here’s why: code is a discussion between the developer and a machine. Writing code that cannot be comprehended by another human being is a bit like writing a letter to your dear friend in a language you can write but not read—strange, at best.

Good code should be self-documenting insofar as you should be able to understand what the code does without the assistance of any comments—after all, the computer gets it, so why shouldn’t you? This can easily be done by the judicious use of clear logic and good programming practices like giving your variables names that are actually related to what they do.

What I suspect Brandon really means is that the comments are there to illustrate the intentions of the author when those intentions are not immediately made obvious by the code itself. This, I daresay, is the crux of Eli’s argument that commenting inside the code is the most important contribution that a developer can make towards eliminating technical debt. That’s because commenting inside the code helps understand the reasoning behind the choices that were made when writing the code, rather than what the code itself accomplishes.

Let me make an example using an actual piece of code taken from a project I’m working on in an effort to avoid the hello-world syndrome that so often affects this kind of post:


function construct(array $pathInfo) {
$section = $pathInfo[0];
$file = $pathInfo[1];

// Assume that the location of this file is inside the main trunk, and that pathInfo has already been filtered.

$path = dirname(
FILE) . '/../../' . $section . '/xml/static/' . $file . '.xml';

if (!file_exists($path)) {
Controller::singleton()->doError(404);
}

parent::
construct('main/xsl/template.xsl', $path);
}


Now, as you can see this code contains only one comment, but I think that any reasonably competent developer would have no problem understanding what it does, because the code itself gives you all the assistance and hints you need. On the other hand, all sorts of big red WARNING flashing lights would be going on in a developer’s mind without the comment: why is the code looking for things using relatives paths? And, most disconcertingly, why isn’t the $pathInfo array being filtered before usage*?

Thus, the concept of “self-documenting” code is not a myth—code should be written so as to tell a developer what it does without the need for any comments. It is a myth insofar as people use it as an excuse to avoid documenting their intentions, which is really what comments are useful for.


* I cheated a bit here, because in the real codebase the filtering is done system-wide, so part of this comment is really superfluous if you know the codebase’s overall design, which, of course, would be documented elsewhere.