Comments, comments, everywhere, but are they making your code clearer or just distracting you? Comments are meant to be there to explain code and help you to understand it. However, they are often out of step and therefore can confuse. Think seriously before adding comments; it is often better to refactor the code so that it doesn’t need so many comments. Less is more as far as comments go.
At one time most universities and colleges really pushed students to add comments to make their code clearer. Unfortunately this often ended up with comments just reiterating what the code was doing, thereby breaking the DRY Principle:
I wish that I could say that this no longer happens, but some programmers seem to have trouble shaking off this commenting for the sake of commenting mind set. I think that the code above is actually clearer without the comments:
I have lost count of the number of times that I have seen comments express the exact opposite of the code. Sure the following line from above was pointless:
But what about:
It is no wonder that this sort of thing happens; a large part of programming is trying things out to see what works. You may have good intentions with your comments the first time you write something, but are you really going to change the comment each time your ideas change? The main thing to remember is that you will rarely read your own comments and hence not realize when they are wrong.
Refactor Rather Than Comment
Comments should not be used as a crutch to support poor code. Wherever possible make the code clear through good use of variable names and methods to describe use and intention. Let the code explains itself. Once you have clear code you can easily test it; something that you can’t do with comments.
The following is inspired by code from xAce. It is not at all obvious what the code inside the function is doing, nor what is expected to be passed to
By moving some of the functionality into separate functions and renaming the variables and parameters it is much easier to see what is happening:
The code is a little longer, but ask yourself “Which code would you prefer to debug or modify?” As far as what to refactor, this will come with experience and familiarity with the common idioms for the language that you are using. There are a number of ways that the above could have been done, but this way worked for me.
Consider Using Assertions Rather Than Comments
The oft repeated reason for comments is to let people know what you can and can not pass to a function. I like to try and keep everything in code. To do this you can use assertions. So in the
save_program() example above, instead of using comments:
Put this into the code using an assertion:
Now the assertions can be tested and kept up-to-date. Of course, assertions can be ugly if overused, so check the data at point of entry and after that trust that it is still correct. It is worth noting that this is not an alternative to good api documentation, where comments definitely should be used.
Won’t All These Extra Methods Make it Slow?
It depends on how you do it, modern compilers are pretty clever and you can often tell the compiler to inline methods if needed. You may also want to consider wrapping some of the
assert() statements so that they are only called when in Debug mode. In general though, I find that the performance difference isn’t noticeable, but the maintainability is increased dramatically.
When Should I Use Comments?
Comments are useful to say why you did something and to state requirements. They are also great for documenting the api, particularly when they are meant to be parsed by external tools. If you want other people to use your library, you have to reduce the barriers to their doing so. Being able to quickly understand the api, will make it much more likely that it gets adopted.
In short: Use comments for what you can’t say in code.