|
|
Re: Cyclomatic Complexity as a Measure of Code Quality
|
Posted: Mar 31, 2006 4:44 AM
|
|
> > Do you evaluate the CC metrics of your code? If so, > what > > tools do you use, and what average CC values do you > find > > in your code? > > I have never used such tools, but I find the idea > interesting. > > What I wonder is this: if based on the CC measurement you > refactor the code so that the CC metric is better, is the > complexity really reduced or is it just transformed into a > different kind of complexity that the CC tool can't detect?
It may well be the latter, and that's a feature, not a bug. :)
Complexity is commonly grouped in two components: Essential and incidental. Essential complexity is the complexity of the domain, itself, and we can't do anything about that (unless we change the domain or domain model), and incidental, or accidental, complexity is any complexity the technology introduces, to implement the essential complexity.
Naturally, we should try to reduce the amount of incidental complexity as much as possible, letting the code express the domain model as clearly as possible.
Then there's two kinds of complexity: local and global/total. This is a big deal, because the local complexity (say a function definition) is the amount of complexity you must be able to keep in your mind at any one time. Studies have shown that we have difficulty keeping more than about 7 +/-2 items in our mind at the same time, which means it makes a lot of sense to reduce the local complexity.
As noted above, essential complexity can't be removed, just organised, so reducing the local complexity doesn't make the complexity go away, and it might increase the total complexity, but it's still a big win, because it can dramatically reduce the amount of information you need to know, to understand a given piece of code.
CC is all about local complexity, så reducing the CC-number reduces the local complexity, with all the benefits this brings.
|
|