Make your Rails Code Better using “Metric_fu”

Metric_fu is a set of rake tasks and compilation of several different tools that provide reports that show which parts of your code might need extra work . It uses Saikuro, Flog, Flay, Rcov, Reek, Roodi, Subversion, Git, and Rails built-in stats task to create a series of reports.

Rcov – Measures test coverage
Flog – Measures code complexity
Saikuro – Measures cyclomatic complexity
Flay – Finds duplication (both copy/paste and structural)
Reek – Spots code smells
Roodi – Finds lots of general problems
Churn – Identifies files that change too often
Rails best practices– Code matric tool
Cane– Code quality threshold violations
HotSpot– Meta analysis of your metrics to find hotspots in the code

Step#1

Add the gem in your gem file

gem “metric_fu”

Then run “bundle install”

Metric_fu will install several other dependencies unless they are already present. These include Flay, Flog, Rcov, Reek, Facets and Roodi. These are the tools that Metric_fu uses to generate its reports

Then, run the following command for generating the metrics

rake metrics:all

It will generate a matrics of your code in the temp/matric_fu folder, it uses a bunch of open source project to give all this to you

Benefits

  • You can major the complexity
  • Creates a “hit list” of your most complex method
  • It helps you to prioritize
  • Make you reveal on unknown problems like bugs and hidden complexity
  • Examine the worst offenders
  • Refactoring complex methods yields many benefits like Smaller, easier to understand methods and Finding bugs

Problem

  • We are aware of unknown complexity
  • Our code will get worse if it is not getting better

Conclusion

Code analysis isn’t an exact science and none of the tools shown are perfect in themselves, so they should all be used as an aid to help you find the areas of your code that might need some work not as an absolute instruction that you must refactor a part of your application.Image