Posterous theme by Cory Watilo

Finding redundant code in Visual Studio solutions.

In the early days of the development of toepoke.co.uk I didn't have any proper source control management (yeah I know, I know !)

Anyway the days of commenting out code whilst I try stuff out and using my own source control management system are long gone (thankfully!).

However I still have a lot of commented out code hanging around from those days and I really need to get rid of it and to tidy things up. Thing is there's load of files and I didn't fancy going through every single file, so I figured there must be an easier way.

I figured typically I'm commenting out multiple lines of (C#) code, along the lines of this:



//public void SomeLazyCommentedOutMethod()
//{
//}


So all we need to do is search for //{ or // { or basically // followed by anything with { at some point afterwards. Sounds like a regular expression search to me. Ordinarily my ability to build a regular expression from scratch extends to this. But thankfully even I can cope this one!

(:b)*(//)+([a-zA-Z0-9])*\{

Which breaks down to:

  1. (:b)* = Zero or more tabs or spaces, followed by
  2. (//)+ = One or more slashes (/), followed by
  3. ([a-zA-Z0-9])* = Zero or more letters or numbers, followed by
  4. \{ = followed by an opening curly brace ({).

So if you bob (:b)*(//)+([a-zA-Z0-9])*\{ into the Visual Studio Find In Files dialog (CTRL + SHIFT + F) and hit Find All you should find most of the areas where you've commented out code.

C#

c# find in files dialog

VB.NET

vb.net find in files dialog

I reckon you should be able to the same in Visual Basic.NET by replacing the // with ' (so (:b)*(')+([a-zA-Z0-9])*\{ ) - however I've not tried it.

Well I've tried (the C# version) on the toepoke.co.uk code and found 158 hits which on the up side means it's probably working, on the downside I've been a naughty boy !

Regards,
Team TP!