I have an application at work, which I started work on about 7 years ago. This was my first engagement in ASP.NET web applications, and coming from a Win32 background I had a hard time adjusting.

All this time, both the project and myself have "matured" and now most parts of it are well defined, properly structured and readable. However, there is a significant piece of it, that is "old junk". By that I mean old obsolete code, that was not working well, or was not properly structured, which has been replaced, but left in the source files instead of being removed. I must note here, that I was not the sole developer, but did most of the coding throughout.

What I want to do, is whenever I have free time at work, to remove the old obsolete and unused code. Are there any techniques/methodologies that I can use/follow in order to do this more efficiently? Any tools perhaps?

EDIT: (clarification based on feedback) The code I am talking about is not used anywhere, and is not called from anywhere in the project. What I need is way to find out where the code resides, since we are talking about a large codebase. So something in terms of code coverage tools would help. Any suggestions for asp.net projects? (the particular case is in Web Site project form)

4 Answers 4


The static code analysis tools included with Visual Studio will find code "with no upstream callers". If you don't want to turn on all of the code analysis rules for your project, look for the following rules to turn on:

  • CA1811: Avoid uncalled private code (Performance Rules)
  • CA1812: Avoid uninstantiated internal classes (Performance Rules)
  • CA1801: Review unused parameters (Usage Rules)
  • CA1804: Remove unused locals (Performance Rules)

No idea why some are under the Performance category and others under Usage, but these are the rules I'm aware of to find unused code in VS.


Personally, if I think some code need not exist, I comment it out, then run the program to make sure it didn't make anything glitch. If everything is fine, I delete the commented-out code.

  • That's what I do currently, but it's not so efficient, because the application is quite large. Dec 21, 2010 at 15:33

I think removing dead code broadly falls under refactoring's definition of "process of changing a computer program's source code without modifying its external functional behavior in order to improve some of the nonfunctional attributes of the software".

There are some well-known books about refactoring.

Maybe a code coverage tool can be helpful in your special case.

  • It's not exactly refactoring, since I am talking about unused old code. Code that I want to wipe out completely, but I want a way to be more efficient in finding the code that is not called anywhere. Dec 21, 2010 at 15:34
  • Code coverage tools was what I was thinking of, and would appreciate any suggestions for asp.net projects, since I haven't used any so far. Dec 21, 2010 at 15:53
  • Ok, at first the question seemed quite general to me. Dec 21, 2010 at 15:56
  • It probably was ;-) Dec 21, 2010 at 16:07

In addition to FX Cop / VS's Code Analysis (they are the same thing, one is command line, one is in the IDE) as mentioned by others, NDepend can help. It will allow you to run queries to determine all callers and callees, right on up the chain. Think of it as letting you run reports on your code. It's not the be-all and end-all, but helps you to get several steps closer to where you want to be at the least.

The fundamental problem in turning to code analysis here is that public members are hard to conclusively say "not used" for a tool. I'm not familiar with a code analysis kit that has rules that say "This bunch of assemblies represent the whole ecosystem so if a method isn't called anywhere, let me know." I guess that once you get into derived types, that gets messy for an automated static code analysis engine.

  • Good idea, I forgot about NDepend!
    – Marcie
    Dec 21, 2010 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.