2 ms·
Occasionally it's necessary to write code that would make people ask, "why?!?" and comments definitely help there, but I feel like that's generally rare, especi
by krupan 3y ago
Occasionally it's necessary to write code that would make people ask, "why?!?" and comments definitely help there, but I feel like that's generally rare, especially when you use good variable/function names, keep functions small, keep nesting to a minimum, and follow all the other good programming practices. If you are writing/reading code and thinking it needs comments, maybe think a little harder about how the code itself could be made more clear.
Comments are not checked by compilers, linters, code formatters, unit tests, integration tests, or any other automated tools for correctness, consistency, and good formatting. They rot quickly.
- tines 3y agoIt's not about the clarity of the code though. It's about things that are uncheckable by compilers, linters, etc. That is to say, when I'm reading code I may perfectly comprehend what the code does. But knowing what it does doesn't help me understand why it does that thing, how it fits into the overall design, and things of that nature. For example, if code is operating on a list, and then it sorts that list and passes it along to some other code, I can see perfectly that the list will be sorted. But that doesn't tell me _why_ it needed to be sorted in the first place. Often that is not knowable except by reviewing things outside the scope of the code in question, or outside any code at all. A comment is essential in that context.