Four Things to Know when Writing Comments

Every developer has been learned from his teachers how important is to comment his source code. You should comment the classes, the methods, the logic, etc. However nobody explained how exactly to code with comments between the lines. Have you ever seen that
i++; // we increment i with one
That’s a shame! This doesn’t tell you anything, and what will happen if sometime later somebody sits in front of that code? He’d be amazed!
How To
In fact the task is not to determine what are the bad parts of not commenting the code, but how to overcome this. How to write comments. Here are some basic advices.
1. Think about the other developers
First of all the most important thing is to write comments with other programmers in mind. They should understand the logic of your code from your comments and not from the code itself.
2. Write in English
Even you’re not a native English speaker, and you English is not so good, it’s a must to write in English. This is really important when you work for a open source project when always there’s a big community around the project, you cannot be sure that everybody understands your language.
I know that this is difficult – also for me the English is not my native language, but that’s really a must. Once you start doing it, you’ll see that it’s not so difficult.
3. Comment the methods as a black box
Don’t write in a method comment what technique is used to achieve the result. It’s good to describe what the method does, not how it does it.
Bad example:
/**
* returns $a + $b
*/
public function sum($a, $b)
{
return $a + $b;
}
Better:
/**
* Returns the sum of two numbers
*
* @param number $a
* @param number $b
* @returns number $a + $b
*/
public function sum($a, $b)
{
return $a + $b;
}
4. Comment the logic, not the technical solution
When there’s a loop or a statement it’s useless to describe that this is a loop or statement, isn’t it? A better approach is to describe in a comment why you needed to implement this logic in such way.
Wrong:
...
$arr = array();
$i = 0;
// in a while loop we
// initialize every element with 0
while ($i < 100) {
$arr[$i++] = 0;
}
...
Better:
...
$arr = array();
$i = 0;
// we initialize an array of 0s
// to store the sorted elements
// on the first pass... etc, etc.
while ($i < 100) {
$arr[$i++] = 0;
}
...
Conclusion
In fact the most important thing is to write comments with other
developers in mind. Tell the future developer of that code what’s in the
code, and pray for mercy ![]()
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)






Comments
Ricky Clarkson replied on Tue, 2010/07/06 - 7:08am
Ronald Miura replied on Tue, 2010/07/06 - 7:15am
5. Think a little harder about whether you should write a comment, or make the code more understandable.
I'm not as radical against comments as some people but, most times (inline) comments do exist just because the code is crappy. Try to make your code cleaner before you write a comment.
Anyway, if you feel more confortable writing comments than refactoring code, maybe you should consider switching careers to documentation authoring ;)
PS.: javadoc comments for public methods are mostly welcome, unless they are
stupidtoo obvious:/** * Returns the name */ public String getName() { return name; }hint: IDE-generated comments are always bad, turn them off.Bino B. Manjasseril replied on Tue, 2010/07/06 - 9:07am
Josh Marotti replied on Tue, 2010/07/06 - 12:12pm
Jonathan Fisher replied on Tue, 2010/07/06 - 3:31pm
Jeroen Wenting replied on Wed, 2010/07/07 - 3:49am
Wrong. We're a company working purely on the local market, for local customers employing local people. Those customers demand we use our native language (which isn't English) for all communications, including documentation and comments.
I will prefer to use English for entity names because mixing native language with English in phrases (and code lines are phrases) just is weird, but javadocs are going to be native language by corporate policy unless the customer demands otherwise.
"Comment the logic, not the technical solution"
Sometimes the technical solution demands documentation in comments. And no, there isn't always a simpler solution available.
Philippe Lhoste replied on Wed, 2010/07/07 - 6:35am
I would be curious to see the code of those against the usage of comments in code!
Sometime, a line of comment explains what kind of little known API is used and what for: it avoid a lookup and an interupt in the reading. Sometime, we have to explain why we chose a given algorithm instead of another: it can avoid that another programmer breaks the code by replacing it with the rejected one. Sometime we have to explain why we do some things, because the frameworks acts in surprising ways... And so on.
For example:
I hate bad comments as well as other people, for example:
and I appreciate long variables/functions/classes names, but telling that code is always self-sufficient, even with doc comments (like JavaDoc) is a bit extremist.
Mikael Couzic replied on Wed, 2010/07/07 - 6:55am
Clean Code - Robert C. Martin
An absolute must read !
Geza Radics replied on Wed, 2010/07/07 - 10:38pm
Stoimen Popov replied on Thu, 2010/07/08 - 6:19am
in response to:
Ronald Miura
Gar Labs replied on Tue, 2011/08/23 - 10:37am
in response to:
Bino B. Manjasseril
This quote is good. That's the best quote I've ever read for Learning and Programming. -GAR Labs
Emma Watson replied on Fri, 2012/03/30 - 6:01am