Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find unused classes in a .net project

I have a VS.NET 2008 project. Is it possible to check for classes that are not used anywere in the project? With FXcop I can find unused variables and unused code, but not unused classes.

like image 777
Ivo Avatar asked Mar 23 '10 14:03

Ivo


People also ask

How do I find unused methods in C#?

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.

Where are unused classes in Visual Studio code?

To find unused members with a Code Analysis Ruleset, from the Visual Studio menu select File -> New -> File… -> General -> Code Analysis Rule Set. Uncheck all the rules.

Where are unused files in Visual Studio?

There's a Visual Studio Extension that searches for unreferenced image files. It finds all image files in your project and then scan all aspx/cs/ashx/css/js files for references.


2 Answers

The tool NDepend can help find unused code in a .NET code base. Disclaimer: I am one of the developer of this tool.

To elaborate a bit, NDepend proposes to write Code Rule over LINQ Query (CQLinq). Around 200 default code rules are proposed, 3 of them being dedicated to unused/dead code detection

Basically such a rule to detect unused method for example looks like:

// <Name>Dead Methods</Name>
warnif count > 0 
from m in Application.Methods where !m.MethodsCallingMe.Any()
select m

NDepend rule to find unused methods (dead methods)

But this rule is naive and will return trivial false positives. There are many situations where a method is never called yet it is not unused (entry point, class constructor, finaliser...) this is why the 3 default rules are more elaborated:

  • Potentially dead Types (hence detect unused class, struct, interface, delegate...)
  • Potentially dead Methods
  • Potentially dead Fields

NDepend is integrated in Visual Studio, hence these rules can be checked/browsed/edited right inside the IDE. The tool can also be integrated into your CI process and it can build reports that will show rules violated and culprit code elements.

If you click these 3 links toward the source code of these rules, you'll see that the ones concerning types and methods are a bit complex. This is because they detect not only unused types and methods, but also types and methods used only by unused dead types and methods (recursive).

This is static analysis, hence the prefix Potentially in the rule names. If a code element is used only through reflection, these rules might consider it as unused which is not the case.

In addition to using these 3 rules, I'd advise measuring code coverage by tests and striving for having full coverage. Often, you'll see that code that cannot be covered by tests, is actually unused/dead code that can be safely discarded. This is especially useful in complex algorithms where it is not clear if a branch of code is reachable or not.

like image 62
Patrick from NDepend team Avatar answered Oct 18 '22 21:10

Patrick from NDepend team


Resharper (with solution-wide checking on) automatically notified you of unused classes in your project & solution

like image 27
thecoop Avatar answered Oct 18 '22 20:10

thecoop