On Sun, Jul 20, 2025 at 5:26 PM Warren Toomey via COFF <coff(a)tuhs.org> wrote:
I've written my fair share of code and also
taught several languages. I'd
estimate my comment to total LOC ratio as about 1/4 to 1/3. But I keep
coming across code bases where the ratio is closer to 1/100 and it really
bugs me! I just can't read the codebase and work out the nuances of what
it is doing.
So this isn't a rant so much as a request for alternate perspectives. If
you have a spartan commenting style, why? Can you read your code and see
all the implications, or do you dislike lots of comments, or do you write
more external documentation etc.?
My major gripes about comments are that people tend to write them
poorly, and they aren't generally checked by the compiler/interpreter:
a poor, out of date, comment can be worse than no comments. But
comments also have issues around discoverability and locality; I have
found that they are not good as a means for system-level
documentation, serving best to explain rationale at some very local
level of code. Crucially, those comments are only really useful when I
already know where to look for them, and while they may explain a
particular quirk of some code, they often don't help me understand the
system/interface/tool as a whole.
However, the "not checked by the compiler" thing has some exceptions
these days. Some languages have the notion of "documentation tests",
in which one can write unit tests in a comment, flanked by special
syntax so that they are checked by the compiler and are executed as
tests when invoked a certain way; this is useful for keeping code
examples up to date as interfaces change. Rust is a good example here:
https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.h…
When I taught, I had two mantras about comments:
Code explains how, comments explain why.
An issue is that people try to go beyond this and use comments for
more general documentary purposes, for which they are not well suited;
at least not without significant additional tooling. Google mandated
that C++ header files all contain comments that described classes _and
how to use them_ at length; this was useful, but sometimes really do
want an external document of some kind that describes things at a
higher level.
But comments are just one way of introducing documentation into a
system, even at the source code level. Python and many dialects of
Lisp both have a notion of "docstrings", for example, that are
accessible from the REPL, and serve an overlapping purpose, but are
semantically different objects than comments at the language level.
"Doc comments", used for automatically generating system-level
documentation, are increasingly common, whether part of the language
or its canonical toolchain itself, or retrofitted with external
tooling.
Code as if the person who takes over your codebase
is a crazed psychopath who knows where you live.
Ah, this old saw. :-)
For me personally, I try to minimize my _need_ for comments, to
whatever extent that I can. This may take the form of introducing a
named, but perhaps otherwise unnecessary, intermediate variable to
hold some part of a computation, where the alternative is to make the
computation itself part of a larger expression that would then be less
obvious. Similarly, I may introduce a function returning a boolean to
encapsulate some predicate behind a descriptive name. If a well-aimed
type can make a comment superfluous, say by encapsulating some
invariant of the data, and the language supports it, then by all means
introduce a new type. If this sort of technique can make a comment
entirely redundant with the code, then I favor removing the comment.
On a related note, John Ousterhout recently had a long-running online
"debate" with Robert Martin, the agile influencer. They recorded the
results of this in a text file that was then uploaded to Github; I
heard about it when John wrote to the mailing list for his book, "A
Philosophy of Software Design". The subject of comments featured
heavily in this discussion, as Martin is infamously averse to them and
Ousterhout views them as essential. You may find it interesting:
https://github.com/johnousterhout/aposd-vs-clean-code
I'm not sure why Ousterhout, who has a long track record of building
real systems, chose to give Martin, who does not have a track record
of building real systems and further displays all the trappings of a
charlatan, a platform for these topics, but he did. Sadly, when
Ousterhout mentioned it on the mailing list, it failed to generate
very much discussion. I had my own response, much of which I cleaned
up and consolidated in my own response to their Java prime number
generator example, and that I pushed to Github here:
https://github.com/dancrossnyc/literateprimes/
- Dan C.