Three Awesome Libraries to Assess Code Quality in Ruby
As part of our Rails upgrade 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 and SimpleCov.
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
e-petitions, an open source
Rails application for the UK Government’s petitions service.
All of them use:
RubyCritic is a gem that uses static analysis gems such as Reek, Flay, and Flog 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:
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:
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:
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
Just like RubyCritic, MetricFu uses other Ruby gems to generate a list of reports for you:
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:
Indeed: Files that change a lot in your codebase may be a bad sign.
Just like RubyCritic, MetricFu generates a report using Reek:
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 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
This tool 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:
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:
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:
In this case, you know that you should probably improve these files:
The next section tells you which files are the best candidates for refactoring:
You can play around with a sample report over here: https://fastruby.github.io/quality/#attractor
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)
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!)