1

I frequently am not sure if I can remove a method safely in an app that I work on at work. It is a dangling, forgotten method?

Firstly, I understand that if the app is well-tested, I wouldn't have this issue. But the reality is that the app is 9 years old and not well-tested.

Secondly, the issue is not as simple as just searching the code base for the method name for 3 reasons:

  1. Method names may be called via dynamic string concat, eg `obj.send("a_#{method}")
  2. We actually have a significant amount of ruby in our database that gets evaled, which (obviously) brittle and awful and hard to search.
  3. Some methods may be used only internal to other libraries, like ransackable_scopes

So I'm looking for some way (maybe a gem) that once implemented, can track what methods have and haven't been called.

Any suggestions?

pixelearth
  • 13,674
  • 10
  • 62
  • 110
  • 2
    Are the answers here still relevant for you? https://stackoverflow.com/questions/9735307/find-unused-code-in-a-rails-app – Joel Blum Jun 02 '21 at 18:52
  • @JoelBlum I did find the mention of the `coverband` gem promising. Thanks! – pixelearth Jun 02 '21 at 20:11
  • Any kind of coverage tracking will really just give you a picture of the current usage. There is no way to actually get a backlog unless it was recorded in some way which seems unlikely. – max Jun 03 '21 at 01:42
  • @max yes of course, I thought that was clear, but I see I was wrong! I've edited for clarity. – pixelearth Jun 03 '21 at 03:45
  • I don't think you'll find a gem that will be able to magically deduce which methods are safe to delete and the way forwards is to leave any working code as and to write tests covering the existing features of the application so that you can start refactoring without regressions. Unfortunately working with legacy code is a tedious grind. – max Jun 03 '21 at 03:59
  • 1
    Yes, legacy code comes with plenty of challenges, I agree! But to be clear I'm not looking for a gem that tells me what methods are safe to delete. I'm looking for something that will track which methods are called/not called. I'll have to make that determination. As it turns out `coverband` does this pretty well. – pixelearth Jun 03 '21 at 14:17

0 Answers0