by danso · 14y ago · 0 comments
Ironically, that's often also the attitude of programmers who don't use comments in their code (in any language).
* Comments that describe what the code does are a complete waste of time. The code already tells you what it does.
* Comments that describe why the code was written has more merit, but I cannot see how the following is any real improvement over the first point:
// Investments of this type have always added the interest
// to the principle. Who are we to question convention?
$balance = $principle + $interest;
I am one of those poor commenters, so I really have no idea what could be said about that line that isn't already known. If you say not everything needs to be commented, then where does the line get drawn? In most cases, the vast majority of your code should be no less obvious than this.* Some suggest the method signature and invariants should be documented, but that seems like just another exploitation of comments to deal with limitations of the language and is no better than putting annotations in the comments.
* I've seen code examples for using a given method in the comments, but that is better left to your test suite. Not only then can the reader see the example, but you can verify your method works at the same time without cluttering up the original codebase to boot.
If you are doing something weird, then yes, I am with you that comments are a big deal. However, if you are doing weird things on a regular basis I have to question your reasons for doing so. You shouldn't have to do weird things 99% of the time.
With that, teach us wise one. How can we all become great commenters so that we do not have to brush it off as something we do not have time for?
Comments within the code should be used to highlight things that are not what they seem. They should answer any "WTF?" questions that naturally arise. A typical example is something like "Hack: Need to pass fifth argument as -1 to avoid crash in broken library".
Comments that act like descriptive audio are useless.
As I mentioned before, it seems the number of people who can actually pull off good comments are exceedingly small. I've looked at some prolific open source projects that have a decent amount commenting around method definitions as you suggest is good practice, but found the comments to be no more enlightening than just looking at the source code itself. I will assume they were just poor examples.
I do agree with everything else you said though.
An example would be:
"Verify account takes an account ID, an optional enabled flag which, if specified, will only search for accounts that have a matching enabled status. The default is to search against all accounts. The return value is true if the account is valid, false otherwise."
These are extremely important in loosely typed languages where there is little indication as to what constitutes an acceptable argument. In a more rigid language like C++ there are usually a lot more hints in the language about what the input and output is.
Still, in programming "by contract" it is important to manage expectations and comments can communicate a lot of this.