Two Laptop Bag
The moose likes Java in General and the fly likes Cyclomatic Complexity Calculation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cyclomatic Complexity Calculation" Watch "Cyclomatic Complexity Calculation" New topic

Cyclomatic Complexity Calculation

vikram solanki

Joined: Dec 07, 2010
Posts: 12

I was trying to find the cyclomatic complexity (CC) of a Java program and i am having a few problems. i tried generating CCby using the below code...

but i don't think this is right... because i am not taking into account the nested ifs and for loops and i feel not all parameters have been added. How exactly are the nested if's and for's should be considered...
Also i need the average CC of the program... now is the average complexity (total CC / Number of methods)? when i calculated the avg CC of the below attached Java file i found it to be 2.26 but when i generated it through a tool i got the value as 1.6. Also do method calls come into account for CC
can anyone please tell me how exactly to deal with this??

vikram solanki

Joined: Dec 07, 2010
Posts: 12
The Java File that i tested for CC is given below...

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Not an answer to your question, but be aware that the code does not seem to ignore keywords that occur in comments and string literals. You're most likely better off using a proper Java source code parser, like those that are available for Antlr, JavaCC and SableCC.

Also note that "foreach", "or", "and" and "xor" are not Java keywords.
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4642

And neither is elseif.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Dan Be

Joined: Feb 14, 2011
Posts: 1
This article may help you find what you are looking for:
I agree. Here's the link:
subject: Cyclomatic Complexity Calculation
It's not a secret anymore!