Let’s track down some code that’s not actually in use (and delete it, naturally).
Those of you with statically-compiled languages will probably have little to do today, but in dynamic languages like Ruby and JavaScript, it’s pretty easy to end up with unused code laying around.
To track down your unused code, I recommend a tool like unused : https://unused.codes/ . I’ve personally used it to good effect in Ruby, and I believe it will work well with JavaScript and Elixir (see the docs).
If you have a different method for finding unused code that works well for you, please share it here so others can benefit.
As always, please share your results. I’d love to hear how many lines you manage to excise.
Good morning everyone!
Last year we migrated a large system from a legacy homegrown PHP stack to a Laravel application, so lots of unused code was still lying around.
Managed to remove an entire namespace of code that was very deprecated.
Which I most definitely think is false. This is a ~ 10 year old Rails application.
Does anyone have an example of how they are running ctags and unused?
There are no unused codes in my current project. This is well covered by SwiftLint.
And I learned that Swiftlint provides many useful unused related rules.
$ unused --only-filetypes rb
...
== UNUSED SUMMARY ==
Tokens found: 184
Files found: 90
Applied language filters: only Ruby
Sort order: token (asc)
Usage likelihood: high
Configuration setting: Rails
Lots of work to be done. I have only removed a couple low-risk unused methods for now to remain within the allocated time period, as I spent most of my time making unused work at first (cf second section below).
@davidw I had the same problem as you, it was returning 0 results on a 7 year old Rails app. I had to regenerate the ctags file to make it work: ctags -R ..
Also as the project readme suggests, if you don’t already have it it is recommended that you use Universal Ctags (brew install universal-ctags on macOS) instead of the default Exuberant Ctags that ships with macOS.
I just learned that you can troubleshoot problems with unused doctor, so this might give you some pointers.
First of all, I love that Josh rewrote unused in Rust, it’s so much faster
The bad news is, I ran unused on multiple codebases and didn’t come up with much. Some single occurrences came up that were false positives, because I know they were used in a metaprogrammed way - tracking this is somewhat difficult in Ruby.
So end of day - 0 LOC excised, but a lot of fun roaming through old codebases!
For python I tried tool called vulture. Installed it using pipx, it analysed the code even without having virtualenv activated.
Places with confidence 60% were those, which were relevant.
Higher confidence was almost for sure a hit of something to fix.
Once I found a bug this way - a variable was not used and was supposed to be.
for line in pub:
writer.writerline(pub)
was reporting line not being used. Proper code was:
for line in pub:
writer.writerline(line)
I am using neovim with LSP pyright and this includes already some linters reporting unused variables, imports etc. I guess, simply following these warnings from linters would detect 90% of “obsolete piece of code here” cases.
Tried running unused against a rails with react FE code base, and the one it found seems to be false negative… not sure if I generated the ctags file incorrectly or not… interesting tools though…
Recently we replaced parts of our persisting code of a Python API. We kept the old SQLAlchemy implementation in place (“just in case”), but really don’t need it anymore. Today I had a reason to get rid of all of the model implementations we had in place. Felt good!
unused did not work for me. It had no docs on how to install it or use it outside MacOS environment, I fought sometime until I found a precompiled linux binary, but it the end it shown nothing useful. (There was also some useless output I could not suppress).
What did work - I have consulted code coverage, and spotted a file no-one on our team knew was there, and the file was never used (some sort of helper to load into your console to do customer support - but none of us was aware of it), so I killed it with fire.
And even the better thing happened then, when I told the team I’m dropping unused code they started bringing in their suggestions on the things they wanted to get rid of.