Sandi Metz is an activist of writing well factored object-oriented code. Recently a set of 4 simple rules by Sandi got publicized and spread throughout the community. In this post I will introduce a gem which automates scanning your code versus Sandi's rules.


First, I will highly recommend you to read the Thoughbot article on using Sandi's rules in their development process. Also there was a talk by Sandi Metz at BaRuCo 2013 couple of weeks ago and I hope that some day the video will be posted.

Why I found it useful

As Sandi mentioned in her talk at BaRuCo – you need some rules to factor a good code. Experienced developers always have rules (and even write books on them) and these 4 rules are perfect if you don't have your own strong rules yet.

For many projects with a plenty of business logic these rules work just great. They are indicator of how good the project is factored (but the result of course should not be 100% of proper classes/method or all green).

These rules are not taboos and of course you are allowed to brake them. But "With great power comes great responsibility".

gem "sandi_meter"


sandi_meter gem is based on Ripper (it converts code to S-expressions, which are in fact nested arrays and then iterates on them and check classes for the rules).

CLI mode

Gem could be used in 3 modes. First – console mode which works perfect when you need to check a project once.

> sandi_meter -p ~/path/to/project

# 1. 94% of classes are under 100 lines.
# 2. 53% of methods are under 5 lines.
# 3. 98% of methods calls accepts are less than 4 parameters.
# 4. 21% of controllers have one instance variable per action.

HTML mode

I like this one the most. In HTML mode the results of each scan are logged into file. HTML file with the diagrams of the latest scan and plots of projects dynamics is updated after each time you run sandi_meter. SandiMeter HTML mode

To use HTML mode run sandi_meter with g option: sandi_meter -g and checkout YOUR_PROJECT_PATH/sandi_meter folder for index.html and sandi_meter.log.

Ruby mode

This mode is useful if you need to use scan results in your own script or CI. It could be done with just 3 lines of code:

require 'sandi_meter/file_scanner'
require 'pp'

scanner =
data = scanner.scan(PATH_TO_PROJECT)
pp data
# {:first_rule=>
#   {:small_classes_amount=>916,
#    :total_classes_amount=>937,
#    :missindented_classes_amount=>1},
#  :second_rule=>
#   {:small_methods_amount=>1144,
#    :total_methods_amount=>1833,
#    :missindented_methods_amount=>0},
#  :third_rule=>{:proper_method_calls=>5857, :total_method_calls=>5894},
#  :fourth_rule=>{:proper_controllers_amount=>17, :total_controllers_amount=>94}}

Some fun

The first version of gem was released in less than 24 hours after Sandi's talk as lightning talk at BaRuCo:


Check out the gem, I hope you find sandi_meter useful and will have some fun with that :) Please, drop an issue or feedback.

#ruby #sandi_meter #static_analysis #ripper