Well written code seldom needs any comments, if the algorithm used is
known or trivial.
I tend to agree, but I don't think that zero comments is the right
answer. Lightly (but accurately and appropriately) commented code suits
me fine.
Specifically, I don't think that having a comment on or near every line is
of any use at all, as more often than not the comments get out of date
(stale) with respect to their contents under such heavy commenting policies.
On the other hand, some of the most esoteric & bizarre code ever seen (seems
to be an epidemic amongst low level system and driver code) has few or no
comments at all. In one sense, if you can't figure out what that code is
doing, you probably shouldn't be in there. On the other hand, using it as
a test for the next guy, or a barrier to competition isn't a good thing
either.
I tend to write clear block comments at the top of a function describing
the intent, how it works, and why that approach was chosen if there
could be any controversy about that. If a well known algorithm is being
used for something, I give a source for it. I also tend to use longer
"self-commenting" variable names apart from loop variables, and comment
them once at declaration, and let the code speak for itself most other
places unless something is being done that is likely to be misunderstood,
or perhaps accidentally thought to be unnecessary. Examples of the
latter would include platform-specific code, performance optimizations
where the specifics used are critical for some purpose, or other very
complicated code that needs to be there for a non-obvious reason.
If it isn't, the comments should describe the algorithm, not the code
implementing it.
I agree. /* add one to x */ comments and their ilk are a waste of time.
Something like:
/* this code uses XYZ's Algorithm for Random BS extraction per the
* paper at
http://randomlink.com/whatever
* At the time this code was implemented it represented the best
* known approach to this problem.
*/
Is the type of thing I do most for anything that might not be obvious
to J. Random Maintenance Programmer.