Looking for unused code

While I was working on a bug fix last week for the top-secret unnamed project that I’m working on, I found a class that has several public methods. A couple of the methods smelled bad to me, so I looked to see where they were used in the app. It turns out that none of the the methods were being used, so I got rid of the class.

I had some spare cycles later in the week, so I thought I would try to identify any classes that were not being used in the system, and as a bonus identify any unused methods. Now, it’s important to realize that the top-secret unnamed system is fairly large, and we’ve had a number of people working on it over the years, so some cruft is expected.

Eclipse does a good job of identifying unused private members and methods, but doesn’t attempt to do anything at a public level. At first I thought I’d write a tool to identify the unused methods. But I realized that this was likely a problem that had been solved, so I went looking for options.

The first (and it turns out, only) tool that I ran across was RefactorIT (the mixed-case spelling is their choice, not mine). It seemed to be exactly what I needed: a tool that could identify unused code (among many other things), had compatibility with Eclipse, and a generous evaluation license. So I downloaded it, set up my evaluation license, and got to work.

According to the first pass, we had 437 unused classes and an additional 8109 unused methods in the code base. Going forward was not as easy as deleting these entities and hoping for the best. Many of the classes were necessary even though they weren’t being “used” by the rest of the system. Many of our EJB local homes are never referenced – as an example, we never look up a address on its own, we always walk from a person to the address. Yet it makes sense to keep the home interface in place just for consistency. Other classes and methods were being used through reflection or alternate means of accessing them.

At the end of my two-day exercise, I had removed 61 classes and a few hundred methods. I likely reduced the size of the code base by 1-2%, which may not seem like a lot, but it was code that was incurring maintenance costs just by existing.

It turns out that RefactorIT was exactly what I needed for the job I assigned myself. I can recommend its use for other projects that want to do this sort of code analysis. A quick look at their pricing page indicates that we’d likely need their Enterprise edition due to the size of our projects, which runs 295 euros. It’s a good deal, even for the limited subset of functionality that I made use of.

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply