3

After using version control tools, it is no longer necessary to comment out old code. However, some team members still comment out old code. Therefore, I want to clean them up.

I want to mark commented lines which are really comments / documentation, so that every time I do not need to re-read all commented regions. The unmarked lines left are new commented code.

How do I achieve this? Any existing tools or need to write on my own?

Similar concept: in git, we have 'partial commit' to select some lines to commit. However, the lines marked in 'partial commit' is valid once only.

1
  • 6
    I guess asking those team members to clean up the mess themselves is not an option?
    – stijn
    Sep 11, 2012 at 12:01

4 Answers 4

18

Don't mark comments as comments, if you find commented out code while working in a file just delete it. Also try to get those that still insist on commenting out code to stop doing that, nothing is going to change unless you can stop people from commenting out blocks of bad code and not just deleting them. Then its just a simple matter of your team all working together to eliminate commented code when they find it, trying to make those that left commented code go back and clean it up is just a recipe for unnecessary drama.

2
  • 2
    I think the point, though, is the "if you find". It's going to be a manual looking-and-finding process. Hard to think there'll be any reliable way to programmatically parse for commented out code blocks.
    – Dan Ray
    Sep 11, 2012 at 12:39
  • 2
    The harm inflicted by commented-out code that no one stumbles across is very limited. The commented-out code that you DO stumble across, you just delete. The biggest drawback of this strategy, as I see it, is that you don't know when you are done.
    – Buhb
    Sep 11, 2012 at 16:56
6

Be careful, because documentation comments sometimes also contain code examples. You want to keep those. For example:

/**
  * This class does something.
  * Usage:
  * <code>
  * MyClass.doSomething();
  * </code>
  */

There is probably no error-proof method, but you could write a short Perl script that finds all comments in the code, keeps the documentation comments, and then makes a guess about the remaining ones using a few regular expressions for the beginning of the comment.

1

I think that the "nuke on sight" strategy suggested by @Ryathal is sound.

If you want to detect commented out code (please don't call it "commented code" because lots of people think that means code with comments!) then I think you are going to need to do this by examining the diffs between successive versions. Even then it is not straight forward because there is a variety of ways to comment out code ... depending on the programming language.

0

If you want an automated solution you could parse the comments to see if they are valid code. In general this would identify commented out code. Example code is one thing you have to be wary of accidentally deleting. Though you could check if there is a somewhat standard way of formatting them that you could use to identify or ignore them. E.g. they are in a header or function comment block, or marked in some other way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.