Pradeep's Blog

Google

Sunday, October 16, 2005

Easy way to find Cyclomatic complexity…


My project required me to find the cyclomatic complexity of a given method (in a java class file). I used a java class file manipulation tool named ASM Using ASM I was able to get the total number of jump instruction in a method. I will explain this with an example. Consider the methods 1 & 2 .
Method 1:

Public void check(int c) {
if (c>0){ //if block } //other instructions.
}

Method 2:
Public void check(int c) {
for (int i=0;i<5>

Both methods have a cyclomatic complexity of 2 (just 2 possible paths of execution). But if I was to use the Instruction count method 1 has just has One JumpInstruction but method two has 2 JumpInstruction (one for comparing the if(i<5))>

0 Comments:

Post a Comment

<< Home