Should You Comment Your Code? Exploring the Good and Bad of the Practice

  • 30.05.2023

In the world of programming, few topics elicit as strong a reaction as the practice of commenting code. The utility and necessity of comments have been hotly debated among programmers for decades. For some, comments are absolutely crucial; for others, they are seen as a crutch or even a detriment. But should you comment your code? Is it a good or bad practice? Let's delve into it.

Understanding Code Comments

Before we begin, it's crucial to understand what we mean by "commenting code". A comment in code is a note written by a developer that explains what a particular piece of code is intended to do. It doesn't affect the actual functioning of the program; its primary purpose is to improve readability and understanding for developers.

The Case For Commenting Code

Enhances Understanding

One of the major arguments for commenting code is that it enhances understanding. When a developer comes back to their code after a long period, or when a new developer takes over the project, comments can clarify the purpose and function of complex or obscure code snippets.

Promotes Better Collaboration

Comments also enhance collaboration among team members. They are a form of communication between developers, explaining the logic behind particular choices, specifying the purpose of a function, or even providing warnings about potential pitfalls.

Aids in Debugging

Comments can also serve as a useful tool for debugging. If a developer provides a short note about what a particular line or block of code is supposed to do, it can help identify where things have gone wrong if the program doesn't behave as expected.

The Case Against Commenting Code

Comments Can Become Outdated

One of the main arguments against commenting code is that comments can become outdated. Code often evolves over time, but the comments attached to it might not. This can lead to a situation where the comments contradict the code, leading to confusion.

Comments May Indicate Complexity

Another argument against comments is that they may be a sign of overly complex code. Robert C. Martin, in his book "Clean Code", advocates that if you feel the need to write a comment to explain your code, it might be an indication that your code is not clear enough. Code should be self-explanatory, following the principle of code readability.

Dependence on Comments

Lastly, reliance on comments can indicate a lack of proper documentation or structure in the codebase. Good code should be organized well, with functions and classes that have clear responsibilities. Over-reliance on comments can sometimes be a band-aid over a deeper issue.

Striking a Balance: Commenting Effectively

While both sides of the argument have merit, the answer isn't as binary as "comment" or "don't comment." The real answer lies somewhere in the middle: comment your code, but do it effectively.

Good comments are those that provide context and explain the 'why' rather than the 'how.' Commenting should be about explaining the logic, the reason behind the design choices, any non-obvious decisions, and perhaps any related resources or assumptions.

Consider removing the need for comments by writing clean, readable, and self-explanatory code. Use meaningful variable and function names, break down complex problems into smaller, manageable ones, and adhere to coding standards and best practices.

Finally, keep your comments updated. If you modify your code, make sure to revise the associated comments. Outdated or incorrect comments are worse than no comments at all.

To comment or not to comment isn't the question. The question is: how can you best communicate your intent to those who will read your code in the future, which might be yourself? The answer may be comments, better naming, more modular code, or a combination of these.

To comment or not to comment isn't the question. The question is: how can you best communicate your intent to those who will read your code in the future, which might be yourself? The answer may be comments, better naming, more modular code, or a combination of these.