Are comments always wrong?
A colleague asked me recently:
Why arenāt developers writing comments any more?
Heād been looking through some code his team had written, and couldnāt understand it ā he was looking for comments to make sense of the mess, but there were none. Before he challenged the team, he asked my opinion: should developers be writing comments?
Excessive Comments
When I started programming some years ago, I would commentĀ everything, and I meanĀ everything.
Ā Ā // Add four to x Ā Ā x += 4;
My logic at the time was that it was impossible to tell the difference between uncommented clear code and gnarly code you just hadnāt spotted the gnarliness in yet. So I would comment everything, where the absence of a comment meant Iād forgotten ā not that it was so trivial as to not warrant mentioning.
No Comments
Eventually I started working with peers who knocked some sense into me, and I immediately halved the number of lines of code I produced. At first it was a shock, but soon I realised that clear code is easier to readĀ if thereās just less noise. And this is all (most) comments become: noise. Sometimes theyāre accurate, sometimes theyāre not. But you canātĀ rely on them, you always have to assume they might be wrong. So if the code and the comment seem at odds, you assume itās the comment thatās wrong and not your understanding of the code (naturally youāre infallible!)
Clean Code
Uncle Bob and the notion of clean code have taken āno commentsā to an almost fanatical zeal. But every time I get into an argument with someone about how maybe this time a comment might be justified: Ctrl-Alt-M, enter your comment as the method name and it makes the code more obvious. Every. Damned. Time.
However, the trouble with a zealous argument like this is it gets taken up by asshats and lazy people. Itās too easy to say āif youād read Clean Coder youād know you donāt need comments. Quit living in the past, grandpa!ā. Uh huh. But your code is still a muddled pile of indecipherable crap. At least with comments thereād be some signposts while I wade through your steaming mess.
Some Comments
The truth is: sometimes commentsĀ do help (squeal clean code weenies, squeal!) There are some cases where extracting a method nameĀ isnāt sufficient. Sometimes having 20 one line methods in my class does not make it easier to read. The end goal is to produceĀ understandable code. Generally, naming things properly helps. Adding comments that get stale does not. But that doesnāt mean that writing crap codeĀ and not commenting is the answer. Donāt use āno commentsā as an excuse to leave your code indecipherable by human beings.
For example, sometimes you need to document why youĀ didnāt do something. Maybe thereās a standard way of converting between currencies in your application ā which this one time youāve deliberately not used. A comment here might help future people not refactor away your deliberate choice (even better is baking your decision into a design ā some class structure that makes itĀ really obvious). Sometimes a method name really doesnāt do a line of code justice, itās better to be seen in the context of the lines before and after it ā but it really needs some explanation of what youāre doing. This is particularly true when dealing with complex algorithms or mathematical formulae.
Getting the Balance Right
How do you get the balance right? Well, your goal is to make code that other people can understand. The best way to get feedback on that is toĀ ask someone. Either have explicit code reviews or pair program. If another human being has read your code andĀ theyĀ could understand it ā thereās a better than average chance that most other capable people will be able to read it too.



I always do a comments and it is good habbit as it is said by someone
“Everyone can write code that machine can understand, but great programmers write code that human can understand”