Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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)

share|improve this question
add comment

4 Answers 4

up vote 2 down vote accepted

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.

share|improve this answer
    
That helped a lot! Thanks! –  Nikos Steiakakis Dec 22 '10 at 8:55
add comment

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.

share|improve this answer
    
That's what I do currently, but it's not so efficient, because the application is quite large. –  Nikos Steiakakis Dec 21 '10 at 15:33
add comment

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.

share|improve this answer
    
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. –  Nikos Steiakakis Dec 21 '10 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. –  Nikos Steiakakis Dec 21 '10 at 15:53
    
Ok, at first the question seemed quite general to me. –  LennyProgrammers Dec 21 '10 at 15:56
    
It probably was ;-) –  Nikos Steiakakis Dec 21 '10 at 16:07
add comment

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.

share|improve this answer
    
Good idea, I forgot about NDepend! –  Marcie Dec 21 '10 at 18:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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