views:

179

answers:

8

Over time I tend to create methods that are no longer useful to me. I think I have moved all my code away from them but maybe not. Or, maybe the code references the old method and everything works fine until I hit that special thing that made me create a new method.

Is there anyway besides just using find on each method, to see what is used where?

I'd like to delete my methods that aren't being used. Thanks.

I am using Visual Studio 2008 Express.

A: 

Right click method name in Visual Studio and select Find Usages? or Find Usages Advanced?

I'm just unsure if it's basic VS function or it comes from my ReSharper. But it's there at least for me :D

Paul G.
I believe this is a Resharper function. I don't think vanilla VS has this.
steve_c
+2  A: 

One way I sometimes do this is to leave the prototypes in the class definition, but then remove the implementations (surround them with #if 0 and #endif), then build your application. The linker will list any methods that it needs to find as well as where they are being called from. Any methods not listed are not called by anyone.

Note that this is not specific to Visual Studio, it works for any C/C++ project.

Graeme Perrow
+8  A: 

You can select "Find All References" in the context menu when you right click the name of the method.

El Cheicon
thank you for you help
johnny
+1  A: 

Simple, dumb method used by simple, dumb people like me:

Add XX to every method name, like FooXX, and compile.

When compiler says Foo is missing, remove the XX from Foo. Keep doing this until you get no more missing references.

Then remove any methods with XX in their name.

But the "find all references" method is better.

Mike Dunlavey
Hey - high-five !
Mike Dunlavey
+1  A: 

If you are using a managed language, it's not possible to determine all functions which will definately be used. You can do a "Find All References" for every function in your code and that will give you a high degree of confidence for every function that is explcitly and statically bound to in code.

However, reflection really throws a monkey wrench into the equation. It allows for functions to be bound to at runtime and there is no reliable way to search for this in code. The string which chooses the method name can even boe a user provided value.

C++ is really no better here. A method can be bound to non-statically by using pointer magic and offsets.

In short, no matter what language you're using, there is no fool proof way to do this.

JaredPar
+1  A: 

You can always comment them out and try to recompile. The compiler will let you know what's missing.

David Morton
+1  A: 

You could rename all methods (place an xxx in front of the name) and then try to recompile. For each method that is used, you'll get an error. Rename the associated method back to the correct name, and try compiling again.

After you've finished - all methods that are unused start with 'xxx'.

You'll need to do this a couple of times as You may have unused methods that call other unused methods.

seanyboy
good point about the circular references.
Mike Dunlavey
+1  A: 

Uncle Bob and crew have a good exercise to explain the Interface Segregation principle that does exactly what you need.

First, make a blank interface. Secondly, make your class in question implement said interface. Lastly, change the instances of your class to that of the interface. When compiling, it will show you what methods you need to implement, or in other words, what methods are actually being used.

Aaron Daniels