Comments – Introduction

All of us have written comments for our code. Many times we have relied on comments to try to make sense of the code we’re seeing. As Uncle Bob puts it in his book, “comments are, at best, a necessary evil” and shouldn’t be used carelessly.

Why we need comments

To put it simply, we write comments in our code when we are unable to properly express our intent only using code. This can be due to the programming language’s limitations or our own.

Think about it, if all your classes and variables had clear names, all your functions names expressed what they did without any doubts, then your code would be so easy to understand that comments would just be an unnecessary redundancy.

Other times we’ll need to write something that can’t be expressed in code, or text that isn’t code but has to be present in the file for other reasons (like legal details ).

Writing Clean Code Is Better Than Writing Comments

Comments shouldn’t be used as a complement to bad code. If our code is complex, difficult to read and hard to understand, instead of spending time and energy writing comments, we should try to make our code cleaner.

In future posts, we will see when adding comments to our code can be a good thing, and when it can be a bad thing.

Leave a Reply