Day 4 - Excise some unused code

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.

Almost done with week 1! Keep it up.

1 Like

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.

Thanks again, and see you tomorrow. :slightly_smiling_face:

1 Like

Ah, I have been down this path before - but in my current project I am working on I tend to get the same results:

== UNUSED SUMMARY ==
   Tokens found: 0
   Files found: 0
   Applied language filters: except Markdown, JSON
   Sort order: token (asc)
   Usage likelihood: high
   Configuration setting: Rails

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?

Normally phpstorm warns me about duplicate or unused. I have colored unused red😛

Currently looking into static analysis with Psalm

I managed to remove around 140 lines of code. Some of it has never been used and have been laying around in the codebase for 4 years :see_no_evil:

1 Like

Hello everyone,

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.

Woot woot, this unused tool is great!

$ 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. :slight_smile: 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.

1 Like

I went through my .js files and they are all looking pristine now :ok_hand:

Good morning!

First of all, I love that Josh rewrote unused in Rust, it’s so much faster :heart:

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!

1 Like

Oldest unused code was some auto-generated boilerplate from project setup. It had even been moved once without being deleted. Whoops

It took me a while to figure out how to generate the tags, and then I didn’t find any unused code. But at least I discovered a new tool :grinning_face_with_smiling_eyes:

Thanks for the tool. It’s nice to explore how not to spend much time on exploring and save much memory. Again Thanks for the tool

Thanks for the tool and for some tips.
Seems like it didn’t work for me since it shows 0 for a legacy app.

$ unused --only-filetypes rb
== UNUSED SUMMARY ==
   Tokens found: 0
   Files found: 0
   Applied language filters: only Ruby
   Sort order: token (asc)
   Usage likelihood: high
   Configuration setting: Default

I would expect it to show at least something for ruby code. I even added a random method and it didn’t show up. Will investigate later.

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… :+1:

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. :+1: Felt good! :slight_smile:

Thanks for the reminder! Hard to prioritise this, but it has benefits and feels good:

Date: Thu Feb 4 14:07:22 2021 +0000
 8 files changed, 1491 deletions(-)

4 spec files were deleted, so we’ll have a marginally faster test suite too! :slight_smile:

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.

So, no code is better than no code.

Not sure about the best solution for React pipeline / Python API so I am updating README with outdated architecture, and old info instead.

Yeah, the lack of plane-old-rust install instructions is a bummer.

Basic build on linux with rust.

git clone https://github.com/unused-code/unused.git
cd unused
cargo install --path .

Then you’ll have an unused binary in target/release/unused.