Marty Andrews

artful code

Saturday, May 23, 2009

Finding CRAP Ruby code

A couple of days ago, Uncle Bob blogged about the CRAP in FitNesse. That prompted a few people in the Twitterverse to ponder about whether it would be possible to build such a tool for Ruby. In particular, Jim Weirich and Kevin Rutherford considered it.

Kevin and I have both built code quality tools for Ruby already (Reek and Roodi respectively), so I pinged him to suggest we could have a go. The CRAP (Change Risk Analysis and Predictions) metric is a function of cyclomatic complexity and coverage. I already knew how to calculate cyclomatic complexity from Roodi, and Rcov was an obvious choice to get coverage information from.

After a couple of hours, we came up with crap4r. It's pretty rough, but it basically works. Here's what it looks like:

~/Data/roodi $ ruby -Ilib ../crap4r/bin/crap4r

1.0  ./lib/roodi/core/visitable_sexp.rb#accept
1.0  ./lib/roodi/core/visitable_sexp.rb#node_type
1.0  ./lib/roodi/core/visitable_sexp.rb#children

[snip]

2.0625  /Users/marty/Data/roodi/lib/roodi/checks/name_check.rb#evaluate
2.0625  /Users/marty/Data/roodi/lib/roodi/checks/line_count_check.rb#evaluate
3.0  /Users/marty/Data/roodi/lib/roodi/checks/abc_metric_method_check.rb#branch?
3.04166666666667  /Users/marty/Data/roodi/lib/roodi/checks/check.rb#evaluate_node
3.04166666666667  /Users/marty/Data/roodi/lib/roodi/checks/assignment_in_conditional_check.rb#has_assignment?
3.04166666666667  /Users/marty/Data/roodi/lib/roodi/checks/parameter_number_check.rb#evaluate
3.140625  /Users/marty/Data/roodi/lib/roodi/core/runner.rb#parse
3.33333333333333  /Users/marty/Data/roodi/lib/roodi/core/iterator_visitor.rb#visit
4.128  /Users/marty/Data/roodi/lib/roodi/checks/empty_rescue_body_check.rb#has_statement?
6.0  /Users/marty/Data/roodi/lib/roodi/core/runner.rb#load_checks

Kevin and I will clean it up over the next few weeks and see if we can publish it somewhere in a more usable fashion.

No comments:

Post a Comment