1

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?

Dan McClain
  • 11,780
  • 9
  • 47
  • 67
  • possible duplicate of [Is there a tool for finding unreferenced functions (dead, obsolete code) in a C# app?](http://stackoverflow.com/questions/65585/is-there-a-tool-for-finding-unreferenced-functions-dead-obsolete-code-in-a-c) – nawfal Oct 12 '13 at 17:48

5 Answers5

8

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 () {}
GEOCHET
  • 21,119
  • 15
  • 74
  • 98
dance2die
  • 35,807
  • 39
  • 131
  • 194
  • I tend to turn on IsError for my own internal libs. For other cases, I leaeve it off – dance2die Mar 19 '09 at 17:56
  • 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 Mar 23 '09 at 12:12
  • @Svish: Turning on IsError seems to be obtrusive sometimes but sometimes when it's gotta go, it's gotta go. – dance2die Mar 23 '09 at 12:32
2

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

Nick Veys
  • 23,458
  • 4
  • 47
  • 64
0

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
  • 10,100
  • 8
  • 32
  • 30
0

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
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 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 Mar 19 '09 at 17:25
  • @Andrew I didn't say it was easy :). – JaredPar Mar 19 '09 at 17:33
  • @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. – dance2die Mar 20 '09 at 01:39
0

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
  • 47,594
  • 12
  • 108
  • 150