Code Comments, Documentation

Writing code tends to be harder than it needs to, because there’s a constant need to please a bunch of other people who want to read and under­stand it. Sometimes, the person who writes the code needs to worry about being able to under­stand it several months later.

Conven­tional wisdom holds that the solution to the problem is documenting the code appro­pri­ately. Conven­tional wisdom is wrong. Reasons are manifold, and quite obvious once you start to think about it.

Documen­ta­tion is always out-of-date. It’s impos­sible to have anyone and everyone who updates code to update the comments and documen­ta­tion accurately. Sure, you could try, but to a large extent, this is simply beyond your control. Typically, this means you need to assume the documen­ta­tion is wrong and read the code in detail anyway, so you might as well simply start by reading the code.

Reading the code is the only way to figure out what it is doing. Documen­ta­tion only tells you what somebody thinks the code is doing. It is quite possible that they’re wrong, or only partially correct. Besides, it’s better to build up a model of what you think the code is supposed to do, than to be told what to expect.

Code is more concise. If the code is well-written and self-documented, it is gener­ally faster to read the code and under­stand what’s going on than to read the comments (which are, by defin­i­tion, more verbose than the code they attempt to explain). And if you can’t trust the programmer to write good code, you can’t trust the guy to write good documen­ta­tion either.

There are some situa­tions where documen­ta­tion is neces­sary, of course. Examples would be when you are attempting to explain the motiva­tions or struc­ture of your design at a larger scale, or if you want to explain what the code is not — why you decided to something one way and not the other.

Tags: ,

4 Responses to “Code Comments, Documentation”

  1. Amit Prakash Ambasta Says:

    aha, ext4 inspired anyhow?

  2. Chintan Agarwal Says:

    I guess a big problem with documen­ta­tion is that it tries to explain the working of something that is very objec­tive in nature. I mean, to the programmer, it will be too obvious to be explained and to the third person, it’ll be too complex to under­stand anyway. That’s why I always thought that documen­ta­tion, beyond a very brief summary of the line of thought of the programmer, is useless. But then, I doubt geeks ever under­stood the need for such things anyway :P .

  3. RRI Says:

    No, Slashdot-inspired…

  4. RRI Says:

    @Chintan: I suppose there are levels of geeki­ness. :-) Long time no see, how have you been?

Leave a Reply