« Testing thread safety - updated | Main | TDD and Genetic Programming »

Dead code elimination

Listen to this articleListen to this article

I recently added jCoverage to the build of a "mostly" TDD project. The bits that weren't TDD are classes that we either "hacked" together for a quick-and-dirty proto-type (and that will ultimately be removed) and some that we used for domain modelling.

I like test coverage results, mainly because it gives me a warm fuzzy feeling. I can see how well or how poorly the developers are going WRT test coverage. But, I've often wondered if it has much benefit over and above the warm fuzzies.

It has occured to me many times that in many cases (especially on a TDD project) the coverage analysis really shows me potential areas of dead code. That is, code that almost never (if at all) gets executed. Especially on large projects with lots of re-factoring going on, it can be hard to keep track of which methods are no longer used.

IDEs such as IntelliJ IDEA and Eclipse and even Checkstyle and I think PMD can show you visually which private members aren't being used. The IDEs can also show you which public and protected methods aren't being used but you have to run the search manually. Granted, it is also possible to write some code that loads all your classes and builds dependency graphs to do the same. But why bother when I already have a tool that does it for me?

And so it came to pass that this morning I was looking over a coverage report and found a few areas where the coverage was a big fat zero. Intrigued, I opened the code in my IDE and did a search for all usages. What do you know. None. Zero. Nada. Bupkis. Zilch. You get the idea :-). I did this on the next few untested methods and I'd say that around 25% of the untested code was actually dead code!

Just for curiosity, I'd really like to put this into a live system and see which parts of the code aren't used. I'm sure some of it will turn out to be old code that handles scenarios that never eventuate anymore. Who knows? Worth a try at least?

Comments

Something to think about: maybe 25% of your tested code is also dead? It just doesn't pop up in jCoverage, just because it is called by testcases...

You can beat the "25% of tested code being dead" problem by focusing this on your acceptance tests. In theory, your acceptance tests represent business functionality that is actually desired. :)

Of course, there's a chance that there is "desired" functionality that's never used.

Or you can just do what Simon said at the end: put it into a live system. Consider an application running in a clustered environment, and imagine one node in the cluster running the monitoring tool (you probably wouldn't want to do it in a non-clustered environment because of the performance overhead). This would give you a very clear picture of what is and isn't used.

Tom,

"Just for curiosity, I'd really like to put this into a live system and see which parts of the code aren't used. I'm sure some of it will turn out to be old code that handles scenarios that never eventuate anymore. Who knows? Worth a try at least?" for exactly that reason.

Post a comment