Average C.R.A.P Score

Trend Metric Trend Metrics / Coverage

Change Risk Analyzer and Predictor (CRAP) pinpoints overly complex and untested code (http://www.artima.com/weblogs/viewpost.jsp?thread=215899). Formula: CRAP(m) = CC(m)^2 * (1 – cov(m)/100)^3 + CC(m), with CC(m) the cyclomatic complexity and cov(m) the percentage coverage. The higher the score, the more painful and error prone the method. Threshold 30 as suggested by the inventors; methods under 10 lines of code are skipped. To list methods with high scores see the rule Test and Code Coverage > C.R.A.P method code metric.

CQLinq source

This trend metric is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.

// <TrendMetric Name="Average C.R.A.P Score" />

(from m in JustMyCode.Methods

// Don't match too short methods
where m.NbLinesOfCode > 10

let CC = m.CyclomaticComplexity
let uncov = (100 - m.PercentageCoverage) / 100f
let CRAP = (CC * CC * uncov * uncov * uncov) + CC
where CRAP != null && CRAP > 30 select CRAP)
.Average(CRAP => CRAP)

//<Desc>
// **Change Risk Analyzer and Predictor** (CRAP) pinpoints overly complex
// and untested code (http://www.artima.com/weblogs/viewpost.jsp?thread=215899).
// Formula: **CRAP(m) = CC(m)^2 * (1 – cov(m)/100)^3 + CC(m)**, with *CC(m)*
// the *cyclomatic complexity* and *cov(m)* the *percentage coverage*.
// The higher the score, the more painful and error prone the method.
// Threshold 30 as suggested by the inventors; methods under 10 lines of
// code are skipped. To list methods with high scores see the rule
// *Test and Code Coverage* > *C.R.A.P method code metric*.
//</Desc>

Trend Metrics / Coverage: all 12 items