Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dead code detection in ruby

Does anyone know of a production worthy package commercial or OSS that can detect which lines of code have been executed or not?

We're looking around for some tools that can help us detect dead code in a production environment, running Ruby On Rails 1.8.7

-daniel

like image 435
Daniel Avatar asked Jan 28 '11 01:01

Daniel


People also ask

How do you find the dead code?

The quickest way to find dead code is to use a good IDE. Delete unused code and unneeded files. In the case of an unnecessary class, Inline Class or Collapse Hierarchy can be applied if a subclass or superclass is used. To remove unneeded parameters, use Remove Parameter.

Which testing can discover dead code?

Polyspace tools help you identify dead or unreachable code in your software. This saves time and reduces the cost associated with testing activities geared for robustness and complete code coverage.

Should dead code be removed?

Removing dead code that eats up runtime and memory is the easiest way to improve performance because it can be deleted and the application's functionality won't be impacted. Dead code also makes your source code unnecessarily complex. This makes the code harder to maintain and build upon.

Why does dead code occur?

Dead code can arise for different reasons: A developer may have forgotten to delete superfluous lines of code. Or the developer has decided not to delete because the code is mixed with functional code. Or the code is intentionally not removed, so it can be reactivated at a later time.


2 Answers

In Ruby 1.9.2 you could simply measure coverage without a significant impact on performance. In 1.8.7, however, this would slow down things way too much. Instead you could get an overview of what's used using perftools.rb (with the CPUPROFILE_METHODS=1 option). As far as I know it has virtually no impact on application performance and it would allow you to see what methods have been called, although you would not get any information about the different code paths (ifs and loops and whatnot).

like image 121
psyho Avatar answered Sep 20 '22 15:09

psyho


The Code Metrics category in Ruby Toolbox mentions code coverage programs such as rcov, but that only covers code that you test.

like image 28
Andrew Grimm Avatar answered Sep 20 '22 15:09

Andrew Grimm