Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does list initialization with lambda causes high cyclomatic complexity?

Initialization of list with lambdas causes high IL cyclomatic complexity: why, and how remove this complexity? For example following code causes the static constructor of the class (which is actually compiler generated) to be very complex: 1 + the list count.

static List<Predicate<string>> list = new List<Predicate<string>>()
{
    s => s == null,
    s=> s.StartsWith(“R”),
    ... With a lot of predicates like that ….
};

Note: complexity is computed with NDepend

like image 422
sthiers Avatar asked Oct 26 '10 08:10

sthiers


1 Answers

Why? Because ILCC is defined as the number of different jump/branch destinations. That list you are initializing contains a lot of if/then logic, contained within the lambdas. I presume the language-dependent CC is lower?

High cyclomatic complexity is just a hint that your functions are over-complex, and thus hard to understand and maintain and test. Whether that hint is correct in this case would depend on how you use that list of predicates. But it is just that, a hint. Keeping CC low should not be regarded as a law of nature. If you think the code is maintainable and testable, note the high ILCC in your documentation, explain why it doesn't matter, and move on.

like image 197
Pontus Gagge Avatar answered Oct 22 '22 18:10

Pontus Gagge