Posted in Software Engineering, Technology
Tuesday, March 14, 2006
Document your code. This is done by inserting comments. The computer ignores them but they are read by other programmers. Almost all serious programming languages allow comments. However, not everyone agrees on how many comments to write and what they should contain.
A common practice I have seen is to write program prologues. These are a collection of comments found at the very beginning of a source code module. Usually it documents: a copyright notice, the source code module name, a description, the date created, the author’s name and the revision history.
Each method can also have a prologue that documents: the method name, a description, the parameters and the return type. I have heard it said that these comments are not necessary since code is self-commenting. The argument goes that if the author chooses a descriptive name for a method and its parameters, then no comments are necessary. In practice this is never true. Don’t be that lazy and arrogant developer who needs a swift kick in the posterior.
It is possible to go overboard and document too much. The problem here is that as code changes you now have an additional task of maintaining the comments. A good rule of thumb is:
Comments should document the "why" not the "how"
For example if the developer choose to implement a splay tree he should not document the fact that it is a splay tree. Why? Because another developer may decide that a red-black tree performs better and replace the original implementation. Now the comment has to change, too. It is better to document things like hacks and decisions that maintaining developers should notice. For example, if there is a bug in the third party library you use to format a date, a workaround may be developed to fix it. A comment should be written to document the fact that the code has been written to work around the problem.
Remember that comments are written for humans. Be aware that other developers read them. Do you really want them to remember you as a foul mouthed crank? Think about that before vent your frustrations in your code comments. Just a thought.
