19

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.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • 1
    Are there any comments that you want to keep? Should the comment only be deleted if it looks like it contains code, and not if it contains ordinary text? – Mark Byers Mar 10 '10 at 22:17
  • That's the issue. There a few useful comments but they are in two, three or four lines tops. The useless, commented code tends to run on for dozens and hundreds of commented lines. – Paul Sasik Mar 10 '10 at 22:20
  • I think some clever regex could take care of this. Something that will find 10 or more contiguous lines that begin with a single apostrophe. If the regex selects the commented code for me i can then simply hit delete and move on to the next block. – Paul Sasik Mar 10 '10 at 22:25

8 Answers8

15

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
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    Wouldn't this ultimately remove all desired comments too? How do you distinguish between old code and desired comments? – Frank V Mar 10 '10 at 22:29
  • 5
    This was meant to find and select commented code, pressing DEL is the OP's job. – Hans Passant Mar 10 '10 at 22:31
  • 1
    +1. This would be much faster and more user-controlled than my answer involving a macro. – p.campbell Mar 10 '10 at 22:33
  • 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 Mar 10 '10 at 22:35
  • 1
    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 Mar 10 '10 at 22:36
  • "Keep the cat away from your keyboard"... hehehe – Javier Mar 11 '10 at 04:04
  • 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 Aug 16 '10 at 10:54
  • @Hans Passant `^.*'.*$` does not work in Virtual Studio 2017 indeed. `Find and Replace` finds nothing. Where am I wrong? – John Aug 03 '21 at 03:04
5
(^\s*//.*\n)^10

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

Commented out tests:

\s*//\s*\[Test\].*\n
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • 1
    This helped me find a load of commented out code :) – Ronnie Jul 16 '14 at 14:49
  • 5
    For some reason this no longer works for me, I have to use (^\s*//.*\n){10} – Ronnie Aug 11 '14 at 15:17
  • @Ronnie : thanks for this. I had to do as you mentioned in VS2010. It also took me a bit to realize this was fed to the "Find/Replace + the use regular expression check box". This is an awesome tip! – MikeJ Oct 21 '17 at 18:31
  • What's `\s*//\s*\[Test\].*\n` used for? – John Aug 02 '21 at 12:31
4

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
  • 25,141
  • 34
  • 106
  • 144
  • 1
    "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 Mar 10 '10 at 23:15
  • 1
    +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 Mar 11 '10 at 06:58
  • @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 Mar 11 '10 at 15:51
  • @MarkJ: Thank you. I think you elaborated on my idea beautifully. – Frank V Mar 11 '10 at 15:51
1

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

(^|[^/])//[^/]
Taran
  • 12,822
  • 3
  • 43
  • 47
1

MZ-tools has a facility to review dead code.

Urbycoz
  • 7,247
  • 20
  • 70
  • 108
0

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
  • 305,152
  • 44
  • 369
  • 561
  • 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 Mar 10 '10 at 22:23
  • Sounds like an education issue. Tell your team not to do this anymore, then start the hard slogging to clean up the mess. – duffymo Mar 11 '10 at 00:08
0

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
  • 98,673
  • 67
  • 256
  • 322
0

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.

Kevin Whitefoot
  • 414
  • 3
  • 15