views:

3358

answers:

10
+19  Q: 

Find unused code

I have to refactor a large C# Application, and i found a lot of functions that are never used in the app. Is there a tool that can check for unused code, so i can remove all the unused functions?

+1  A: 

ReSharper does a great job of finding unused code.

In the VS IDE, you can right click on the definition and choose 'Find All References', although this only works at the solution level.

Mitch Wheat
Given the age of this question and lack of comment, I can only assume the downvote is a 'punishment' one.
Mitch Wheat
A: 

I use Resharper and find it very helpful, not only for finding unused code.

gyrolf
A: 

You want a tool that generates a call tree.

Adam Liss
+10  A: 

It's a great question, but be warned that you're treading in dangerous waters here. When you're deleting code you will have to make sure you're compiling and testing often.

One great tool come to mind:

NDepend - this tool is just amazing. It takes a little while to grok, and after the first 10 minutes I think most developers just say "Screw it!" and delete the app. Once you get a good feel for NDepend, it gives you amazing insight to how your apps are coupled. Check it out: http://www.ndepend.com/. Most importantly, this tool will allow you to view methods which do not have any direct callers. It will also show you the inverse, a complete call tree for any method in the assembly (or even between assemblies).

Whatever tool you choose, it's not a task to take lightly. Especially if you're dealing with public methods on library type assemblies, as you may never know when an app is referencing them.

Jeff Schumacher
NDepend is great, but as you mention the learning curve is quite steep.
Mitch Wheat
@Mitch - thanks for the spelling correction. :)
Jeff Schumacher
Another word of caution, if your app is asp.net, with NDepend you will need to precompile your site so you can analyze the code-behinds and NDepend cannot cover/know about calls from the aspx pages (ie method calls in ObjectDataSources and the like)
Jaime
+3  A: 

Resharper is good for this like others have stated. Be careful though, these tools don't find you code that is used by reflection, e.g. cannot know if some code is NOT used by reflection.

mmiika
A: 

I could've sworn that FxCop does that sort of thing.

Patrick
@Patrick: what is that cat in your Gravatar doing?
Mitch Wheat
It's pushing a watermelon out of a lake. What did you think it was doing?
Patrick
You just broke my brain.
quillbreaker
A: 

FXCop is a code analyzer... It does much more than find unused code. I used FXCop for a while, and was so lost in its recommendations that I uninstalled it.

I think NDepend looks like a more likely candidate.

A: 

The truth is that the tool can never give you a 100% certain answer, but coverage tool can give you a pretty good run for the money.

If you count with comprehensive unit test suite, than you can use test coverage tool to see exactly what lines of code were not executed during the test run. You will still need to analyze the code manually: either eliminate what you consider dead code or write test to improve test coverage.

One such tool is NCover, with open source precursor on Sourceforge. Another alternative is PartCover.

Check out this answer on stackoverflow.

Dan
+3  A: 

Yes, ReSharper does this. Right click on your solution and selection "Find Code Issues". One of the results is "Unused Symbols". This will show you classes, methods, etc., that aren't used.

And you don't have to buy NDepend.

Jarrett Meyer
this is great. not enough people know about this. You have to turn on Solution Wide Analysis also to get everything to show up.
mcintyre321
A: 

As pointed Jeff the tool NDepend can help to find unused methods, fields and types. To elaborate a bit, this can be done thanks to the 3 following CQL (Code Query Language) queries. These 3 queries can be customized to your exact need. Notice also how these queries are prefixed with WARN IF Count > 0 IN, that transform them from CQL queries to CQL rules. These CQL rules can be checked in your CI environment, or even, interactively in VS itself while you are coding:

// <Name>Potentially unused methods</Name>
WARN IF Count > 0 IN SELECT METHODS WHERE
  MethodCa == 0 AND            // Ca=0 -> No Afferent Coupling -> The method is not used in the context of this application.
  !IsPublic AND                // Public methods might be used by client applications of your assemblies.
  !IsEntryPoint AND            // Main() method is not used by-design.
  !IsExplicitInterfaceImpl AND // The IL code never explicitely calls explicit interface methods implementation.
  !IsClassConstructor AND      // The IL code never explicitely calls class constructors.
  !IsFinalizer AND             // The IL code never explicitely calls finalizers.
  !IsVirtual AND
  !IsEventAdder AND
  !IsEventRemover 

// <Name>Potentially unused types</Name>
WARN IF Count > 0 IN SELECT TYPES WHERE  
 TypeCa == 0 AND // Ca=0 -> No Afferent Coupling -> The type
                 // is not used in the context of this application.
 !IsPublic  AND  // Public types might be used by client applications of
                 // your assemblies.   
 !NameIs "Program" 

// <Name>Potentially unused fields</Name>
WARN IF Count > 0 IN SELECT FIELDS 
WHERE 
 FieldCa == 0 AND  // Ca=0 -> No Afferent Coupling -> The field is not used in the context of this application.
 !IsPublic AND     // Although not recommended, public fields might be used by client applications of your assemblies.
 !IsLiteral AND    // The IL code never explicitely uses literal fields.
 !IsEnumValue AND  // The IL code never explicitely uses enumeration value.
 !NameIs "value__" // Field named 'value__' are relative to enumerations and the IL code never explicitely uses them."
Patrick Smacchia - NDepend dev