473,651 Members | 2,510 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 12230
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.comw rote in message
news:Oy******** *****@TK2MSFTNG P05.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.comw rote:
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.comw rote:
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>.Get ReferencedAssem blies()'; 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 "GetMethodB ody" 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_o f_il_parser.htm l)

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.comw rote:
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.comw rote:
>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
1531
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 Smalltalk). But finding the most obvious ones would already be of some help.
4
3712
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 them) But this generates a warning (gcc4) saying it's an unused variable. Is there a better way to do this?
2
1712
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
2089
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 for those methods which are not called in any of my assemblies. 1) I want to remove all methods (either private,
9
3181
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
1729
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
1210
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
3360
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
6969
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 expected that records will be removed in the future, leaving more missing values. I would like to have a text box have as its default value the first unused entry in this table. I found a way to do this, but its not very efficient and requires an...
0
8347
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8694
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8457
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7294
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
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 we have to send another system
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.