Do iterative methods reduce cyclomatic complexity and improve supportability

cyclomatic-complexity

Do iterative methods such as are commonly found in modern languages such as C#, JavaScript, and (hopefully) in Java 8 reduce cyclomatic complexity's impact on understandability and supportability of code?

For example in C# we might have the following code:

List<String> filteredList = new List<String>();

foreach (String s in originalList){
   if (matches(s)){
      filteredList.add(s);
   }
}

This has a simple cyclomatic complexity of 2.

We could easily re-write this as:

List<String> filteredList = originalList.where(s => matches(s));

Which has a simple cyclomatic complexity of 0.

Does this actually result in more supportable code? Is there any actual research on this topic?

Best Answer

My guess is that you just divided/moved the complexity. It decreased because you doesn't count the implementation of .where() in your CC.

The overall CC hasn't really moved, your own code's CC decreased, only because it's now moved to the framework's code.

I'd say it's more maintainable. When it's a feature of the language, use it. It's no a "ohh, I see, it's a clever trick" that you're using, only a simple inline reduce-like function.

Related Topic