Wednesday, 12 November 2008

Dead code finder

One thing that I would love to have is a tool that will run in the background that will look for dead code. It would separate code that 1) is never used and 2) code that is only used by tests. There is a plugin for eclipse and PMD which claim to remove dead code, but both only talk about class members and parameters and private methods. Eclipse does that already if you crank up the warnings.

The original problem might not be as easy as you'd think at first though... some code like serialVersionUID is only used by the JVM, other code might be not called like private constructors, other code might be called via reflection.

Perhaps this could highlight code that is never references AND is never hit by tests. Either way it would be a nice fast way to cut down the LOC of a project. It would be awesome to take a project and instantly burn off all the cruft.

One can dream.

2 comments:

  1. It would be an interesting experiment to automatically "monkey test" a project while it is profiled, so you can see which methods are hit (and how often) and which are not. Then you can look at the methods that aren't hit and maybe remove them.

    ReplyDelete
  2. I know for the .Net environment, there is FXCop which will do static code analysis to tell you which methods or properties aren't being used so that you can remove them. However, trying to do this on a running system would be quite problematic. Anybody who has run a profiler has probably experienced major slowdowns in the system while running the profiler.
    Personally, my point of view on this becomes, if it ain't broke, don't fix it. The minute you remove code that never runs from the system, somebody will create exactly the right set of circumstances necessary to have that code run, and basically find bugs. Except in the case where a method is never being called, I would be inclined not to remove existing code.

    ReplyDelete