I had a discussion the other day with somebody about the uselessness of measuring cyclomatic complexity of methods. In short the discussion went along the lines that using just a single measurement does not give you anything else than a number. And some methods with high cyclomatic complexity may still be easy to understand. This reminded me of a tool I looked at a long time ago. At that time only crap4j was mature and crap4net was in an early alpha (looks more mature now). So what do these tools do? Well they calculate CRAP where CRAP stands for Change Risk Anti-Patterns. The idea is to use both cyclomatic complexity and code coverage to find methods that are likely to be dangerous to change in the future. Complex methods are more likely to be difficult to change without introducing new defects. And the same goes for even simple methods without automated test coverage. Hence CRAP is calculated using the following formula:

CRAP = COMP^2 * (1 - COV)^3 + COMP

COMP is the cyclomatic complexity for a method and COV is the code coverage for that method. Preferably the path (or condition/decision) coverage should be used for code coverage. Only code coverage generated using automated tests should be used (i.e. no manual tests). If CRAP is more than 30 it is considered a high risk method. CRAP of 5 or below is considered good.

I like how the use of these two metrics that are easy to get together give you a much better tool to find methods that need some attention. That's what the Uncle is doing. And as he says; these metrics are so easy to get and helps you focus on what code needs to be fixed for the future. Just do it!