9

G'day,

Inspired by a comment I made in my answer to this question on commenting Perl regexps I was wondering the following.

When you are working on older, old, bloody ancient, code do you update comments that are out of sync with the code or do you just update the code and ignore the comments that are already "miles out" from the reality of the current code?

I guess basically approaching things from the "broken window" syndrome?

BWS basically says that people just say "stuff it" and don't take care to fix errors that are there if they see that people, already involved with the system in question, didn't care enough to fix these sorts of things. A totally pervasive mind set IMHO!

I'd be interested to see what other people "at the coalface" do.

cheers,

Community
  • 1
  • 1
Rob Wells
  • 36,220
  • 13
  • 81
  • 146
  • should be community wiki – SilentGhost Jun 12 '09 at 21:51
  • I voted to close this because I don't understand the question at all. What is the possible benefit of leaving the comment alone? Of course everyone will try to keep them updated. – mqp Jun 12 '09 at 21:54
  • You assume that the reason the comments haven't been fixed is that people involved in the system don't care enough to fix them. I'd argue that comments that have longevity may be there for a reason--they are familiar enough to be used for navigating the file rather than for explaining the workings of the code. – Nosredna Jun 12 '09 at 21:56
  • @mquander: See my answer. The comments in very old files have become familiar enough to maintainers that they are used for purposes of navigation. It's not necessarily good practice, but it's quite common. – Nosredna Jun 12 '09 at 21:58
  • I agree with mquander, I voted this down, the answer seems obvious and not editing wrong comments would be irresponsible. @Nosredna I get your answer but I don't think "landmark comments" are necessary, if somebody is remembering a list of landmarks why aren't they remembering functions or organizing their code in a logical structure so functionality is where it should be and not splashed around the code. I think your answer is valid if your using an IDE that doens't have code navigation tools, functionality is spread amount files, there is no organization, and you don't have source control. – John Farrell Jun 12 '09 at 22:10
  • @jfar, the question specifically asks about ancient code. The programmers working on it could have their habits well-formed and work in vim or emacs. Why are you assuming an IDE? – Nosredna Jun 12 '09 at 22:14

14 Answers14

21

I will absolutely fix bad comments.

Sometimes it is just to delete them so that they don't mislead.

Better still, I prefer to refactor the code so the comments are not necessary - self documenting code is better than comments because it can't get out of date.

Chadwick
  • 12,555
  • 7
  • 49
  • 66
  • 1
    I second the option of deleting them. I also ruthlessly delete commented out and otherwise unused code. – Instance Hunter Jun 12 '09 at 22:16
  • 1
    Thirded. Having no comments is better than having wrong comments. It only takes one new hire to come to it afresh and fix the wrong one to really mess with everyone's heads, because then the code matches the comment and everything looks hunky dorey. – user60401 Jun 13 '09 at 00:24
  • 1
    I agree with what you're saying. However, I believe self-documenting code _can_ got out of date. Method and variable names can be wrong some times, too! – Sam Jun 01 '12 at 22:45
6

Recently I started following more of Uncle Bob's Clean Code advices and I'm trying to transform comments into functions that enclose (extract) the commented code in a function that is at least as meaningful as the comment it replaces.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
4

I always try to update them if I can, or at the very least add a note to the effect that they might be wrong. It's a worthwhile investment of a small amount of time.

The other thing I always do is add any relevant bug numbers, and what effect those edits had - that's always useful to see further down the line.

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
  • Generally you should use your version control system to record each bug fix and the associated edits that go with it. Fix one bug, test, then commit the change with a commit comment that includes the bug number and title. Lather, rinse, repeat. This way the code doesn't get cluttered up change history. But there will be a few cases where you do need to link the active code to bug history as part of the documentation. – Jim Ferrans Jun 13 '09 at 01:44
3

If the comment is necessary, I'll update it, otherwise I'll attempt to change the code to the point where I can just outright delete the comment and let the code document itself.

Brandon
  • 68,708
  • 30
  • 194
  • 223
2

When i find useless GhostDoc-generated comments, i sometimes just delete them:

What's the purpose of these comments?

/// <summary>
/// Saves the form properties.
/// </summary>
/// <param name="form">The form.</param>
/// <param name="propertyNames">The property names.</param>
void SaveFormProperties(Form form, string[] propertyNames);
Rauhotz
  • 7,914
  • 6
  • 40
  • 44
2

As a fairly fresh programmer, I don't really work much on older code as much as my own, but I try to go back every once in a while to ensure my own comments are mildly close to the truth. It makes no sense to leave comments that don't properly describe the function of the code.

That said if you're in a hurry it makes no sense to waste too much time updating comments. Adding one to the effect of "This is out of date" solves the problem of retaining navigation without leaving it ambiguous as to how accurate it is.

Jeff Bain
  • 337
  • 2
  • 8
1

I would fix the comments.

Why not fix any problem you can? If you understand the code you're working on, the comments should be the easiest fixes of all. Obviously if you've taken the trouble to delve into it the code should all be left better than you found it.

I would even posit that in groups where multiple people will be touching the code the comments could be considered just as important as the code itself. Without it the communication of ideas is broken down.

In practice I probably don't comment as well as I should. It's hard to admit that you or someone else will probably be digging around in your "masterpiece" later.

jjclarkson
  • 5,890
  • 6
  • 40
  • 62
1

If your documentation is being generated from the comments (I highly recommend this anyway) then yes, I keep the comments in meticulous synch with the code.

Jason Watts
  • 1,086
  • 5
  • 13
1

I immediately fix the comments if I see a problem, and probably note down what it would take to improve the code.

If I then get hit by a bus, the code is all the better for my quick, pointed attention.

Then, if I have time, I'll sort the code itself out (fixing it usually requires time consuming regression testing). If not, I'll leave it for another day, but at least I'll know what the problem is right away when I do have time to come back to it.

ChristianLinnell
  • 1,368
  • 2
  • 16
  • 22
1

Comments are oftten really useful for telling any future maintainers or programmers either what the code is doing or why it is doing it that way.

If you change the code without updating the comments, at best it's going to be confusing, if not downright misleading.

Andy
  • 3,794
  • 24
  • 28
  • 1
    @andy, totally agree with you in theory. Trouble is when you get a bleary eyed Ops engineer who's already been woken by three alerts since two in the morning updating the comments aswell as the associated code! We're all human mate! (-: – Rob Wells Jun 12 '09 at 21:32
  • Very good point, Rob. "In theory, theory and practice are the same thing". In practice it'll be nice to have code that works with any form of comment. – Andy Jun 12 '09 at 22:56
1

I strive to follow the Boy Scout Rule and leave the code cleaner than I found it. I will look to update the comment or refactor the code to obviate the need for the comment. I find that it is generally better not to have a comment at all than it is to have an incorrect comment.

Brandon E Taylor
  • 24,881
  • 6
  • 47
  • 71
1

If the project is under version control (and it should be nowadays, but you never know) and there's a huge hunk of out-of-date comments, I delete the hunk and leave a new comment to the effect that I've deleted a hunk of old comments that no longer seemed illuminating, and I submit with a note that I've deleted out-of-date comments.

Eventually, I'll remove the comment that mentions the deletion, or replace it with comments that apply to the new code.


However, here's the downside to changing old, supposedly meaningless comments in a system that's being maintained by a group of programmers:

The comments aren't acting as comments anymore! They are acting as landmarks for programmers familiar with the code. They're landmark comments as opposed to explanatory comments.

Programmers may actually be searching on keywords in landmark comments to navigate the file.

If you remove landmark comments, or even change them, you may drastically slow programmers who are using them to navigate the file. You're messing with the mental maps of people who have a long relationship with the code, and you'll do more damage than good. The brain's a funny thing. It may be much easier to remember a word or phrase in a funky comment than the name of a method.

It seems to me that if the comments are dreadfully out-of-date with the code, you should learn why. It seems incredibly presumptive to assume that the other programmers don't care about the code. Maybe they do, maybe they don't. If you're taking over the files from a guy who left, and you have clear ownership, dig in! If you're the new guy amongst a bunch of guys who've been working on the code for 20 years, and there's other evidence that they do care about the code, maybe you're missing something.

This is similar to the question of reformatting--changing spacing, altering where the opening braces go, etc. And much depends on ownership. Are you going to be in a file 20 times as much as the guy next to you? Or 1/20th as often? If it's the latter, do you really want to disorient him?

So make sure that's not what you're doing, or tomorrow you may hear someone yelling, "Where the hell is that function?"

Nosredna
  • 83,000
  • 15
  • 95
  • 122
  • @Nosredna, totally agree. That's why I get so frustrated with people who commit code to the SCM system after adding the new lines and commenting out the existing lines just in case! 8-O – Rob Wells Jun 12 '09 at 21:39
1

I would fix the comments. I saw some answers said they would rewrite the code if the comments were out of date. It seems absurd to me to rewrite working code just because the comments are bad. That kind behavior might even get you fired if your work is important enough.

user122302
  • 132
  • 5
  • I didn't see anyone say to rewrite the code if the comments are bad. I said that if the code is dense that's a better argument for refactoring the code than for rewriting the comments, but I didn't say you should do either. However, those in the Extreme Programming camp say to "Refactor Mercilessly. For them, improving code relentlessly is part of a programmer's job. – Nosredna Jun 12 '09 at 22:19
  • Chadwick and Brandon eluded to re-writing (refactor) the code. – user122302 Jun 15 '09 at 14:24
1

I always fix the comments - primarily because I am usually the one who works on a piece of code. It may be an OCD like thing for me, but I just like code that I am working on to look nice - good variable names, formatting (not an issue now with modern IDE's), and comments.

I don't believe code can be "refactored to the point it is self-documenting". It can be refactored to the point where only comments for functions, procedures, member variables, classes etc. are needed, because each function call is only 1-5 lines each. Coming from a Lisp background I like to program through decomposition - much simpler, testable, and easier to prove correct.

Larry Watanabe
  • 10,126
  • 9
  • 43
  • 46