tjb said:
I often see code like this:
/// <summary>
/// Removes a node.
/// </summary>
/// <param name="node">The node to remove.</param>
public void RemoveNode(Node node) {
<...>
}
In my view, there is no benefit in doing this. It brings nothing to the
table, and creates the following issues:
o It get in the way of the code -- it's unnecessary noise.
o When something significant changes, both the code and the comment
need to be updated, rather than just the code.
o It often breeds bad code ("don't worry about giving that method a
better name -- the comment explains it well").
I've seen some pretty knowledgeable people write code like this, mind you.
Why do people do it? Is it just a consistency thing? Must every single
method be commented?
There have been oodles of other reactions to this, so I'll try not to
duplicate what has been said. I have two points to make, which I hope
are new(ish).
First, comments are supposed to add an essential element to the code:
the code tells you what the programmer *did*. Comments tell you what
the programmer *meant to do*. Arguably, this latter function is also
served by unit tests, but the documentation *should be* a quick summary
of precisely what the method was meant to do. If it's blindingly
obvious, then that's great, but at least the developer wrote the doc so
you know that he, too, agreed that this method is as simple as it looks
at first glance.
In other words, code without documentation is missing one critical
piece of information: is what the code is currently doing what the
programmer intended it do? Or is it a mistake?
Those here who say that they don't trust documentation and read the
code instead, I'm sorry: I don't believe you. I don't trust
documentation, either. However, what I mean by that is that I read the
documentation, and then evaluate whether it: a) looks complete and
detailed, and b) given what I'm after (which is usually tracking down a
bug) does it seem that the documentation is accurate? If the
documentation seems reasonably clear and it seems unlikely that the
method in question could be the source of the problem, I move on. I
can't imagine that programmers who "don't trust documentation" read
every line of every method they come across while looking for a bug.
I'm guessing that, like me, they read the doc, but warily,
half-expecting it to be inaccurate. It's a guide, not the gospel truth.
That said, when I find a method for which the doc isn't complete, or
which doesn't clearly state what the method really does, I *update the
doc*. No matter who wrote it (and often it was me who wrote it). I see
it as my job to maintain the documentation (the statement of what
*should* be happening in the code) as it is my job to maintain the code
(which makes things happen). If methods have bad doc, I improve the
doc, just as if methods have missing unit tests, I add them. I do that
because I eventually want the comments to do a good job of expressing
the intent of the method, so that later they'll help me decide how to
fix bugs.
Second, there are always those routines that are so obvious and so
simple that they really don't require header comments, so why write the
comments? I will offer another reason beyond those already given by
other posters: discipline. Simply put, people (and thus programmers)
tend to settle into the lowest common denominator of sloth. Yes, it's
probably true that very simple methods don't need even the minimal
documentation that the triple-slash template asks for. However, if you
say to your team that they don't need to document trivial methods, then
shortly thereafter the bar on what constitutes "trivial" will be
lowered to include methods that are a little more complex, but which
one can still guess at what they do without any help. Then the bar will
be lowered further, then further.... The point is that people being as
they are, it's often a good idea to say, "You have to document every
single method" rather than make exceptions and watch the slow slide
into "no documentation".