Saikuro is a Ruby cyclomatic complexity analyzer. When given Ruby source code Saikuro will generate a report listing the cyclomatic complexity of each method found. In addition, Saikuro counts the number of lines per method and can generate a listing of the number of tokens on each line of code.
gem install metric_fu-Saikuro
Saikuro is a command line program.
saikuro -h will output a usage statement describing all
the various arguments you can pass to it.
A simple example is:
saikuro -c -p examples/samples.rb
The above command is a simple example that generates a cyclomatic complexity report on the samples.rb file, using the default filter, warning and error settings. The report is saved in the current directory.
A more detailed example is:
saikuro -c -t -i examples -y 0 -w 11 -e 16 -o detailed/
This will analyze all Ruby files found in the "doc/examples/" directory. Saikuro will generate a token count report and a cyclomatic complexity report in the "out" directory . The "-y 0" command will turn off filtering and thus show the complexity of all methods. The "-w 11" will mark all methods with a complexity of 11 or higher with a warning. Finally, "-e 16" will flag all methods with a complexity of 16 or higher with an error.
In the doc/examples directory is are sample files that has examples of the various possible cases that we examined and documented the expected cyclomatic complexity result.
The results of running the above commands is also included.
About Cyclomatic Complexity:
The following document provides a very good and detailed description by the author of cyclomatic complexity.
NIST Special Publication 500-235 Structured Testing: A Testing Methodology Using the Cyclomatic Complexity Metric
By Arthur H. Watson and Thomas J. McCabe HTML http://hissa.nist.gov/HHRFdata/Artifacts/ITLdoc/235/title.htm PDF http://www.mccabe.com/iq_research_nist.htm
How and what Saikuro counts to calculate the cyclomatic complexity:
Saikuro uses the Simplified Complexity Calculation, which is just adding up the number of branch points in a method.
Each method starts with a complexity of 1, because there is at least one path through the code. Then each conditional or looping operator (if, unless, while, until, for, elsif, when) adds one point to the complexity. Each "when" in a case statement adds one point. Also each "rescue" statement adds one.
Saikuro also regards blocks as an addition to a method's complexity because in many cases a block does add a path that may be traversed. For example, invoking the "each" method of an array with a block would only traverse the give block if the array is not empty. Thus if you want to find the basis set to get 100% coverage of your code then a block should add one point to the method's complexity. It is not yet for sure however to what level the accuracy is decreased through this measurement, as normal Ruby code uses blocks quite heavily and new paths are not necessarily introduced by every block.
In addition, the short-circuiting "and" operators (&& and "and") currently do not contribute to a method's complexity, although McCabe's paper listed above suggests doing so.
Example for "and" operator handling:
# Starting values for case 1 and 2 x = false y = 15 r, q = nil # case 1 puts "W" if ((r = x) && (q = y)) puts r # => false puts q # => nil # case 2 puts "W" if ((q = y) && (r = x)) puts r # => false puts q # => 15
Case 1 illustrates why "and" operators should add to a method's complexity, because the result of ( r = x ) is false the if statement stops and returns false without evaluating the ( q = y ) branch. Thus if a total coverage of source code is desired, one point should be added to the method's complexity.
So why is it not added? Mainly, because we have not gotten around to it. We are wondering if this would increase the noise more than it should.
The japgolly fork is a part of an attempt to get metric_fu working in a modern Ruby environment, specifically compatibility with Ruby 1.9 and Bundler.
Saikuro is written by Zev Blut (zb at ubit dot com)
If you find mistakes or missing examples please report them in the issue tracker.
Thanks to Elbert Corpuz for writing the CSS for the HTML output!
Other metric tools for Ruby:
Ryan Davis has an abc metric program as an example in his ParseTree product: http://www.zenspider.com/ZSS/Products/ParseTree/
The PMD project has a tool called CPD that can scan Ruby source code looking for source duplication: http://pmd.sourceforge.net/
Saikuro uses the BSD license.