This may be better in the polling booth, but I'm curious what you all think about or have experienced (and I want to vent a little). First the question, then the venting.

Is it good or bad to leave commented-out code in production? Why?

I think it is bad. Why? In the code-maintenance phase, it's often difficult enough to figure out the intent of the executing code. The difficulty is compounded if you have to sort through code that was executing at one point in time.

I've seen this where I work, and it annoys, frustrates, sometimes confuses me, for the reason cited above. (If you ever work with me on any project an notice me doing this, slap me. :-)

Update: It looks like there are more votes for "it's bad" than for "it's good", which makes me :-). perrin, followed by others, mentioned one (and in my opinion, after considering it, the only) justifiable reason for leaving in commented-out code:

...leave it in commented out with a note saying "This looks like it should work, but it doesn't because XYZ." That could save other coders from duplicating this effort.

(My emphasis added.) I like dragonchild's idea even better: instead of commenting out blocks of code, remove the code, and put the algorithm that didn't work in the comment.

And now to possibly offend some of you, I don't agree with the "leave functionality commented out now that the users may ask for later" argument. My experience has been that by the time the user asks for the functionality I thought would "be cool" to have, what they're asking for is different enough to justify a re-write of what I was thinking the first time through. Sorry...

Still, the famous clause "it depends" applies in this case too. (That's been the ultimate answer to several of my posts. :-)

Replies are listed 'Best First'.
Re: Commented out production code?
by perrin (Chancellor) on Dec 12, 2001 at 23:46 UTC
    There is a legit reason for leaving some commented out code in: to keep others from making the same mistake! In other words, you try a performance hack that looks like it should work, it doesn't quite work, but you leave it in commented out with a note saying "This looks like it should work, but it doesn't because XYZ." That could save other coders from duplicating this effort.
(Ovid) Re: Commented out production code?
by Ovid (Cardinal) on Dec 12, 2001 at 23:32 UTC

    I think commented out code is a bad idea. Typically, it's done because a company doesn't have a proper version control system in place and commented out code makes it "easy" to back up to previous versions. Comments, in general, should be explaining why something is done, not how. If you have a particularly tricky section of code, commenting the how for that section may be appropriate. Commented out code fits neither reason and is just clutter.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Commented out production code?
by dws (Chancellor) on Dec 13, 2001 at 00:30 UTC
    In my book, it is a sin to comment out blocks of code by any of
    • #ifdef ... #endif or /* ... */
    • =foo ... =cut
    • if ( false ) { ... }
    Doing any of these can make maintenance a living hell. It becomes difficult to tell live code from dead, and grep (or equivalent) become an unreliable way to tell where a variable is referenced, or where a method is called.

    I'm O.K., though, with commenting out blocks of code via any of

    • //
    • #
    in the left margin, as long as there's a left-justified comment explaining why why the code has been commented out. (This causes the comment to stick out like a sore thumb, but that's the point. It's got to be immediately evident to whoever is reading the code.)

    Since commented-out blocks of code represent issues that need to be resolved, I've adopted the practice of using a special comment "tag" that's easy to extract mechanically (via, surprise, a Perl script!) so that we can generate reports of where our code issues are during development or maintenance. These tags look like:

    #TBD(dws) version 2.0 of mummble breaks if you do this # $mummble->do_this();
    The goal is to drive issues to zero at release, but sometimes timeboxes don't permit that. And lack of time is no excuse to lose track of where your issues are.

Re: Commented out production code?
by runrig (Abbot) on Dec 12, 2001 at 23:54 UTC
    IMHO, it depends. If you don't have some version control to rely on, it can provide extra documentation to someone. Let's take a trip to the real world: you have a long script which processes files in different loops in different ways, and some PHB says, don't 'blurk' the 'B' files, and you figure out that that means changing this:
    for $filename (@filelist) { ...
    to this:
    for $filename (@filelist) { # PHB doesn't want B files blurked next if $filename =~ /^b/i; ...
    A week later some other PHB is asking why the B-files aren't being blurked, and grudgingly gets the other PHB to agree that it should be changed back. Do you comment out that line, or delete it?

    Two weeks later they decide that they really do want the b-files blurked, and you're out on holiday or have quit to preserve you sanity, and some other programmer who is not too familiar with the code has to figure out what exactly needs to be changed. It may be a poor man's CVS (which is odd considering CVS is free), but that commented out line really helps him, and he buys you a beer when you get back. The trick, of course, lies in knowing what should be commented out and what should be deleted :-)

(kudra: not the epitome of evil) Re: Commented out production code?
by kudra (Vicar) on Dec 13, 2001 at 00:47 UTC
    Recently I wrote some code that included a comment along the lines of $foo = ... I commented it out because $foo was not actually being used in that section, and having it uncommented would (to me) suggest it is being used. I included it as a comment because I believe there's a good chance that future modifications will use $foo, and it was much easier to add the calculation for $foo while it was still fresh in my mind.

    Some may disagree, but I don't think what I did was so bad. Commenting out code is, in my opinion, like many other practices which are generally a bad idea but from time to time work.

    A better example came up last week at the Amsterdam.pm meeting, when the person giving the talk was asked why he had implemented a portion of his project in one way rather than another. The question was quickly answered when he displayed the code, which contained not only the code that was in production, but also two other ways of doing it along with explanations of why they didn't work.

    I doubt I'm the only one who has looked at something I wrote just the day before and wondered why I did it that way. In such a case, code comments can prevent the same mistakes from being repeated, either by someone else maintaining the code, or even by the original author with a faulty memory.

    Update: In this particular case, it wasn't algorithms which were tested, but rather system calls with different combinations of arguments. Because it was the syntax that was being explained, code seems a logical way to document. That's certainly common--just consider the pseudo-code often found in the synopsis of modules. Of course, the code still required text explaining what the problem with each approach was in order to be of any use.

      In that case, I wouldn't write out the code for the broken sections, but, instead, lay out the algorithms that didn't work and why they didn't work.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Commented out production code?
by perlmoth (Hermit) on Dec 12, 2001 at 23:48 UTC
    Once, doing maintenance, I had to find and fix a bug in some code. I spent ages sifting through a printout of the source trying to figure out how it worked and what was going wrong. It was only when I loaded the source into a syntax hilighting editor that I realised that a big chunk of the code I was trying to decipher had actually been commented out.

    Production code should not have any commented out sections. Production code should *always* go through some sort of version control system while it is being developed. This means that all changes made during development are recorded, but the poor maintainer does not have to see them unless he needs to.
Re: Commented out production code?
by dragonchild (Archbishop) on Dec 12, 2001 at 23:10 UTC
    The reason often given is to indicate what was planned or something like that.

    I personally think it's a sign of someone who couldn't make good decisions. (Either the programmer or the manager who didn't give the programmer enough time.) That's a problem.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Commented out production code?
by talexb (Chancellor) on Dec 13, 2001 at 02:06 UTC
    I'm in the process of finishing up a project that generates a PDF on the fly. There are various diagnostic bits for PDFs that I'd like to keep -- if I have to dig through the PDFLib docs again it'll take me hours as compared to minutes to do 'that thing' again.

    I also leave a few useful debug messages in suitable places (I always use __LINE__ to mark where they are), commented out, but ready if I have to go digging again.

    I may also leave old versions of statements lying around in case (this never happens, heh heh) the user changes their mind and wants to go back to the old way of doing things.

    Now if you're talking about code that's been rewritten, go ahead and delete it swhen it's not longer relevant.

    But if there's stuff that is/was relevant, comment it out and leave it.

    So, my vote is, 'keep that stuff!'

    "Excellent. Release the hounds." -- Monty Burns.
    T. Alex Beamish, TAB Software -- Toronto, Ontario

Re: Commented out production code?
by chip (Curate) on Dec 13, 2001 at 05:08 UTC
    Delete unused code. If you someday want to recover it, well, that's what source control is for! CVS is, sometimes, your friend.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      I would add to this:
      Delete unused code when it is released or goes into production.
      There's some very good, valid reasons for leaving in comments in non-production/unreleased code. I find myself trying different things, adding/moving code around, and I comment as I go. Sometimes, I'll have a 1.1 cut of test code sitting inside a 1.0 version as I play around with it.

      Your comment about using cvs rings true to me, and I'm a firm believer in it, but only again, in released code should you prune. If I'm working on a file, and it's going through several milestone stages working up to a release, I don't want to have to remove comments or commented code, check it in, then have others work on it, check it back out, copy back in the code from the first imported version, hack it up, take out the comments, commit it back, etc.

Re: Commented out production code?
by moodster (Hermit) on Dec 13, 2001 at 18:31 UTC
    In my not so humble opinion there's no excuse for leaving commented-out code in a release. During debugging you might want to comment-out areas of code to see what happens and that's fine. Then you might decide that you want to remove certain parts permanently but also that you should be able to restore the code if necessary. That's what version control is for. My rule of thumb is to NEVER check in anything which contains commented-out code, because that kind of stuff tends to build up over time.

    As an example, on one fairly complex C++ project I worked on , two of the other coders not only commented away code but also had little conversations in the comments, like this...

    ... for ( i = 0; i < max; i++ ) { doSomething(); // I really think this part should be removed. /Stefan doSomethingElse(); // No, it shouldn't. /Björn } ... and even further down... // This code is strange. I fear it.
    These guys shared the same room. Wouldn't it've been better if they'd talked to each other directly? :)

    Cheers,
    -- moodster

Re: Commented out production code?
by FoxtrotUniform (Prior) on Dec 13, 2001 at 22:44 UTC

    I just wrote some code that looks like this:

    my @logs = <LOGFILE>; chomp @logs; #XXX: map { &mung_log_entry($_) }, @logs;

    Now, this isn't really the same kind of "commented-out code in production" that we've been arguing about; it's more of a pseudocode comment to tell me that I've been thinking about munging up each log entry after reading them all in. In this case, I think that writing the comment as "code" is more legible (mostly in that it doesn't break up the flow of code on the page) than writing in a little note in prose:

    my @logs = <LOGFILE>; chomp @logs; # Mung up each log entry?

    Eh. Matter of opinion, I guess. I think that the "code" comment more accurately conveys what I was thinking at the moment (even if it is an Evil Use of map In A Void Context -- hey, I said it was pseudocode).

    Update:Yes, foreach would be clearer than map, but I've been thinking in terms of map lately.

    --
    :wq
      Wouldn't an inline foreach loop be clearer as a comment (particularly for whoever might come after) than the inline map?

      I know that I find it provides clearer documentation of intent. (Whether in a comment or in working code...)