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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With