Re: McCabe complexity
- From: Steve Eddins <Steve.Eddins@xxxxxxxxxxxxx>
- Date: Tue, 03 Jun 2008 07:47:03 -0400
Walter Roberson wrote:
In article <g1proh$40p$1@xxxxxxxxxxxxxxxxxx>,
Kenneth Eaton <Kenneth.dot.Eaton@xxxxxxxxxxxxx> wrote:
It seems clear that the McCabe complexity measure doesn't like switches all that much, unless they're tiny and simple. Is this anything other people have seen? Should I be concerned about this?
Or maybe this is just one of many subjective complexity measures that I just shouldn't worry about? What are everyone's thoughts?
I did a little looking around at some of my code, and the
McCabe measure was an absolute minimum of 8 for all but the
most trivial logic. A simple nested-if with all four branches
populated was enough to get up to 8. Anything with a triply-nested if
was 20 or higher.
Now perhaps it is the case that people have to strain a little
to get a triple-nested if straight in their mind, but in terms
of practical programming, a triple-nested 'if' is nothing special,
and deliberately writing code to avoid it would end up
with a lot of fake state variables that would hard to keep
straight, or would require breaking up the code into routines
that had little more than temporal linkage.
Consider this simple and standard functional block:
if we need to do this block of work
result = somefunction(the appropriate variables);
if result indicates failure
display an error message;
return;
end
end
This standard logic will generate a McCabe measure of at least 8.
If there is any but the most trivial surrounding logic, you are going
to blast through the 10 "limit". And yet it is logic that effectively
cannot be reduced in complexity without introducing catch/error:
if you try to push the logic into a subroutine in order to reduce
the complexity, the logic for calling the subroutine will be the same
as the above logic!
I count a McCabe complexity metric of three for your example: One for the straight-line code path, one for the outer if, and one for the inner if.
It is not difficult, given a little practice, to routinely write functions that have a complexity metric of 10 or less. Taking coherent bits of logic and "naming" them (by putting them into their own function with thoughtfully chosen name) increases the readability and maintainability of most code, in my experience. My team uses this as a coding standard for new code. For making changes to existing code, our standard is to not increase the complexity metric above 10 for functions we are modifying, and to look for opportunities to reduce the complexity via behavior-preserving refactorings.
Switch statements do pose a problem for this standard, as the original poster noted. I've been thinking about switch statements and the McCabe metric for some time now, and I've arrived at this conclusion: Switch statements with a "clean outline structure" are allowed, even if they cause the complexity metric to go above 10. By "clean outline structure" I mean that no conditional logic is allowed in the cases; straight-line flow only.
---
Steve Eddins
http://blogs.mathworks.com/steve/
.
- Prev by Date: Windows Service
- Next by Date: Re: 3D Morphology
- Previous by thread: Windows Service
- Next by thread: linking to shared object libmx.so problem
- Index(es):
Relevant Pages
|