Three Awesome Libraries to Assess Code Quality in Ruby

Three Awesome Libraries to Assess Code Quality in Ruby

As part of our Rails upgrade opens a new window business we get to evaluate a lot of codebases every month. We usually need a quick way to assess the quality of the code we get. For this we like to use CodeClimate opens a new window and SimpleCov opens a new window .

CodeClimate is free for open source projects and paid for private projects. I know that not everybody can pay for their service, so I thought it was a good idea to share some free, open source alternatives.

Here is a list of 3 tools that can help you assess the quality of your next codebase.

What I like about these tools is that they use other Ruby gems to calculate the complexity of the codebase.

I went ahead and created a sample report for each of them. As my test project, I picked e-petitions opens a new window , an open source Rails application for the UK Government’s petitions service opens a new window .

All of them use:

RubyCritic

RubyCritic opens a new window is a gem that uses static analysis gems such as Reek opens a new window , Flay opens a new window , and Flog opens a new window to provide a quality report of your Ruby code.

One of the best things about this tool is that it provides a quick overview of the project you’re analyzing:

RubyCritic Overview for alphagov/e-petitions

With this you can get a glimpse of the complexity and churn in all files.

In the next section you can find all the files that have an “F” grade:

RubyCritic: Files that need attention

With this you can pick what files you want to refactor next. :)

In the last section you can find all the code smells in the project:

RubyCritic: Smelly files

This section could use some improvement. It seems to sort code smells alphabetically. You might want to use that section if you prefer to focus on one smell at a time.

You can play around with a sample report over here: https://fastruby.github.io/quality/#ruby-critic opens a new window

MetricFu

Just like RubyCritic, MetricFu opens a new window uses other Ruby gems to generate a list of reports for you:

MetricFu: All the reports

Unlike RubyCritic, MetricFu does not provide a quick overview of the application’s codebase. You need to drill down the reports list to investigate each aspect of the quality report.

For instance, if you want to find what files have been updated the most, you will need to review the Churn report:

MetricFu: Churn report

Indeed: Files that change a lot in your codebase may be a bad sign.

Just like RubyCritic, MetricFu generates a report using Reek:

MetricFu: Reek report

With this report you can get a quick glimpse about the most common code smells in your project.

If you check out the Flog section, you will find the methods that are hardest to test, the ones that are most complex:

MetricFu: Flog report

MetricFu is definitely more ambitious than Attractor and RubyCritic, but it hasn’t been actively maintained in years.

You can play around with a sample report over here: https://fastruby.github.io/quality/#metric-fu opens a new window

Attractor

This tool opens a new window is a new tool created by Julian Rubisch. It is certainly simpler than MetricFu and RubyCritic, as it only uses churn and complexity to calculate the most painful files of your project.

This graph is quite similar to the one I showed you in RubyCritic’s overview screenshot:

Attractor: Churn vs. Complexity

It shows file complexity (Y Axis) vs. file churn (X Axis). You can quickly determine which files have changed the most and are most complex. For example:

  1. spec/models/petition_spec.rb
  2. spec/models/signature_spec.rb
  3. spec/controllers/sponsors_controller_spec.rb

Sometimes that information is not very useful. I prefer to focus my refactoring efforts in application code, not test code. So, if you want to filter by directory, you can just run this command:

attractor report -p app

That way you can see what application files need some love:

Attractor: Filter App Files

In this case, you know that you should probably improve these files:

  1. app/models/signature.rb
  2. app/models/petition.rb
  3. app/models/site.rb

The next section tells you which files are the best candidates for refactoring:

Attractor: Refactoring

You can play around with a sample report over here: https://fastruby.github.io/quality/#attractor opens a new window

Final Thoughts

Assessing code quality is a tricky subject. Every time you get the opportunity to join a project, you should make sure you do the homework to assess whether you’re joining a stable project or a dumpster fire. I hope that you find these tools useful and that you avoid getting stuck in the tar pit!

If you are looking for Rails-specific suggestions for judging the quality of an application, check out this article: Legacy Rails (Silently Judging You) opens a new window

What tools do you like to use to assess code quality? Let me know in the comments below! (I know that I forgot to mention a few!)

Get the book