Friday, January 11, 2008

Cyclomatic complexity

In his last post , Shaun talked about the new Code Metrics feature in 2008, in particular Cyclomatic Complexity (CC).

Many of you out there might not have come across the term before so I thought I'd give a readers digest definition.

So what is CC? In a nutshell its a measure of complexity of some code. It is got by measuring the linearly independant paths in the measured code. What the hell does that mean?

Basically its the amount of branching your code does, with control statements like for or if.

Example



The above code has two branches. To do branch coverage, then two test cases are sufficient, to do path coverage then four test cases are needed. The cyclomatic complexity of this code is 3.

This is achieved (there are a few ways to calculate) by the following formula:

M = Number of Closed Loops + 1 (where M = CC)

So to cut the post short, the higher the CC number the more rigid the unit testing needs to be.



kick it on DotNetKicks.com

No comments: