This thought/rant recently arose whilst editing a large program.

How much trust should you have in comments?

The problem I had was easy to fix, but the code it was in had been copied from elsewhere along with embedded comments that no longer applied. So I spent a quiet half hour checking out comments throughout the code, only to find that the most applicable one was right at the start, describing the programs functionality (and even that was wrong - parameters in/out etc).

The 'cut and paste' culture of the development has spread inaccurate or irrelevant comments over the whole system, to the point now that they can/should be ignored. Descriptive naming of fields and functions will normally provide a far better clue as to the functionality than these comments.

It wouldn't be too bad but I've seen this happen time and again across lots of different development teams (and no doubt been responsible for a few as well). Problems arise when you pick up a piece of code to fix/change part of it. Following the logic flow is easy enough (usually), but for the most part that requires reading the code - not the comments.

 

And so this leads me to 4 levels of generalisation:

1. The code does exactly what the comment say's it does so look elsewhere for your bug.

    Good: Can skip through a routine quickly to the appropriate parts. Bad: Some very large assumptions are made.

2. Comments are there to help if the code, or the reason for the code isn't obvious.

    Good: You can ignore comments around most of the 'obvious' code. Bad: Still relying on them for difficult code.

3. Comments should be treated as a work of fiction until proved otherwise.

    Good: Might give you a hint - but you'll still have to read the code to check it. Bad: Now you have to read the code and a description.

4. Comments are the work of the devil, put there to fool you. Ignore them at every turn and rely solely on code. 

    Good: You'll always know exactly what's going on.  Bad: May take a long time when the description was right under your nose.

 

Breath in for 10, hold for 10, and breath out for 10.

Now I've calmed down about that, I'd like to share my thoughts on dealing with the little rascals. 

The only sensible approach that I've found is to apply each rule in turn and to treat comments as if they are code. Fix the broken ones, only put them in when it's necessary, and remove redundant ones. Oh, and to only apply these rules to the areas of code that I touch.

Remember - Comments are for life, not just Christmas.

Thanks for letting me get that off my chest.


'I think the problem lies in the fact that your data doesn't fit my program'.

edited: Wed Apr 23 13:58:30 2003 by jeffa - added readmore tag


In reply to No Comment by awkmonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.