The Artima Developer Community
Sponsored Link

Java Community News
Cyclomatic Complexity as a Measure of Code Quality

10 replies on 1 page. Most recent reply: Apr 5, 2006 3:11 AM by Choy Rim

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 10 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Cyclomatic Complexity as a Measure of Code Quality Posted: Mar 30, 2006 8:03 AM
Reply to this message Reply
Summary
Cyclomatic complexity (CC) measures code complexity by showing how many different paths code execution can take. Studies on code quality have shown correlation between CC and bugs in a given piece of code, and CC has been used to complement code coverage metrics. A recent article shows how to measure CC in Java code with open-source tools.
Advertisement

Cyclomatic complexity (CC) measures code complexity by showing how many paths code execution can take. For instance, if code can choose between 10 different execution paths, then that piece of code has a CC value of 10.

The greater the CC value, the more complex the code. Studies have correlated CC metrics with defects, showing that code with a CC of 10 or higher tends to have a lot more bugs than simpler code.

Because each execution path should, ideally, be tested, CC metrics also help determine how many unit tests should be written for a given piece of code. While 100% test coverage is not often desirable, the CC metrics at least give an idea of the number of possible test.

In a recent IBM DeveloperWorks article, Andrew Glover explains CC, shows why CC metrics complement code coverage, and demonstrates open-source tools to measure the CC of Java code.

Because cyclomatic complexity is such a good indicator of code complexity, there is a strong relationship between test-driven development and low CC values. When tests are written often[...], developers have the tendency to write uncomplicated code because complicated code is hard to test. If you find that you're having difficulty writing a test, it's a red flag that the code under test may be complex.
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?


Jeff Ratcliff

Posts: 242
Nickname: jr1
Registered: Feb, 2006

Re: Cyclomatic Complexity as a Measure of Code Quality Posted: Mar 30, 2006 8:45 AM
Reply to this message Reply
> 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?

Maruis Marais

Posts: 290
Nickname: maruis
Registered: Aug, 2005

Re: Cyclomatic Complexity as a Measure of Code Quality Posted: Mar 30, 2006 3:13 PM
Reply to this message Reply
You should have a look at my Code Project article explaining how we use CC to find complex code in our project. It also helps to see which member in the team needs more couching on OO and Refactoring concepts.

Read it here: http://www.codeproject.com/useritems/practicalexp.asp

Maruis Marais

Posts: 290
Nickname: maruis
Registered: Aug, 2005

Re: Cyclomatic Complexity as a Measure of Code Quality Posted: Mar 30, 2006 3:35 PM
Reply to this message Reply
We want all our methods in a class to be below 10. But personally I prefer it to be below 5. One of the reasons we found high CC is due to many .NET developers not having good OO skills and still writing procedural code. This is after many years of OOA/D being around.

Why is that?

Well, I think it might be due to VB6 mentality. But that is another subject on it's own.

TDD pushes people into a more OO way of thinking, but that is probably why people find TDD extremely difficult to do.

They don't get concepts like:
1. Code against interfaces
2. Tell don't ask
3. Don't test the dependency, substitude it (Injection)
4. etc...

We use <a href="http://www.campwoodsw.com/sm20.html">SourceMonitor</a> at the moment. We'd like to include it in our build as a as after compile task. Source Monitor has the ability to save the results of each check, giving you a nice history of how the code is evolving.

We've also used Reflector's Add-in before.

Chris Dailey

Posts: 56
Nickname: mouse
Registered: Dec, 2002

Re: Cyclomatic Complexity as a Measure of Code Quality Posted: Mar 30, 2006 3:45 PM
Reply to this message Reply
devMetrics for .NET, JavaNCSS for Java.

Yes, fixing cyclomatic complexity sometimes retains the complexity but just moves it to somewhere not measured by CC. However, lower-CC code is more testable, so there are huge benefits.

There seems to be a trend. Reduce complexity of methods increases the complexity of classes. Reducing complexity of classes increases the complexity of coupling. Reducing complexity of coupling usually involves minimizing dependencies, and seems to be the last (but perhaps most important) stop in the migration of complexity.

For Java, coupling can be kept track of easily with a product called OptimalAdvisor; I don't know if there are any other competing products. How I wish there were an equivalent product for .NET.

Ivan Kaschenko

Posts: 2
Nickname: om600
Registered: Mar, 2006

Re: Cyclomatic Complexity as a Measure of Code Quality Posted: Mar 31, 2006 12:06 AM
Reply to this message Reply
We use CheckStyle for code quality checking. See http://checkstyle.sourceforge.net/.

Achilleas Margaritis

Posts: 674
Nickname: achilleas
Registered: Feb, 2005

Re: Cyclomatic Complexity as a Measure of Code Quality Posted: Mar 31, 2006 1:53 AM
Reply to this message Reply
> 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?

Cyclomatic complexity and number of bugs only shows that imperative programming has a real problem with execution paths: there are few people that can hold really big and complex imperative execution paths in their minds.

A functional program with many evaluation paths has much less bugs due to freeing the programmer from the constraints of a) sequence b) destructive update.

But computers are imperative by nature, so an improvement in programming languages would be to limit the imperative features of a programming language to those necessary to speed up functional algorithms.

Terje Slettebø

Posts: 205
Nickname: tslettebo
Registered: Jun, 2004

Re: Cyclomatic Complexity as a Measure of Code Quality Posted: Mar 31, 2006 4:44 AM
Reply to this message Reply
> > 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.

Jeff Ratcliff

Posts: 242
Nickname: jr1
Registered: Feb, 2006

Re: Cyclomatic Complexity as a Measure of Code Quality Posted: Mar 31, 2006 8:01 AM
Reply to this message Reply
> 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.
>

You expressed that well. I think there is a fuzziness in the fundamental character of essential and incidental complexity, however. In real-world applications we don't get to fully choose the technology we use to implement an application. The complexity introduced by that technology is technically incidental but shares essential complexity's characteristic of being beyond our control to eliminate.

> 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.

Of course, the manipulation of global variables or global objects in a function pretty much expands its "locality" to global scope, so any tool that measures complexity should take this into account.

Jussi Santti

Posts: 3
Nickname: jussi
Registered: Jun, 2005

Re: Cyclomatic Complexity as a Measure of Code Quality Posted: Apr 4, 2006 5:03 AM
Reply to this message Reply
Dear list,

There is a recurring controversy on the (dis)merits of exceptions-used-as-control-structures aka exception-used-as-return-codes aka Java checked exception. How do the various CC tools support these and what are the conclusions? Do the result shed some light on this issue?

Regards Jussi

Choy Rim

Posts: 3
Nickname: choyrim
Registered: Apr, 2006

Re: Cyclomatic Complexity as a Measure of Code Quality Posted: Apr 5, 2006 3:11 AM
Reply to this message Reply
i use NDepend and SourceMonitor. High CC numbers indicate stinky code, alluding to flaws in design.

it doesnt translate directly to "bad code" but is quite close.

Flat View: This topic has 10 replies on 1 page
Topic: Cyclomatic Complexity as a Measure of Code Quality Previous Topic   Next Topic Topic: Bytecode Metaprogramming for More Flexible Systems

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use