Find and remove unused code using leftovers gem #420
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
#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 usinga new
Analysis
step. The gem uses static analysis to create a reportof all of the code that it thinks we haven't called.
We can use the
.leftovers.yml
file to define general patterns forthe gem to either ignore (in the
keep
section) or consider to havebeen 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 ActiveRecordassociations 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
annotationwhen 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 toidentify 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.