Ecommerce Developer
 
 

Code

Ruby on Rails: Code Metrics Identify Weaknesses, Strengthen Code

 

Using Ruby on Rails, programmers can quickly and easily build complex sites in very little time. As a result, Rails is an ideal framework for quickly iterating on new ideas, especially with a small, focused team.

However, as time goes on, many Rails teams find that the rate at which they can add or improve features (known as the team's velocity) is significantly reduced. They notice it is difficult to change the code without introducing defects and that technical debt accumulated early on now makes it costly to add new features.

To address this problem, teams should adopt practices that allow them to identify risky and high-maintenance code and refactor the code to improve it.

Many practices can assist with this process of identifying and fixing bad code. Maintaining a comprehensive suite of automated tests allows code to be safely refactored and prevents regressions. Performing regular code review (or engaging in pair programming) can get fresh sets of eyes on code, which can expose problems.

Code Metrics

Code metrics are another, complementary approach. Automated tools can analyze many aspects of a project's code base. For example, tools can find code that is highly complex or that changes frequently (a measure known as churn). Research has shown that complex and high-churn code is more likely to contain bugs.

Automated tools can also detect poor programming practices such as duplicated code. Duplicated code adds to the cost of maintenance since any change in one instance of the code will have to be repeated in all duplicates. Duplicated code can also be a source of bugs if a programmer changes one copy and neglects to change the others.

Caliper Brings Key Code Metrics Tools Together

Caliper is a web application that makes it easy to generate metrics for Rails projects. Caliper provides detailed metrics on code smells, duplication, code complexity, lines of code, and churn. In addition, it summarizes this information so programmers can understand the overall health of their project and identify which parts need the most help.

To get started, visit the Caliper website, enter the URL for the Git repository for your project, and click "Get Metrics." (At this time, Caliper only supports open-source Ruby projects that have a public Git repository.)

Example from Caliper, showing get metrics text field and submit button

In order to demonstrate the features of Caliper, I'll use the metrics for the web framework Sinatra project as an example. (I'm not picking on Sinatra. It's one of my favorite Ruby projects, but it happens to be a good example project.)

The first thing to notice is the metrics dashboard on the main page. This dashboard provides a summary of the state of each metric. For example, Sinatra currently has 150 total "Reek smells."

Example showing 150 Reek Smells

Reek reports this project has 150 code smells.

After surveying the dashboard, you may understand that your project needs some work, but may be wondering where to get started. The "Hot Spots" report can help with this. Click on the "Hot Spots" link to display the files, classes, and methods with the most problems across all metrics.

For the Sinatra project, I can see that the instance method "route!" on the "Base" class could be improved: It has a high code complexity and three code smells.

Route and Base example from Caliper

The Hot Spots report identifies the sections of code that need improvement and sums up the problems.

Once you've identified some code that needs improvement, you can view the detailed report for a specific tool. Click on the "Reek" link to see a full report of the code smells in your project. Scrolling down, I can find the three code smells in the "route!" method: It has some duplication, the method is too long, and it contains a "Control Couple" (basically, a less-than-ideal control flow within a method).

Example of trouble areas in the code

The Reek report gives detailed information about code smells.

After you've improved your code, you can commit your changes, push to your public Git repository and regenerate metrics. All of this has made your code easier to maintain and less likely to have bugs.

Conclusion

As Rails projects grow, teams often find that their velocity is reduced due to code debt and increasing defect rates. Used in combination with practices like automated testing and peer review, code metrics are a powerful tool for identify and improving bad code. Caliper brings together a set of useful code metrics and allows developers to easily review the overall health of their project as well as identify key areas that should be improved.

Related Articles

0 Comments

Rss-sm