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

8

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.

Legator answered 10/3, 2010 at 22:14 Comment(3)
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?Round
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.Legator
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.Legator
B
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.

Bonaventure answered 10/3, 2010 at 22:27 Comment(8)
Wouldn't this ultimately remove all desired comments too? How do you distinguish between old code and desired comments?Shrimp
This was meant to find and select commented code, pressing DEL is the OP's job.Bonaventure
+1. This would be much faster and more user-controlled than my answer involving a macro.Inspan
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 + codeLegator
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.Legator
"Keep the cat away from your keyboard"... hehehePoetics
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.Nett
@Hans Passant ^.*'.*$ does not work in Virtual Studio 2017 indeed. Find and Replace finds nothing. Where am I wrong?Oeillade
N
5
(^\s*//.*\n)^10

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

Commented out tests:

\s*//\s*\[Test\].*\n
Nerland answered 16/8, 2010 at 10:13 Comment(4)
This helped me find a load of commented out code :)Expropriate
For some reason this no longer works for me, I have to use (^\s*//.*\n){10}Expropriate
@Expropriate : 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!Kootenay
What's \s*//\s*\[Test\].*\n used for?Oeillade
S
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.

Shrimp answered 10/3, 2010 at 22:28 Comment(4)
"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.Stupefacient
+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.Josephinajosephine
@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.Shrimp
@MarkJ: Thank you. I think you elaborated on my idea beautifully.Shrimp
C
1

MZ-tools has a facility to review dead code.

Cladophyll answered 20/4, 2011 at 9:42 Comment(0)
E
1

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

(^|[^/])//[^/]
Entrant answered 12/9, 2016 at 11:39 Comment(0)
S
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.

Stupefacient answered 10/3, 2010 at 22:18 Comment(2)
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.Legator
Sounds like an education issue. Tell your team not to do this anymore, then start the hard slogging to clean up the mess.Stupefacient
I
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.
Inspan answered 10/3, 2010 at 22:29 Comment(0)
S
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.

Spheroidal answered 23/5, 2017 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.