473,503 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to find unused methods?

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
Apr 20 '07 #1
8 12217
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#."

--
cheers,
RL
"Frank Rizzo" <no**@none.comwrote in message
news:Oy*************@TK2MSFTNGP05.phx.gbl...
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

Apr 20 '07 #2
On Apr 20, 1:56 pm, Frank Rizzo <n...@none.comwrote:
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.

Apr 20 '07 #3
On Apr 20, 1:56 pm, Frank Rizzo <n...@none.comwrote:
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.

Apr 20 '07 #4
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.

On 20 apr, 19:56, Frank Rizzo <n...@none.comwrote:
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

Apr 20 '07 #5
On Fri, 20 Apr 2007 10:56:09 -0700, Frank Rizzo <no**@none.comwrote:
>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.
--
http://www.kynosarges.de
Apr 21 '07 #6
MZ-Tools (http://www.mztools.com/index.htm) add-in can do it. Use
"Review Dead Code..." functionality. This will also show you unused
variables and exceptions.

--
Peter Macej
Helixoft - http://www.helixoft.com
VSdocman - Commenter and generator of class documentation for C#, VB
..NET and ASP .NET code
Apr 22 '07 #7
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/arch.../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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Apr 23 '07 #8
Peter Macej wrote:
MZ-Tools (http://www.mztools.com/index.htm) add-in can do it. Use
"Review Dead Code..." functionality. This will also show you unused
variables and exceptions.
Thanks. I actually have a license and it's installed in my VS2005
instance, just didn't notice the feature.

Regards
Apr 23 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1513
by: Martin Drautzburg | last post by:
Is there an elegant way for finding unsent methods as in Smalltalk ? I am aware of the fact that due to pythons dynamic typing, no tool in the world can find ALL unsent methods (same in...
4
3699
by: artooro | last post by:
Hi, I'm using some lines of code like this: ClassName *var = new ClassName(...); and that's it. The class constructor does everything I want (there are methods to do more but I don't need...
2
1707
by: Venkat | last post by:
Hi, My application is splitted into more than one assembly. And I want to clean up the methods which are not called or not used among any of the assemblies.
5
2088
by: Venkat | last post by:
Hi, My application is splitted into more than one assembly. And I want to clean up the methods which are not called or not used among any of the assemblies. And also I want to remove the code...
9
3171
by: ThazKool | last post by:
I am just wondering if all the methods that are never called upon during a programs execution are removed when optimizing. For example you have: class FooA { void a(); ... void z(); }
9
1727
by: Martijn Mulder | last post by:
A nice feature of the csc.exe compiler is that it warns you if a variable is declared but never used. But unused methods are not flagged, even when the warning level is set to 4.
0
1200
by: Fabian Wein | last post by:
Hi, I hope this is not too off-topic! Is there a free tool that scans for unused methods? Maybe a (gnu) linker option? Thanks, Fabian
2
3351
by: Ming | last post by:
Hi folks, Is there a handy tool to find which methods are not used at all in a php project? Thanks,
6
6944
by: nickvans | last post by:
Hi all, I have a table called tblRecords that has "DashNum" as its primary key. The lowest value of this table is 116 and the highest value is 269, though there are some missing values. It is...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.