2 ms·
Comment your code. Top level comments explain the purpose behind a function or type. It can explain nuances that can't be discovered from the name (does this
by NateDad 10y ago
Comment your code.
Top level comments explain the purpose behind a function or type. It can explain nuances that can't be discovered from the name (does this function append a newline at the end? Does it ignore empty values? What happens if this value is null? etc).
Aside from trivially bad comments (i.e. "add 1 to n"), comments are almost always helpful to get into the head of the person that wrote the code (even if that person was you, a few weeks ago). Comments are great at explaining why. Why this code exists at all, why it's doing things the way it is, why the hell we're removing the last character of this string, etc.
Sure, you should write clear code... but there is no way that a function name can convey all the details of the function... otherwise the name would be as long as the comment you put above it:
IndentJsonWithTheGivenIndentStringAndReturnWithoutTrailingWhitespace
If your comments are getting out of step with the code - you need better code reviewers. Code review should be a part of any professional development organization. If you're just hacking on a side project on your own... then yes, it means you have to review your own code. This is a skill, it takes practice, but one of the things you should always be looking for is "do the comments match the code?"
I would be very VERY skeptical of any experienced developer that said they don't comment their code.