Personally, I do not write many comments, but the general types of comments that I write:
- (sketch) proves that my code is correct.
- explanations, why not just do the obvious thing (for example: optimization is required, the API that I call causes terrifying hype, the "obvious thing" led to a subtle error).
- a description of the input boundary case requiring special processing, explaining why a special processing code appears
- a reference to a requirement or specification that provides for the behavior performed by a particular line of code
- description of the top level of the sequence of steps: ("first get the input", "now split the shell", "finally parse"), which are not needed if the names of the functions called for each step are well selected and unambiguous in the context, but I'm not going to write a shell do-nothing for a generic function, just to give it a more specific name for one use
- The documentation will be automatically extracted. This is a large proportion of the comments in terms of volume, but I'm not sure that it "really matters."
- things that may be part of the documented interface, but not because they may need to change in the future, or because they are internal helpers that are not needed by third-party users. Therefore, it is useful for the developer to know, but not for the user. Invariants of the private class are included in this - do you really want to read the entire class every time you want to rely on "this member is not equal to zero", or "the size is not larger than the capacity" or "access to this member must be synchronized"? It is better to check the participant’s description before his appointment to see if you are allowed. If someone does not have the discipline not to violate the clearly commented invariant, well, it is not surprising that all their comments are erroneous ...
Some of these things are handled by complex tests, in the sense that if the tests pass, the code is correct (hah), if you also open the test code, you can see special input cases, and if some fool hides comments, he makes the same mistake. like me and refactoring my code to do the obvious thing, then the tests will fail. This does not mean that comments do not do this better in the context of reading code. In none of these cases is reading a comment a substitute for reading code, they are intended to emphasize what is important, but not obvious.
Of course, in the case of automatic documents, this is quite reasonable and probably more useful for viewing the documentation separately.
When debugging, as opposed to making changes, it is more valuable that the code can explain itself. Comments are less useful, especially those particular invariants, because if you are debugging, then there is a reasonable chance that the person who wrote the code + comments made a mistake, so comments can be erroneous. On the positive side, if you see a comment that does not reflect what the code does, there is a reasonable chance that you found a mistake. If you just see what the code does, maybe for a while before you realize that what the code does is not what it should do.
Sometimes good naming is important to replace comments. However, names can be as deceiving as comments if they are mistaken. And frankly, often names are at least a little misleading or ambiguous. I doubt that the author of this blog post will go to replace all the names with "var1, var2, func1, func2, class1, class2 ..." to make sure that they were not distracted by the original author wrong intentions for the code. And I don’t think it would be true to say that "comments are always wrong," than to say that "variable names are always wrong." They are written by the same person at the same time for the same purpose.
Steve jessop
source share