Blog

Automatic and Static Ruby Code Analyzer Pt 1

Code analyzers are useful tools for measuring metrics around code standards because they give you an idea of your code quality. This is really important because, as a software developer, you should always be certain you’re delivering quality software products. This is a great field of study in computer science and is developing continuously.

To begin and give a short summary, analyzers are divided into 2 categories:

Automatic and Static Ruby Code Analyzer Pt 1

Here, we can see that each row is a category. We have manually and dynamic or manually and static, for example. In this post, I’ll focus on automatic and static.
As a Ruby developer, I’ll focus on tools for this language. However, these same concepts can be used in other languages. If we take a look in RubyToolbox, my favorite source to look for gems, we can check the category of Code Metrics. Here we see a list with some really good gems:

  • Simplecov: Really good for checking test coverage against your overall code.
  • Rubocop: I use with Atom to always stick to the ruby style conventions.
  • Reek: Report code smells in your code.
  • Saikuro: Cyclomatic Complexity in your code.
  • Cane: This gem checks that your organization and setup meet quality standards.
  • Flog: Reports tortured code.
  • Rubycritic: We will talk about this below:

I’m pretty interested in Rubycritic and it’s the gem I usually use to have an idea of the quality I’m delivering to my clients. Why Rubycritic? This gem gives us, in a summarized way, three important metrics: Churn (change in code over time), Reek (code smells: bad variable names, duplicated code, unused parameters, etc.), Flay (checks similar code structures to detect where we can do refactoring), Flog (detects code complexity using ABC heuristic). Here, let’s check out Rubycritic in action to illustrate this. I’ll use a simple, but buggy, research project (with some ugly practices like mixing model/controller logic, duplicated code everywhere, high complex, not DRY classes, etc) I created to show how useful Rubycritic is to outline those defects.

First, we install Rubycritic. In your gemfile, add to group ‘development

gem ‘rubycritic’

bundle

And when it’s done, just run:

rubycritic

This will generate a new folder called tmp and it will open in your browser overview.html.

You’ll see something like this:

Automatic and Static Ruby Code Analyzer Pt 1

 

This is the overview of your project. You can see here that you have a score of how your code is doing. In the example shown here, we can see that it is not doing so well. You always want to avoid having dots (which represents classes or files) next to the top-right corner. Why; you ask? As you can see, the higher your dot is, the more complex your class is. The farther right your class is, the more churn (changes during time measure with commits to your repo) increases. Therefore, the top-right corner says to you that you have a unmaintainable file. In other words, your file is really complex and constantly changing file/class.

Going to the code (2nd from left) tab, there is more tabular information. Each row is a file with its qualifications for churn: complexity, duplication, smells and an overall rating. Even your tests are listed here, so you can also check if you’re creating quality tests.

In the following image, you can see some files for my project. As the overview indicated to me, some file classes are not doing well. This is telling me that I should do some refactoring to improve my code base.

Automatic and Static Ruby Code Analyzer Pt 1

 

If we check a specific class, we can see the smells detailed view:

Automatic and Static Ruby Code Analyzer Pt 1

 

Scrolling down we can see others:

Automatic and Static Ruby Code Analyzer Pt 1

If we click the smell referenced, it redirects to a GitHub page with the description of the smell and some hints on how to solve it.

Automatic and Static Ruby Code Analyzer Pt 1

Finally, what I believe, is the most important, is the Smells tab. This is because, here, is where we actually can detect specific issues. This tool is smart enough to give you hints on how you should fix them.

Automatic and Static Ruby Code Analyzer Pt 1

 

Checking the specific file, as we did above, you can see the lines with the code smell and a link which shows you some hints for solving them.

Automatic and Static Ruby Code Analyzer Pt 1

Conclusions

This part 1 focuses on diagnosing your code issues with some common metrics. With this tool, you can provide your client with some good metrics on your coding ability and improve your coding skills. Subscribe to our blog and stay tuned for my next post, where I will talk about rubocop and how it helps you to improve your Ruby quality from the moment you begin coding.

Gorilla Logic is always on the lookout for the best and brightest, be sure to check out our Careers Section.

Ready to be Unstoppable? Partner with Gorilla Logic, and you can be.

TALK TO OUR SALES TEAM