Comments considered harmful
Posted by Monty on May 10th, 2010I often have this discussion with less learned members of staff, with regards to commenting code. The main argument is “Comment code so I can see what is going on!” and “If you don’t comment the code, I dont understand”. In the first instance, I recommend refactoring the code and rewriting it, so you do not NEED the comments, and usually (99.99%) with the second argument for commenting is, If you do not understand the code, should you really be touching it?
There is the 3rd style of commenting I sometimes come across, and that is the Newbie Comment. Typically, the code has been lifted from a coding website, and someone has left the comments in there, because they either dont know how to use their text editor, or they dont understand the code they are coping and pasting. Both of which, to me at least, indicate that the person should not be writing code and should spend more time understanding the framework and how things work in general, like the following:
//create file object FileInfo fi = new FileInfo(filePath); //read the file into a stream reader StreamReader sr = fi.OpenText(); //store the contents in a string string emailBody = sr.ReadToEnd(); sr.Close();
The most common comments I see are totally useless, and simply repeat what the line below will do, for example i++; //increment i. How is that comment at all useful? Its not, it just tells you what the line is doing, which the line itself should tell you. Its duplication of code, you are repeating yourself, which is violation of the DRY principle:
Don’t Repeat Yourself
From the wonderful book of The Progmatic Programmer. The comment adds nothing to the overall readability of the code, so it shouldnt be there, it is simply extra clutter to make things more difficult to read and maintain.
Recent Comments