开发者

Quick and easy way to remove "dead" (commented out) code

I'm working with an inherited code base which contains thousan开发者_运维问答ds of lines of commented out code. I know the previous coder meant to save all his hard work for posterity rather than simply deleting it but: I will never read it and it just gets in the way. One problem example is that when I perform text searches for certain code segments I gets dozens of "false" hits in the commented code. PITA.

Is there a quick/easy way to detect large blocks of commented out code? A clever RegEx perhaps?

I happen to be working in VB.NET at this time and comment character is a single apostrophe.


You can use a Regular Expression search. Search for

^.*'.*$

To find a single line with a comment. You'll probably want to find at least 3 lines that start with a comment:

^.*'.*\n.*'.*\n.*'.*$

Keep the cat away from your keyboard.


(^\s*//.*\n)^10

Find 10 concurrent commented out lines of the // style.

Commented out tests:

\s*//\s*\[Test\].*\n


I'm afraid I agree with duffymo. I don't think you'll find a reliable automatic way to remove the commented out code. I'm sure if you search hard enough, you'll find one but your time would be better spent on your work.

I've been in this situation in the past (far too often) and what I end up doing is removing the commented out code as I work on various modules.

As an example, I open class Person to make a change and I see commented out code that has yet to be removed. I checkout the code (we use VSS), remove the bad code, check it in and finally, check it out to do my work.

It takes time before it all goes away, but I feel it is an effective use of time to resolve the issue.


MZ-tools has a facility to review dead code.


For any C# people who end up here, use the following regex to find commented out lines.

(^|[^/])//[^/]


That's what version control systems are for.

I'd make sure it was under version control (hopefully not Visual Source Safe), check it out, remove all the commented code, and check it back in.

I'd also discourage the practice in your development team for the future.


I'd suggest writing or finding a macro for Visual Studio that will help delete comments.

Some pseudo-logic:

  • start at a line number, read first character. remember this line number.
  • if is the VB comment character ', then continue
  • read next line's first character. if is comment character, continue.
  • when finding a line that isn't a comment character, analyze the number of lines traversed.
  • if the number of lines traversed matches your threshold of n, then delete them.


I know that this is an old thread but the trick that helps me hasn't been mentioned.

I search for this regex:

(^\s*'.=.\n)^

It finds commented out lines that contain an equals sign. My experience is that most blocks of commented out code contain at least one line that has an assignment and that equals signs are quite rare in real comments.

I don't automate the replacement, I just press F3 and if it matches a single line I just press the enter key to replace the highlighted text with a new line. If it lands on a line that is part of a block of commented out lines then I just manually select, press enter and the F3 again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜