Lately, I’ve been feeling really tired of hot discussions on if it’s necessary to add comments in the code. As a rule, there are self-confident juniors with the indisputable statement as: “Why not to comment it, it will be unreadable without the comments!” on one side. And experienced seniors are on the other side. They understand that if it’s possible to go without the comments than “You better, damn it, do it in this way!” Probably, many developers got comment cravings since they’ve been students when professors made them comment every code line, “to make the student better understand it”. Real projects shouldn’t contain a lot of comments that only spoil the code. I don’t agitate for avoiding comments at all, but if you managed to write the code that doesn’t need comments, you can consider it your small victory. I would like to refer you to some good books that helped form my position. I like and respect these authors and completely share their opinion.Steven C. McConnell, Code Complete (http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670)
Robert Martin, Clean Code: A Handbook of Agile Software Craftsmanship (http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882)
Dustin Boswell, Trevor Foucher, The Art of Readable Code (Theory in Practice)
(http://www.amazon.com/The-Readable-Code-Theory-Practice/dp/0596802293)
So, what makes me mad about the comments? Here are some main statements:
• Comments spoil the code itself, make it less readable;
• Comments require time for writing and maintenance;
• Comments lie (starting from the improperly composed comments and ending with the obsolete ones)
Basically, I agree that in some cases the comments are necessary. But the less such cases exist the more beautiful your code is. Besides, writing a good comment is the art as well. Unfortunately, just few developers have that skill, the rest of them write the comments anyhow. The benefit of such commenting is quite doubtful. Isn’t it better not to spend time and effort for writing a good comment but to spend it for re-writing the code in the way it could go without any comments?
Let’s discuss some typical scenarios and define to what extent the comment is necessary. I provide C# code, but this is not that critical for this article.
When you can go without the comments
Comments that repeat code
Let’s have a look at the following code:
// This method is responsible for calculating array elements sum
// The method receives elements array at the input
public int CalcSumOfElement(int[] elements)
{
int result = 0; // Creating a special variable for the result
int n = elements.Length; // Defining array length, i.e. number of elements
for (int i = 0; i < n; i++) // Running cycle for all elements
result += elements[i]; // Adding an element to the result
return result; // Returning the result
}
Of course, this sample is fake, but unfortunately, I don’t overdo. I saw such code quite often. In this sample, the comments don’t provide any new information; everything can be understood from code. And reading this code without the comments would be much easier. I don’t even say about maintenance of such code: it’s quite tiresome to support this level of commenting. Most likely, when the code becomes more sophisticated half of code won’t have comments, the rest of code will include obsolete comments.