Thursday 28 August 2008

.Net code comments: To use or not to use ?

There are different types of comments we "often" use in our code, for example, inline comments in methods, XML comments on classes, etc. Are these comments really useful ?

In my opinion, they are and will be useful if:

- they are precise and clear (no need to tell stories in your comments)
- they are updated together with the code they explicit (Revise your code comments)

XML comments enable Visual Studio to provide intellisense on your classes and methods. In addition to this, they can be used to generate techincal documents (e.g. .chm files with all your class and method descriptions).

Most of the time, inline comments won't be necessary provided that you have an intuitive method name with a precise XML comment. Depending on the complexity of the method, you'll be writing some inline comments explaining specific parts of your code. For example, we could do this to explain some complex business logic. Another solution could be to move this logic to another method having its own XML comments.

Once again, there is no one good solution to a problem. The best solution is the one which best suits the problem at hand.

I thank my colleagues for the discussion we had on the above subject.