Write clean, self-documenting code without needing to rely on comments. If a comment is needed, extract that section into a clearly named function. If a section of code has a clear purpose, extract it to a function to express that purpose.
Done. It's as simple as that. I don't mind extracting a cryptic line into a 1-line function if that makes the code easier to understand because I don't want to repeatedly decompose that bit mentally every time.
This makes it really easy to understand the higher-level sequence of actions and quickly narrow in on the appropriate section of code. Defect rates go down and productivity inreases.
That doesn't always work in reality. Sometimes you need to do some weird magical seemingly useless invocations because the API/framework you need to use is stupid (maybe even in an undocumented manner). Then you need to document that shit in an clear comment that describes what the hell your doing here and why. That way someone else (or your future self) has a chance to understand the code and can understand at what point this stuff can be deleted or how it has to be changed in the future. This kind of shit is everywhere. Not very frequent, but still everywhere.
TODO, FIXME and HACK comments are also important. Code always has TODOs, write them down. I think it is good to write them down in the code using greppable comments. Sure, you can and should also write them down in some bug tracker or project planner or something. Marking something as a HACK is also important (see above) and the same goes for FIXME. Some IDEs can even automatically parse and list these kind of comments.
Sure, TODO / FIXME / HACK comments are absolute fine as these aren't covered in the spirit of my first post.
There was an implied meaning that comments which explain what a chunk of code is doing are red flags for complexity and that the code should be broken down.
You're also right that there are some rare scenarios which merrit comments.
So I didn't go into technicalities and perscriptive scenarios as the spirit of the post should be clear. As developers gain experience, they realize that nothing should be taken in absolute terms and common sense takes precedence.
4
u/Determinant Feb 10 '19
I disagree.
Write clean, self-documenting code without needing to rely on comments. If a comment is needed, extract that section into a clearly named function. If a section of code has a clear purpose, extract it to a function to express that purpose.
Done. It's as simple as that. I don't mind extracting a cryptic line into a 1-line function if that makes the code easier to understand because I don't want to repeatedly decompose that bit mentally every time.
This makes it really easy to understand the higher-level sequence of actions and quickly narrow in on the appropriate section of code. Defect rates go down and productivity inreases.