Reputation: 1015
Can Anyone point Me to a tool to detect unused code, objects, methods, parameters, etc., in Ruby code?
I saw nitpick
but it does not seem to give me the coverage I want. I also checked laser
and reek
but their respective gems seem to have issues which prevent them from running.
I thought at one point the Ruby binary had a mode which would detect unused constructs but I do not seem to be able to find it.
Upvotes: 1
Views: 398
Reputation: 22650
You can use a static analysis tool like Debride to find methods that appear to be unused. It is not guaranteed to be correct, since you can dynamically construct the methods you want to call, and as a static analysis tool it does not actually run your code to see what is used and what is not used.
You can also use a tool like Coverband to detect (in production) which methods get called. In this case a method might not be called because it belongs to a feature that no one cares about, even though there is a code path through which it could hypothetically be called.
I recommend using both. One takes a more rationalistic approach and the other a more empirical approach, and between the two the chances of wrongly identifying unused code seems extremely low.
Upvotes: 0
Reputation: 1042
You can also use a mutation tester that mutates your code. In case the mutation tester can delete a construct without your tests noticing. You found an unused construct.
I know two mutation testers for ruby:
Disclaimer, I'm the author of mutant.
Depending on your setup, your ruby version, spec layout, test framework heckle and or mutant can do the job for you.
Here you can see mutant in action: http://ascii.io/a/1707
Upvotes: 1
Reputation: 4377
JetBrains RubyMine http://www.jetbrains.com/ruby/quickstart/index.html
Upvotes: 0
Reputation: 10564
It might help if we had a little more context in how you want to "detect unused code" - is this code coverage of your tests you're looking into? Otherwise, how would you know from run to run whether you hit all the use cases? Or are you looking for a statistical "heat map" of coverage over time for e.g. performance reasons?
In any case, for code coverage while testing I use SimpleCov - it uses Ruby 1.9's built-in Coverage library with some nice sugar on top.
Upvotes: 0