views:

518

answers:

11

I talk about source-code-comments, that explains what happens on the next lines of code, not Javadoc/Doxygen-comments (that comment on the interface).

Comments can make your intention for the code clearer, but on the other hand they could be redundant or even worse outdated (code was changed without changing comment).

A: 

There was a very similar question raised this morning, maybe it's of interest to you: Comments in source code

Michael Barth
+10  A: 

Add comments where you need them to explain to another why you did something, the code itself should show clearly enough what you did. When Adding comments detailing what the code is doing is when you run the risk of redundant or outdated comments confusing readers of your code.

Robin
+3  A: 

I have a controversial opinion on this subject.

Ed Guiness
That is called experience (the hard way).
Gamecat
Not that controversial at all, I think. :-)
Mnementh
+2  A: 

If software is understandable without comment. You don't need it.

Comment are a way to increase clarity. But they need to focus on the information that can not be expressed by source like the "why".

Gamecat
+2  A: 

Good quality comment is always a good thing. Try seeing some old code of yours without any comments and time how fast you can determine what's going on.

Of course, comments are never a replacement for good quality code.

Miguel Ping
A: 

Source code comments unless meant to be machine collected are not a good idea.

When you write source code you tend to write comments, explaining the logic or something. Now, over a time your source code tend to be changed by multiple people and you end up having either latest source code or latest comments, but neither in sync. Of course, IMHO.

nils_gate
+2  A: 

MS did a study on comments.

They catagorized code into three;

  1. no comments
  2. average comments
  3. lots of comments

Second catagory - average comments - had the most bugs.

Turns out code with zero comments is written by people who know exactly what they're doing.

Code with lots of comment is written by people who fully understand what they're doing and explain it.

Code with average comments is written by people who don't really understand what they're doing.

Blank Xavier
Citation Needed?
jfar
Sorry - was at an MS lecture, some years ago now, the MS guy described the study.
Blank Xavier
A: 

there were many similar questions, just browse the comments tag. why asking again?

ax
I found many questions about comments, but not my question. Did you found a similar question? Can you point me to it?
Mnementh
http://stackoverflow.com/questions/750464/comments-in-source-code , http://stackoverflow.com/questions/209015/self-documenting-code , http://stackoverflow.com/questions/36432/commenting-code , http://stackoverflow.com/questions/142830/what-are-your-hard-rules-about-commenting-your-code , ...
ax
First ask for tools generating stuff, second for self-documenting-code (a complete different thing to explicit comments), third for best-practices regarding commenting at all (not only source-comments) and I really don't understand the fourth question. So no match here.
Mnementh
Please read better the questions, before saying this is a duplicate.
Mnementh
read the *answers* to those questions, and you get answers to your question, too. i did read your question. it just looks like a often-asked-often-answered-get-some-rep-fast-thing to me, sorry.
ax
A: 

It seems to me to be impossible to document why you do something if you do not say what you do first. what is especially important if you use a non-trivial algorithm. Of course you do need to explain why you use that specific algorithm (speed? accuracy? etc).

Even if the why is not important, the what can be. In my experience it is much faster to read 1 line of English comment than 20 lines of code, even if the code is very understandable.

Besides, if there is no comment explaining what a piece of code should do, how can you determine whether it is doing it correctly? Indeed you might have the problem of outdated comments, but then you can at least make sure that at one of them (comment or code) is incorrect (or both). W/o the comment, you have no clue to what your predecessor meant to do.

BlackShift
A: 
A: 

Where I currently work it is required that people comment every single method including the standard ones (like Page_Load) by copying the standard stuff from one file to another. To the chef it increases readability. To me it adds noise.

If you have nothing to say, don't comment it.

What would be the point of a useless commenting like this?

/// <summary>
/// Handles the standard PageLoad event
/// </summary>
/// <param name="sender">Event sender</param>
/// <param name="e">Event arguments</param>
protected void Page_Load(object sender, EventArgs e)
{
    // .........
}
User