views:

274

answers:

14

And if so, where do you draw the line? My coworkers and I disagree on this subject. I have seen such things as

// fixes bug # 22

to

// fixed bug: shouldnt be decrementing
i++;

Is it ok if the change is fairly significant, and radically changes what the method was written to do? Or do you simply change the summary text of the method to reflect what it is now meant to do?

My opinion is that this information should be put into source control. Some state that this is bad because then it will be lost outside of the context of source control (say you switch systems and want to keep historical data).

+4  A: 

We had a few comments like this, but then our Bugzilla server died and we restarted at bug #1 so they're all meaningless. A short explanation of the bug is my preferred method now.

jeffamaphone
+31  A: 

Comments should explain how the methods work.

Source control explains why changes were made.

sh-beta
My thoughts exactly.
Frederik Gheysels
Couldn't agree more.
Davie
What was said here.
Vladimir Kocjancic
A: 

Assuming comments aren't superfluous (the classic i++; //increment i example comes to mind), there is almost never a reason to argue against adding a comment, regardless of what it's related to. Information is useful. However, it's best to be descriptive and concise - don't say "fixes bug #YY", but instead add something like "this used to fail for x=0, the extra logic here prevents that". That way someone who looks at the code later can understand why a particular section is critical to proper function.

Amber
... but too much information is use*less* and just adds noise.
David Lively
Hence the "assuming the comments aren't superfluous" introduction. The comment should still be relevant to the code, but just because a bug is no longer present doesn't meant warning others from making the mistake again isn't useful.
Amber
Rather than referring to a past bug, would it not be better to say (for this example), "this prevents a failure when x=0".
Steve Melnikoff
I wouldn't say it's necessarily "better", Steve - the core item of information is present either way; and both statements imply that such is a case to which attention should be paid.
Amber
+6  A: 

No. You should keep information on bugs and the change set that fixes the bug external to the source code. Any comments in the code itself should only relate to what the code is doing. Anything else is just clutter.

tvanfosson
+6  A: 

I personally feel that comments should be about the code itself, not about a bug fix.

My reason for this is maintainability - 2 (or 10) years later, this comment will no longer be meaningful. In your example above, I would prefer something like:

// Increment i to counteract extra decrement
++i;

The difference is that it's not tied to a bug, but rather what the code is doing. Comments should be commenting on the code, not meta info, IMO.

This opinion is partially because I maintain a very old codebase - and we have lots of comments that are no longer meaningful related to bug fixes or feature enhancement requests, etc....

Reed Copsey
A: 

I rely on FogBugz and check-in comments in svn. Works great, though as jeffamaphone said case numbers don't make a lot of sense if you lose your bug database.

A problem with putting comments in the code is that, over time, your code will become littered with comments about problems that haven't existed for awhile. By placing such comments in the source control check-in comments you're effectively tying information about the fix to the specific version where it was corrected, which can be helpful later on.

David Lively
A: 

My view is that comments should be relevant to the developer's intention, or highlights of 'why' surrounding the algorithm/method.

Comments shouldn't surround a fix-in-time.

p.campbell
+23  A: 

Adding a comment about bug fixing is a good idea, if you write the right thing.

For example,

/* I know this looks wrong, but originally foo was being decremented here, and 
   it caused the baz to sproing. Remember, the logic is negated by blort! */

Stuff like Fixes bug #22 is better kept in source control. Comments in your code should be signposts to help future sojourners on their way, not satisfy process and tracking.

erickson
+1. A idealist would say something like "Comments should explain how the methods work. Source control explains why changes were made." This ignores the fact that when you're reading complicated code, it helps to know why it's doing something nonobvious, and a mention of the case that influenced how it is written is as good a way as any to explain this.
Edmund
Well that example comment is explaining how the method works. with a little historical context thrown in.
Martin
Agreed, once in a while you have to put something in that explains why you're doing something that would look stupid most of the time.
jcollum
A: 

I agree that such data should be placed in source control or another part Configuration Management. Having worked in codebases that place information about bug fixes in comments, I can say it leads to very cluttered comments and code later. Six months after the fix is in place, do you really want to know that about a line fixing some long-past bug? What do you do with comments when you need to refactor the code?

Andrew
+2  A: 

Something like // fixes bug # 22 is quite meaningless on its own, and requires supplementary steps to even get an idea about what it means and what role it fulfills. A short description is in my opinion more appropriate, regardless of the bug tracking or source control software you might be using.

luvieere
+1  A: 

If the algorithm needs to be coded in a certain way - to workaround a bug in a 3rd party API for example, then that should be commented in the code so that the next person that comes along doesn't try to "optimise" the code (or whatever) and reintroduce a problem.

If this involves adding a comment when you fix the original bug then do it.

It will also serve as a marker so you can find the code you need to check if ever you upgrade to the next version of the API.

ChrisF
A: 

We use Team Foundation Server for source control here at my company and it lets you tie a check-in to a bug report, so I wouldn't put a comment directly in code to server the same purpose.

However, in situations where I'm implementing code as a workaround for a bug in the .NET framework or a third-party library I like to put a the URL to the Microsoft TechNet log or website that describes the bug and its status.

Rob Sobers
Right--a link to an *open* bug is something different.
Jason Orendorff
A: 

So obviously

    // fix bug #22
    i++;

is not effective communication.

Good communication is mostly common sense. Say what you mean.

    // Compensate for removeFrob() decrementing i.
    i++;

Include the bug number if it seems likely to help future readers.

    // Skipping the next flange is necessary to maintain the loop
    // invariant if the lookup fails (bug #22).
    i++;

Sometimes important conversations are recorded in your bug tracking system. Sometimes a bug leads to a key insight that changes the shape of the code.

    // Treat this as a bleet. Misnomed grotzjammers and particle
    // bleets are actually both special cases of the same
    // situation; see Anna's analysis in bug #22.
    i++;
Jason Orendorff
Another take: bug numbers are hyperlinks. Current code and old bugs often aren't related, but sometimes they are and a link is appropriate.
Jason Orendorff
A: 

In the Perl5 source repository it is common to refer to bugs, with their associated Trac number in Test files.

This makes more sense to me, because adding a test for a bug will prevent that bug from ever going unnoticed again.

Brad Gilbert