What types of code comments and documentation do you find useful that you wouldn't be able to find in the code itself, and which ones do you find counterproductive?
Sometimes that means explaining the requirements or business reasons that drove a choice. Other times it is, "I'm in crunch mode to get a release out, feel free to improve this when you have time." Both of those will help future coders make a decision whether or not to change the code.
More often than not, a type definition or a smaller function with a descriptive name and predictable properties/parameters serves the purpose of all of these cases much better than a comment would. And when I do write comments, my heuristic for whether it’s valuable is basically “is it longer than a sentence, and can it link to some other reference for future reading?” Secondarily, “did someone ask about this in review and we decided to leave something non-obvious intact?” Not perfect heuristics! But they’ve served me well, especially compared to trying to understand comments which don’t seem to make any sense when I encounter them.
Now that one's easy: when some joker changes the code and ignores the commentary right above the affected line (I know this is a silly example, because this comment is more-or-less restating what the code does, but I have a hard time distilling the actual cases I encounter at $JOB into a tl;dr)
// if the list came back empty, warm the local cache
- if (items.isEmpty()) {
+ if (items.size() == 1) {
I do have some sympathy for not changing the method docs, or class docs, or a completely separate file that describes the code's behavior, but not reading the *but it's right there* drives me bananas
I once had a colleague who wrote extra long comments and I felt like it was an overkill (maybe I was intimated!?) and a distraction when reading the code with IDE that cannot hide comments as needed. If you’re a vim user see [0] on hiding comments with fold.
Example: https://github.com/torstenvl/cpgtou/blob/master/cpgtou.h
Typically in code archaeology settings, I've been trying to figure out the correct behaviour when something non-visible has changed, and the code as-is isn't trustworthy for whats the right expectation
What do I mean by that? Write as many comments as possible. You do not need to read them. However, if you could help at least some people finding the why of some code, you helped them a lot.
Also never underestimate how much the human brain forgets in a couple of months. Many devs are blind to the fact, that for every part of code they are working at, a lot more don't.
John Woods has it: "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. Code for readability." Comments help a lot.
Do your teams as well as your future self a favor. ;)