19

I need a tool I can run that will show me a list of unused methods, variables, properties, and classes. CSS classes would be an added bonus.

I heard FXCop can do this? or NDepend or something?

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
PositiveGuy
  • 46,620
  • 110
  • 305
  • 471
  • Not quite a duplicate since you want CSS too, but: http://stackoverflow.com/questions/65585/is-there-a-tool-for-finding-unreferenced-functions-dead-obsolete-code-in-a-c – user7116 Nov 10 '09 at 15:49
  • And I don't want to have it change things across the board. Basically I want a list of possible issues where I can pick one by one. I can't just go fixing the entire application, my boss would kill me – PositiveGuy Nov 10 '09 at 16:01
  • I would like to know if NDepend gives you a nice list first before you apply any changes to code via code analysis. Anyone know off the top of their head that uses it? – PositiveGuy Nov 10 '09 at 16:12
  • NDepend makes no changes, it just provides data. – user7116 Nov 10 '09 at 16:45
  • This was asked over a year and a half ago. –  Jun 10 '11 at 14:47

5 Answers5

20

Look at ReSharper.

Matt Grande
  • 11,964
  • 6
  • 62
  • 89
  • 3
    +1 Definitely R#, and see their function "Code cleanup" for finding all unused code. – David Hedlund Nov 10 '09 at 15:43
  • When you're editing in R# all grayed-out methods/fields/properties are unused. – Nathan Taylor Nov 10 '09 at 15:48
  • does code cleanup automatically change the files without your permission or does it give a nice list first? – PositiveGuy Nov 10 '09 at 15:50
  • well, I've got a color scheme that might override this. I don't see greyed out methods and I am using R# – PositiveGuy Nov 10 '09 at 15:50
  • 1
    Coffeeaddict - Code Cleanup only performs **non-breaking** changes. It will not delete any unused methods or otherwise, but it will delete extra using statements, fix formatting, convert to auto properties and more. This is all customizable, but no, it doesn't confirm before it performs tasks, however, it doesn't save automatically so you can always undo things. – Nathan Taylor Nov 10 '09 at 15:53
  • yea, I don't want to have it change things across the board. Basically I want a list of possible issues where I can pick one by one. I can't just go fixing the entire application, my boss would kill me. – PositiveGuy Nov 10 '09 at 16:01
  • 2
    You can always perform "cleanup" on a single file at a time. – Nathan Taylor Nov 10 '09 at 16:02
8

Code Analysis in VSTS will generate warnings about this during the build process. You can set it up to treat Warnings As Errors.

Winston Smith
  • 21,585
  • 10
  • 60
  • 75
5

You can use ReSharper to find unused code and Dust-Me Selectors to find unused CSS.

Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
5

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

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:

NDepend is integrated in Visual Studio, thus 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 above 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.

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
2

Gendarme has also different rules to find unused code.

Bobby
  • 11,419
  • 5
  • 44
  • 69
  • I'd never heard of this one before, so I thought I'd give it a download... It looks pretty nice! – Matt Grande Nov 10 '09 at 20:28
  • 3
    I've used Gendarme. It's great at finding unused *variables*, but doesn't really help with unused methods or classes. – dan04 Jun 04 '11 at 21:42