I don’t like comments
As a general principle, I don’t like comments in source code. It’s not that I consider them onerous to write, I just don’t think that they’re very useful, for three reasons:
- They’re not synchronised with the code they describe, and their accuracy can deteriorate over time.
- They’re often a substitute for clearly-structured code
- They make source code longer, which can obscure the underlying logical flow.
That may sound controversial to some people: extensive commenting is still in some quarters considered an essential discipline of computer programming. But I use modern, expressive programming languages that allow me to communicate a lot more in the code itself.
Here’s a good Ruby example I encountered earlier in the week (I’ve broken the second line to make it readable on the site):
# pick the lowest total entries pager.total_entries = kittens.total_entries > puppies.total_entries ? kittens.total_entries : puppies.total_entries
You might legitimately ask why the comment’s there at all. It’s not as if it tells us anything we couldn’t work out by reading the second line, right?
Except … the second line is a bit long, and actually needs a bit of thought to decipher it—and when we do so carefully, we find that the code is doing the opposite of what the comment says: it’s finding the highest total entries.
It turns out that the code is right, and the comment’s wrong—we might have expected that, because there are tests to ensure that the code does what it’s supposed to. So what’s the comment doing, again, except for misleading us?
There’s no blame to be apportioned here—this is a natural consequence of trying to explain a long line of code—but we can improve it. Instead of using a comment to explain what’s going on in this line, we can get Ruby to do it for us:
pager.total_entries = [kittens.total_entries, puppies.total_entries].max
In less space than the original, that clearly conveys that we are choosing the maximum of two values. There was nothing wrong with the original code, it’s just that our programming language has a better way of doing it.
It’s a trivial example, but a good principle: use descriptive code instead of comments. The desire to add a comment is often a good indicator that the code itself can be made clearer. By choosing good variable names, extracting functional chunks of code into clearly-named methods, and taking advantage of language features, comments become mostly superfluous.
There is still a place for comments: sometimes, a program has to do something counter-intuitive that can’t be explained by the code. But these cases are relatively rare.
2010-02-24 23:16 UTC. Comments: 4.
higgis
Wrote at 2010-02-25 10:20 UTC using Safari 531.21.10 on Mac OS X:
I forget who said this, but I’ve always liked this rule of thumb: “where necessary explain why you did it the way you did, not how you did it”Ben Griffiths
Wrote at 2010-02-25 16:32 UTC using Safari 531.21.10 on Mac OS X:
Oh, yes. Very well said.One comment a fortnight is about right for the average developer I think.
It’s about once every two weeks that you come across something that needs explanation because it doesn’t do the obvious; or you interface with some piece of code outside your control that needs special handling.
But, if the code doesn’t do the obvious and it’s in your control. Roll your sleeves up and fix it.
I’ve noticed that specious commenting is very common in academia, so IT graduates usually need deprogramming from this cult.
I liked your example above – one point I would make is that if the #max method hadn’t existed already, then you should have created it.
Paul Battley
Wrote at 2010-02-25 16:45 UTC using Chrome 4.0.299.0 on Mac OS X:
That’s a good point about creatingmax, Ben. Even in a relatively unexpressive language like C, it’s still possible to write amax()function (or macro, for the speed junkies who daren’t waste any cycles). In fact, unless you’re writing in assembly language, there’s probably no excuse for not doing so.Tim
Wrote at 2010-03-09 11:05 UTC using Firefox 3.5.8 on Windows XP:
But what if the comment was right and the code was wrong? And the original testing didn’t pick up a certain case, or was just plain shoddy.The comment enables me to see the mistake, be clear that it is a mistake, and correct it (instead of wondering about whether I’m doing the right thing because I’ve failed to understand the logic).
All you’ve shown here is that comments aren’t useful when they’re wrong (duh), but in my experience the code (and the testing) are a lot more likely to be wrong than the comments.
Obviously, if the code can be clear enough to avoid the requirement for comments (as per the second example (and even the first to be honest)) then so much the better.