impactoss / impactoss-server

IMPACT OSS - server-side application & API
https://demo.impactoss.org
MIT License
3 stars 8 forks source link

Find and remove unused code using leftovers gem #420

Open lukearndt opened 1 week ago

lukearndt commented 1 week ago

Context

https://github.com/impactoss/impactoss-server/issues/363

We want to clean up the code base by removing unused code.

Changes

This commit takes a first step at removing and preventing unused code by installing the leftovers gem and running it as part of the build using a new Analysis step. The gem uses static analysis to create a report of all of the code that it thinks we haven't called.

We can use the .leftovers.yml file to define general patterns for the gem to either ignore (in the keep section) or consider to have been dynamically executed (in the dynamic section).

Where the gem finds individual lines of code that it thinks are not called, but we are confident that we want to keep, we can annotate them using # leftovers:keep. Examples of this are uncalled ActiveRecord associations and methods that we need to define for gems to call.

In some cases, I was not sure if the code that leftovers reported was safe to delete so I have annotated it and left comments to that effect.

In the future, we can use search for the # leftovers:keep annotation when we're trying to figure out if something is safe to delete.

Finally, the commit removes some code that the gem reported as unused and that I was confident enough to go ahead and delete.

Considerations

The leftovers gem does not perform any kind of runtime logging to identify which code paths are called (or not) on an ongoing basis.

Without that, it cannot report on entirely vestigial features such as groups of unused routes, controllers, models, and views.

For that task, we could either install a tool that performs runtime logging or rely on our knowledge of the system and judge accordingly.

This commit does not fully address issue #363 because it does not delete all of the code that we've identified and listed there. That work can be completed as part of a future change.