Killing dead Rails controllers

So you’ve inherited an app. It is pretty well tested and doesn’t throw too many errors but the app is big enough that it is hard to tell what is going on most of the time. The original developers are long gone and you need to build new, mission-critical features into the app but writing new code takes too long because all of the dead code already in there.

You ever feel like that?

Half the problem is knowing where to start hacking at first.

As you probably guessed from the title; I suggest you start by killing zombie controllers. These controllers are still walking around but not doing anything and they are easy to find. Also, killing them off lets you use static analysis to find other methods on models, services, etc that could be killed off too.

A few methods to identify zombies

The manual way. Add an after_filter to the application controller that logs controllers and methods to a .log file then (after a while) check it and remove all methods (or whole controllers) that aren’t in that file.

New Relic. You can also find all the calls make to controllers and methods with New Relic and just delete any that aren’t there.

Coverband. Want to keep it simple? Add ‘gem coverband’ to your Gemfile and it will measure your code usage in production with a counter for each time a line of code is hit. It also works for more than controllers.

It is a good idea to use a combination of these approaches to make sure you aren’t killing off code that you might still be (unknowingly) using.

Got a better tool or idea that I missed? Drop it in the comments?

Need a VPN to stay safe online? Dragon Astronauts recommends NordVPN.

How you can clean up some crazy controllers with form objects.

Other articles you might like: