steffen9349_man_working_on_computer_not_clean_frustruated_caric_a1ab5046-04f1-46a1-9473-29701ad700ed (Custom).png

Code comments have long been regarded as a necessary evil in the world of software development. They serve as a way to explain complex code or provide context for future developers (or even our future selves). However, I believe that if you have the need to add code comments, there may be something fundamentally wrong with the code itself. In this blog post, we'll explore why I consider code comments to be a code smell and how writing clean and self-explanatory code can eliminate the need for excessive commenting.

The Problem with Code Comments

Code comments, when used sparingly and effectively, can be helpful. They can provide insights into the intent behind certain lines of code or clarify complex algorithms. But more often than not, they become a crutch for poorly written or convoluted code. Here are a few reasons why I think code comments should be minimized:

1. Code Should Be Self-Explanatory

Good code should read like well-written prose - it should be clear and easy to understand without the need for additional explanations. When we write clean and self-explanatory code, it becomes easier for other developers (and ourselves) to follow along and make modifications if needed.

2. Comments Can Be Misleading or Outdated

One major issue with code comments is that they are not always maintained alongside the actual codebase. As the code evolves over time, comments may become outdated or even misleading. This can lead to confusion and introduce bugs when developers rely on outdated information.

3. Comments Can Hide Bad Code

Sometimes, we use comments as a way to justify or excuse poorly written or overly complex code. Instead of taking the time to refactor and improve the structure of our code, we slap on a comment to explain what's going on. This creates technical debt and makes it harder for others (and ourselves) to maintain and understand the codebase.

The Solution: Writing Clean and Readable Code

So, if code comments are not the answer, what is? The solution lies in writing clean and readable code that tells a story. Here are a few tips to help you achieve this:

1. Use Meaningful Names

One of the most important aspects of writing clean code is using meaningful and descriptive names for variables, functions, and classes. By choosing names that clearly convey their purpose or functionality, you can significantly reduce the need for comments.

2. Break Down Complex Logic

Complex logic can quickly become hard to follow, even with comments. Instead of relying on comments to explain convoluted code, focus on breaking down complex logic into smaller, more manageable pieces (ie. methods, classes). Each piece should have a clear purpose and be easy to understand without additional explanations.

3. Embrace Design Patterns and Best Practices

Design patterns and best practices exist for a reason - they help us write cleaner and more maintainable code. By following established patterns and best practices in your codebase, you can make it easier for other developers (and yourself) to understand the code without relying on comments.

4. Write Self-Documenting Code

Self-documenting code is code that is so clear and well-structured that it doesn't need additional comments to explain what it does. By structuring your code in a way that follows established conventions and standards, you can make it easier for others to understand the logic behind it.

5. Classes should do one thing only, and do it well

If a class needs comments it could be a code smell that this class is doing too much or is doing it in a inappropriate way. In the world of programming and software development, it is widely accepted that classes should adhere to a fundamental principle: they should have a single responsibility and excel at performing that task.  By following this principle, code becomes more maintainable, reusable, and easier to understand. When a class is focused on one specific aspect or functionality, it becomes less prone to bugs and complications. Moreover, this approach promotes modularity and allows for more flexible designs. By ensuring that classes do one thing only and do it well, developers can create robust systems that are scalable and efficient.

Conclusion

While there may be some cases where code comments are necessary or helpful, I believe that they should be used very sparingly. Relying too heavily on comments can indicate underlying issues with the code itself - it may be poorly structured or lack clarity. By focusing on writing clean and self-explanatory code, we can eliminate the need for excessive commenting and create a more maintainable and understandable codebase.

So, the next time you find yourself writing a comment, take a step back and ask yourself if there's a way to improve the code itself. Remember, code should be like a well-written story - it should tell a clear and concise tale without the need for additional explanations.