How to find unused methods?

  • Thread starter Thread starter Frank Rizzo
  • Start date Start date
F

Frank Rizzo

Is there a setting in VS2005 to quickly locate methods that are unused
(maybe through compiler warnings)? If not, any utilities out there that
do that?

Thanks
 
hmm,
Are you doing unit test and code coverage?
You have $$$ to burn, you can try the VS team's developer and tester (I do
not use it).
If no $$ to burn, google "unit test+code coverage+C#."
 
Is there a setting in VS2005 to quickly locate methods that are unused
(maybe through compiler warnings)? If not, any utilities out there that
do that?

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

Another alternative might be to use Reflector.Net and open all the
assemblies in your application, then use the Analize feature.
 
Is there a setting in VS2005 to quickly locate methods that are unused
(maybe through compiler warnings)? If not, any utilities out there that
do that?

Thanks

TestDriven.NET NCover should do it, but I've never used it.
 
I think some context is missing in this discussion. There isn't any
way to do what you want. And for good reason.

For example think about the case that you a couple of methods and
classes that are only called resp. instantiated by using reflection.
There's no telling which methods aren't used and which methods are in
general.

In the specific case that you have a "bounded application" with just a
couple of DLL's and executables you can try using code coverage or
other tests -- but that might prove to be problematic because not
every method has to be called during a test run.

So that leaves you just with one possible option: trying to figure out
the dependencies of a method by creating a program.

Perhaps I can help with a starting point. Let's assume you don't use
reflection. Then you can actually exploit reflection of .NET to find
assembly references:
1. use '<assembly>.GetReferencedAssemblies()'; push all those in a
dictionary and recurse.
2. compare the assemblies in the dictionary to the assemblies in the
directory. That leaves you with unused assemblies.

Furthermore you should notice that methods are usually called from
within classes. Ask every assembly for its types and iterate over all
methods. You will end up with a bunch of "MethodInfo" objects. Find
the "GetMethodBody" function and loop through the list of
"LocalVariables" and find the "LocalType" member of that. Combine that
with the parameter types, generic types, inheritance types and the
attribute types and you will get a list of all types that the method
uses (hope I'm not forgetting something...). Compare that to the types
you find in all assemblies. You will end up with the classes that
aren't used.

.... and to end up unleashing all hell I would try to parse the IL code
in the GetMethodBody function. After all, how hard can that be? :-)
(http://www.chiramattel.com/george/blog/2004/08/16/
first_version_of_il_parser.html)

Perhaps it's easier to just leave the unused methods to rot where they
were in the first place...

Cheers,
Stefan.

P.S.: if you decide to turn to code coverage you can always try (free
trial) ANTS profiler, (free) CLR profiler, or the likes. While it does
way more than you want, you can deduce most of the information you
seek there as well.
 
Is there a setting in VS2005 to quickly locate methods that are unused
(maybe through compiler warnings)? If not, any utilities out there that
do that?

I believe FxCop issues warning for unused methods, as well as unused
variables.
 
Hi Frank,

First, here's an answer from Eric Gunnerson on why C# doesn't warn about
unused methods:

#C# Frequently Asked Questions : Why doesn't C# warn about unused methods?
http://blogs.msdn.com/csharpfaq/archive/2004/03/19/93068.aspx


Second, Fxcop's rule "Avoid uncalled private code" also only checks on
private or internal (assembly-level) member that does not have callers in
the assembly, is not invoked by the common language runtime, and the member
is not invoked by a delegate. Also, there's another rule called "Avoid
uninstantiated intenral classes" may help. However, the rules can report
false positives if there are entry points that are not currently identified
by the rule logic.

For more information on Fxcop, please see
http://www.gotdotnet.com/Team/FxCop/

Hope this helps.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top