Cyclomatic complexity calculation

Cyclomatic complexity calculation

Hi, I have a bit of a problem - maybe you can give me a direction, I'm using NCover to calculate cyclomatic complexity for my .NET code, for non .NET code I'm using a different tool, I've tried to compare between the 2 tools and I've noticed that for the same .NET methods - both tools gave different values of cyclomatic complexity (CC), Do you have any idea why this happends? Are there various ways of calculating cyclomatic complexity? For example : I looked at a simple method that only has one lambda expression (and does not have loops)- NCover gave a CC of 1 while the other tool gave it CC of 2 – do you have an explanation for that? I don’t really need the 2 tools to give the same absolute values - I just want to verify that the difference is due to difference in the "scoring methods" between the 2 tools (which is fine with me) and not because there's an actual bug in one of the tools. I can write this functions (or other functions that gives different CC) if necessary .

Thank's


RE: Cyclomatic complexity calculation

Hi Assaf,

Thanks for your question. It's one we've had before and this is what the development team had to say about how NCover calculates cyclomatic complexity:

We calculate CC by taking the branching graph of the method (calculated from the IL). Cyclomatic complexity is the number of edges - the number of nodes + 2. Another definition is that it is the number of linearly independent paths through the code.

I hope this is helpful!

Best regards,

NCover Support


RE: Cyclomatic complexity calculation

Thank's! This is helpful - and is consistant with your numbers,

Two more clarifications though:

1) Is that formula a customary one? 2) How do you decide whether a lambda expression contributes to the complexity?

Assaf


RE: Cyclomatic complexity calculation

Hi Assaf,

Here's what the dev team had to add:

Yes, that is the the most commonly used definition of cyclomatic complexity.

We treat lambdas (anonymous functions or delegates) as independent methods. CC is calculated for each lambda within itself and that calculation does not increase the complexity calculation of the containing method.

Best regards,

NCover Support


RE: Cyclomatic complexity calculation

Well I just want to know the manual process to find out the cyclomatic complexiety of any sample code.

Best Regards, Chetan


RE: Cyclomatic complexity calculation

Hi Chetan,

There are several ways of determining Cyclomatic Complexity, but we calculate CC using the branching graph of the method (calculated from the IL):

Cyclomatic complexity = number of edges - the number of nodes + 2.

I hope this helps.

Best Regards,

NCover Support


RE: Cyclomatic complexity calculation

Hi Chetan,

There are several ways of determining Cyclomatic Complexity, but we calculate CC using the branching graph of the method (calculated from the IL):

Cyclomatic complexity = number of edges - the number of nodes + 2.

I hope this helps.

Best Regards,

NCover Support