All of the above is why I tell learners to focus on writing comments that describe their
intent, instead of describing the code.
When I implement a standard
Xorshift64* pseudo-random number generator, I simply name it. When I only use the 32 to 40 high bits of its output, I mention that that way the output passes the full BigCrunch randomness tests, and is thus "better" than e.g. Mersenne Twister (MT19937).
In multithreaded code, I use comments to describe the locking schemes. For standard
pthread_mutex_t protecting a set of variables, I mention that where the variables are declared, but for anything more complicated, I mention the scheme is described in an external document (
LOCKING, typically).
If I implement an easily rehashed hash table or say a disjoint set data structure, I describe it as such, and try to explain why it was chosen (by describing the most important features, and most unimportant downsides). There is no need to explain standard abstract data structures internals; only if one deviates from standard data structures. For example, if you create a thread-safe binary tree, you definitely need to describe the locking scheme and its limitations, especially wrt. node deletions and concurrent traversal.
For embedded/microcontroller code accessing hardware, it is nice to have a root-level block comment describing where that hardware is documented, especially when multiple subsystems are used. (Say, an interrupt and DMA-driven SPI interface, and its latency and interrupt priority requirements.)
There is
absolutely no way to reliably infer developer intent from the code itself. The code is the result, not the design; and any particular feature –– or even the overall approach –– may be an unintended error, or more commonly, a misunderstanding or a "thinko": a logic bug/error similar to a typo. These happen to every single human programmer.
When dealing with code without comments describing the developer intent, I always have to try and infer that first. It is a heuristic process, and therefore prone to errors. Anyone claiming they don't write comments because they don't think they are necessary, needs to be hit with a cluebat. The true purpose of comments is to provide the information the code does not provide, and that is
intent. Only when the intent is known, can one compare the implementation to the intent. Even then, either one can be faulty! When the intent is not documented at all, it can only be inferred from the code, which takes time and adds another possible source of errors.
When I am trying to fix or extend others' code, my most common thought is
"what the hell did the author of this
crap think when they wrote this?"because in a vast majority of cases, problematic code could be simplified by switching to a better algorithm/approach. This definitely applies to my own code as well, because "good code" is not simply written once and then put out there. Truly good code
evolves through refactoring and algorithmic optimization. Sure, if you intend to write a piece once and never touch it again, you can omit any comments and write documentation that satisfies the client, but such code is never, ever truly "good"; not when compared to code that has been refactored and competed with other possible implementations.
This is why documenting the developer intent matters: it provides the information needed to fix/enhance/refactor the code in the longer term, allowing it to evolve instead of just accumulate/aggregate into a larger, more complex whole. If your refactoring increases rather than decreases the complexity of the whole, you are doing it wrong. Having to try and heuristically infer the developer intent from the code is unnecessary waste of effort, because the intent can always be described in simple, short comments. Not even trying to write them in the first place is, well, kicking extra work onto others because you're too lazy to do your own bit.