Reputation: 2679
According to the basic rules of cyclomatic complexity, the below code should have a complexity of 2 (only one branch point - the for loop).
function SumArray(array_to_sum) {
var sum = 0;
for (var i = 0; i < array_to_sum.length; i++) {
sum += array_to_sum[i];
}
return sum;
}
Many modern frameworks and so on provide data mapping functions, such as jQuery.each()
or most methods in the Underscore.js
framework. Consider the following code:
function SumArray(array_to_sum) {
var sum = 0;
jQuery.each(array_to_sum, function(index, value) {
sum += value;
});
return sum;
}
By typical rules of cyclomatic complexity, the second example has a CC measure of 1. The work is exactly the same, the human complexity of the function has not changed at all. All I did was exchange one means of looping data for a different means of looping data.
Likewise consider this contrived example where we wrap the internals of our original function in a single self-calling closure, producing a cyclomatic complexity of 1 for the outer method while not actually changing how the method works:
function SumArray(array_to_sum) {
return (function() {
var sum = 0;
for (var i = 0; i < array_to_sum.length; i++) {
sum += array_to_sum[i];
}
return sum;
})();
}
Should true measures of cyclomatic complexity include considerations for data mapping/reduction methods such as jQuery.each()
particularly when using an anonymous local closure?
Perhaps closures should export their complexity to the closing parent. Also perhaps methods in general should be able to define an export complexity that is added to the complexity of any function that calls it - for example perhaps jQuery.each()
should have an export complexity of 1 so that using this in place of a normal loop counts complexity the same.
Upvotes: 4
Views: 836
Reputation: 35881
Cyclomatic Complexity is a measure of readability of code. McCabe describes the premise of measuring Cyclomatic Complexity in part by "Furthermore, complex components are also hard to understand, hard to test, and hard to modify."
i.e. code that is complex to the reader is more likely to have bugs because it's harder to test (more paths through a single block of code. It's likely to stay buggy because it's hard to modify because it's harder for them to understand so people avoid modifying it.
Upvotes: 1
Reputation: 26970
Should true measures of cyclomatic complexity include considerations for data mapping/reduction methods such as jQuery.each() particularly when using an anonymous local closure?
Yes. Sure. Whatever.
But remember that "cyclomatic complexity" is the work of man; it's just a name we give to a particular family of heuristics for estimating how "complicated" a particular bit of code is. If you happen to be designing a cyclomatic-complexity-auditing tool for jQuery (or any other lambda-toting language), then yeah, a special case for jQuery.each()
would be a nice feature, and letting the user define the "exported complexity" of their own functions would be an even nicer feature. But let's not get carried away with grandiose language about how this is the Most True Way of computing cyclomatic complexity. It's just a refinement of a heuristic. I could give you ten more possible refinements off the top of my head.
Upvotes: -2