views:

148

answers:

6

Is there a way to figure out if every function defined in code is called somewhere?

I have been doing a major code update to a large project of mine and I want to make sure the old functions that are no longer used are removed from the code.

Is there a better way then searching for each function in the solution?

A: 

Code coverage tool like NCover?

EDIT: This assumes you have sufficient tests for functionality and are determined to get rid of every unnecessary function. Delete-then-compile would work but isn't scalable.. regardless, the point is that you're going to want some kind of source analysis tool (either static or runtime analysis).

Andrew Coleson
A: 

Here's a way that will catch everything but reflection.

  1. Delete the method
  2. Compile

This seems a bit overkill but it has the advantage that you can "batch" queries by deleting multiple functions.

JaredPar
Order could matter here, though? If A() calls B() but nothing calls A(), deleting B() first will be misleading since it won't compile.
Andrew Coleson
@Andrew I didn't say it was easy :).
JaredPar
@JaredPar: I think, I have read about that process from "Working Effectively with Legacy Code" by Michael Feathers on how to refactor legacy code with no tests.
Sung Meister
+2  A: 

FxCop should be able to find orphaned/unused methods. I think static analysis is what you're looking for, not code coverage.

Nick Veys
A: 

Two suggestions:

  • Depending on your development tools, you may be able to generate warnings for functions that are declared but never called.

  • You may be able to generate a linker map, then compare its list of functions with a list you generate (with grep or ctags?) directly from your source.

Adam Liss
+2  A: 

See earlier question: What tools and techniques do you use to find dead code in .NET?

Alex B
+7  A: 

Mark each method you are trying to remove as Obsolete with IsError set to true. When you mark a method as such, you will get a compilation error and will be able to find out if you can safely remove the method.

    [Obsolete("Don't use this method", /* IsError */ true)]
    public void Foo () {}
Sung Meister
I tend to turn on IsError for my own internal libs. For other cases, I leaeve it off
Sung Meister
I do that too. And often leave them like that for a while, until I am absolutely certain I don't need them anymore. Unless I really am absolutely certain I don't need it anymore already of course :p
Svish
@Svish: Turning on IsError seems to be obtrusive sometimes but sometimes when it's gotta go, it's gotta go.
Sung Meister