views:

200

answers:

7

I'm working with a code base, which in places has code 10 years old. The code has undergone numerous additions, changes, editions, and refactorings without the removal of any code, or modules which are no longer required. As a result, the code base is littered with unnecessary code.

Have any of you come up with a comprehensive technique or product to audit your code base to find redundant unused code.

A: 

Hey there,

There are tools that help you identify dead code paths in your code and classes/methods that are unused. One of them is Project Analyzer for Visual Basic, but there are others as well. It really depends on what language you're using.

One thing you might also consider is revisiting your design documentation, if you have it. Evaluate what your product is being used for and all the use cases. Then, look at your UML/class diagrams and try to identify whether you have objects and helpers that are unnecessary.

This is also a really great time for you to contemplate architectural changes since you're going to be cutting a ton of code out of your code base anyway. Removal and re-architecture of spaghetti code can lead to huge performance and maintainability gains, and I commend you for taking on the audit.

Ed Altorfer
+5  A: 

At a guess, one way might be to run a complete set of tests, and to use a 'code coverage' tool to see what code isn't run.

I don't know PHP, but http://www.google.ca/search?hl=en&q=php+code+coverage+tools returns 300,000 hits including http://stackoverflow.com/questions/348850/code-coverage-tools-for-php

ChrisW
+2  A: 

Depending on what's available for the language the code base is written in, starting with some tool that uses static analysis to identify dead code might be a good start. Cross-check the results against the code carefully, to avoid being tripped up by reflection or other tricks.

I've known of one organization that ran their old production code for a time with profiling enabled to help them identify unused code. They bet on processor speed being faster, and memory larger, than when the application was originally fielded.

Dave W. Smith
+1  A: 

How about write a program that loads a source file independently. Using reflection and a function like get_declared_classes(), you should be able to generate a list of public methods, public static properties, and constants which could be accessed externally.

Using this list, you could then search text and find instances in code that reference each file. Put this in a spread sheet and look at which files are disconnected.

Here's more on reflection: http://nz.php.net/oop5.reflection

You could also build a graph and check for connectivity if you want to be really thorough: http://en.wikipedia.org/wiki/Connectivity_(graph_theory)

thesmart
A: 

Code coverage and profiling.

Code coverage should be able to tell you what code can theoretically be accessed, right down to telling you if particular branches of if statements are ever accessible (in theory), and profiling will tell you what practically IS accessed.

Profiling will also give you some insight into where the hot spots in the code is. If you know theres a big pile of garbage code, but its very hot, then you might approach with caution, if theres a big pile that only ever gets called once in a very specific way, you might be able to slice it out and replace it with a neat refactoring.

Its all a lot of hard work no matter what you do.

edit: It looks like your doing PHP. What you need is XDebug (an extension for php), WinCachegrind (KCacheGrind is remarkably better if you have access to a linux box) to read its output for profiling, and you'll need to find some sort of decent code coverage tool to use with the output of XDebug.

shazamzor
A: 

For Java code, Bill Pugh, the father of Findbugs, introduced some cool ideas on finding dead code through lightweight instrumentation (and of course some excellent ideas on static analysis) in a recent presentation at JavaOne

Jim Bird
A: 

See this SO answer (specific to Python, but in fact completely general): http://stackoverflow.com/questions/3883484/using-python-code-coverage-tool-for-understanding-and-pruning-back-source-code-of/3886403#3886403

Ira Baxter