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?
-
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).
-
Here's a way that will catch everything but reflection.
- Delete the method
- Compile
This seems a bit overkill but it has the advantage that you can "batch" queries by deleting multiple functions.
Andrew Coleson : Order could matter here, though? If A() calls B() but nothing calls A(), deleting B() first will be misleading since it won't compile.JaredPar : @Andrew I didn't say it was easy :).Sung Meister : @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. -
FxCop should be able to find orphaned/unused methods. I think static analysis is what you're looking for, not code coverage.
-
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
orctags
?) directly from your source.
-
See earlier question: What tools and techniques do you use to find dead code in .NET?
-
Mark each method you are trying to remove as Obsolete with
IsError
set totrue
. 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 offSvish : 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 :pSung Meister : @Svish: Turning on IsError seems to be obtrusive sometimes but sometimes when it's gotta go, it's gotta go.
0 comments:
Post a Comment