Well-Commented If Statements Make Software Robust
mikerourke.dev
mikerourke.dev
I often see comments from junior developers that more or less re-state what the code already says.
// Do the thing if foo
if (foo)
thing();
These types of comments are just noise because they add no additional info. Now, if the comment had told me why we do thing in case of foo, it would actually be helpful.I’m not in favor of “space shuttle” commenting because it is too easy for comments to rot. Still, it’s hard to know when to leave a comment, and it’s a skill I’m continuously improving on.
https://softwareengineering.stackexchange.com/questions/5130...
I spent many years trying to write my code well enough to not need comments. At the same time I just hit 9 years at my current job and what I’ve come to realise is although the code reads, and makes sense, it sometimes lacks context of why.
So for the last few years I’ve been trying to add brief supporting comments to explain /why/ something was done, or how it ends up in a certain state, etc.
As a result when I go back to that code week or months later I find adding functionality or fixing bugs much easier.
I try not to comment too much or too long unless I really need to.
I'm in the same boat when it comes to knowing when to leave a comment. I might suck at it, but I subscribe to the belief that it's better to look like an idiot than an a$$hole. I might look dumb by stating the obvious in a comment, but it beats being someone who didn't bother explaining themselves because "good code is self-documenting" [eye roll].
The hostility versus unnessecary comments is just making devs cargo cult for "clean code".
We end up spending a lot of time naming things (variables, functions, classes etc) well, we forget we can also just include a longer description to help.
For me, most of what passes for programming is "refactoring" and fixing edge-cases.