0

Is it possible to find all the unused method in a solution?

I.e.

Private method not called inside the class or method which are called from methods which are not called

Revious
  • 7,816
  • 31
  • 98
  • 147
  • It must be possible because Resharper seems to be able to do it. – Joel Etherton Mar 13 '14 at 16:03
  • @Revious: I'm not sure if that's how Resharper does it, but for private methods I guess you can just look for it's name in the file (or more in case of a partial class). If you can't find it except in the declaration, the method is obviously unused. Note that this is not a 100% guarantee since everything can be called using reflection. – CKII Mar 13 '14 at 16:12
  • I remember seeing a Microsoft video of test and it would show any code the test did not hit but I cannot find the reference. Search test. – paparazzo Mar 13 '14 at 16:18
  • @Revious ReSharper likely does it using the same mechanism by which it finds usages. I'm not sure, but I think it has limited support for detecting dynamic uses, but I'm not sure if that also covers Reflection. It isn't always safe to remove a method that *looks* like it isn't used ;-) – Adam Houldsworth Mar 13 '14 at 16:36

2 Answers2

1

You can use static code analysis for this task if you have access to a Visual Studio Pro (or above). The relevant rule is CA1811. Please note that this method gives you a good starting point but might report false positives, so you should be cautious when you remove methods.

See this link for an overview of static code analysis.

Markus
  • 20,838
  • 4
  • 31
  • 55
0

It's also possible to use NDepend following this article:

http://www.ndepend.com/DefaultRules/webframe.html?Q_Potentially_dead_Methods.html

Revious
  • 7,816
  • 31
  • 98
  • 147