views:

317

answers:

6

I'm working with an inherited code base which contains thousands 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.

A: 

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.

duffymo
We're using Subversion so i'm not worried about "losing" anything. This code started its life many years ago as, wait for it, VB3, before version control systems were the norm. I am removing the crud manually but it's labor-intensive and so i'm looking for a quicker way. i'd like to move on and refactor "live" code.
Paul Sasik
Sounds like an education issue. Tell your team not to do this anymore, then start the hard slogging to clean up the mess.
duffymo
+4  A: 

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.

Hans Passant
Wouldn't this ultimately remove all desired comments too? How do you distinguish between old code and desired comments?
Frank V
This was meant to find and select commented code, pressing DEL is the OP's job.
Hans Passant
+1. This would be much faster and more user-controlled than my answer involving a macro.
p.campbell
This is very close to working. i just to tweak the regex a bit. i tried something like this: ^.*'.*\n.*'.*\n.*'.*\n.*'.*\n.*'.*\n.*'.*\n.*'.*\n.*'.*\n.*'.*$ ugly but sort of works. One drawback is that it also selects lines that have commnents suffixed at the end. Better match would be to detect newline + any white space + apostrophe + code
Paul Sasik
Agreed with nobugz. I just need to speed up the process. Let RegEx do the selection, i'll decide whether the selected code is worth keeping. i'll either press F3 to move on or Del.
Paul Sasik
"Keep the cat away from your keyboard"... hehehe
Javier Morillo
I think you'll have to consider XML commenting. Typically, XML comments are 3 or more lines long. I really don't know too much RegEx and I wanted to know if it can be modified to automatically check and skip XML comments.
Alex Essilfie
+2  A: 

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.

Frank V
"we use VSS" - I'm so sorry. It's the worst product on the market, hands down. Why would you do that when there are more capable alternatives available that cost nothing? But thank you for the support.
duffymo
+1. I've been in that situation too and I did the same. If some specific commented out code is annoying you, delete it. If it exists elsewhere, but it's not getting in your way right now, it's a waste of time to delete it. Be pragmatic and dont waste time tidying code that works and doesn't need to be changed or read.
MarkJ
@duffymo: I agree but it's beyond my control. (I've made suggestions) But, it's the company standard and it's a large company.
Frank V
@MarkJ: Thank you. I think you elaborated on my idea beautifully.
Frank V
A: 

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.
p.campbell
A: 

I wonder... does Resharper has some sort of functionality to find code like this?

Nick
Didn't see anything in ReSharper (running version 4.5) that would suggest this kind of cleanup. Though i am running with VB.NET which has more limited support than C#.
Paul Sasik
A: 
(^\s*//.*\n)^10

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

Commented out tests:

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