Wrong Cyclomatic Complexity?

Wrong Cyclomatic Complexity?

Hi all, I have a very strange report from NCover regarding a tiny portion of my code. It looks like:

private void InitMap() { var map = new Dictionary<IHeader, Message> { {IHeader.CA1, new Message(args => new CA1Class(args), h => h.CA1)}, //other 22 lines like the one above }; }

In short, the Message class accepts in the ctor a couple of simple lambda expression. Well, the generated report indicates this method having a complexity of 45 (and the average was ~1.1)!!

Do you think it's correct this report or am I missing something?

Thanks


RE: Wrong Cyclomatic Complexity?

Deimos,

I believe you are seeing correct numbers because NCover is treating each lambda line in that case as a brancheable point in the execution graph.


RE: Wrong Cyclomatic Complexity?

Hi DWilliams, thanks for your prompt reply, but I still have some doubts. CC metric should be used to identify "hard" portions of the code, starting from the assumption that a lot of (nested) branches makes the code hard to mantain and change.

But, in my concrete example, where is the branch? Doesn't it sound too much simple, in this case, to say that since you're encountering a lambda expression then it's automatically a branching point?

Thanks


RE: Wrong Cyclomatic Complexity?

Deimos, It's a good question. I'll try explaining it a differently, counting the cyclomatic complexity in each step.

1.) A new dictionary instance is created. The closing brace in your code signals a cyclomatic complexity of 1 since all code has at least one linearly independent path.

count = 1

2.) A new line of code instantiates a new class that takes two lambdas as arguments. That is, each class object has 2 new functions it can call. Because they haven't been called, there are three paths to follow through each new class: (a) No lambdas are executed, (b) one lambda is executed, (c) both lambdas are executed. Therefore, the number of linearly independent paths jumps to 4.

Count = 1 + 3 = 4

3.) 22 new lines create 22 class objects, each with two lambdas as arguments. Since each new line has 3 paths it adds to the number of linearly independent paths, the count is 45.

Count = 1 + (22 x 2) = 45

Does that make sense? I took a good deal of time to discuss it with another dev today just to make sure I wasn't imagining things. Please let me know if you find an error in my logic.


RE: Wrong Cyclomatic Complexity?

Hi Dwilliams, thanks for the detailed answer. Just out of curiosity, I googled and found a similar problem reported for VS2008 analyzer (http://connect.microsoft.com/VisualStudio/feedback/details/555560/method-using-many-lambda-expressions-causes-high-cyclomatic-complexity#details), maybe it could be useful.


RE: Wrong Cyclomatic Complexity?

Deimos,

Thanks for the link. I'll look it over and pass it along to some other devs as well. It's a great example to consider.


RE: Wrong Cyclomatic Complexity?

Deimos,

Thanks for the link. I'll look it over and pass it along to some other devs as well. It's a great example to consider.