Cyclomatic Complexity with Example:

The

**cyclomatic complexity**metric is based on the number of decisions in a program. It is important to testers because it provides an indication of the amount of testing (including reviews) necessary to practically avoid defects. In other words, areas of code identified as more complex are can-didates for reviews and additional dynamic tests. While there are many ways to calculate cyclomatic complexity, the easiest way is to sum the number of binary decision statements (e.g. if, while, for, etc.) and add 1 to it. A more formal definition regarding the calculation rules is provided in the glossary.Below is a simple program as an example:

IF A = 354

THEN IF B > C

THEN A = B

ELSEA= C

ENDIF

ENDIF

Print A

The

**control flow**generated from the program would look like Figure 3.2.The control flow shows seven nodes (shapes) and eight edges (lines), thus using the formal formula the cyclomatic complexity is 8-7 + 2 = 3. In this case there is no graph called or subroutine. Alternatively one may calculate the cyclomatic complexity using the decision points rule. Since there are two decision points, the cyclomatic complexity is 2 + 1 = 3.

good one

ReplyDeleteOMFG! I finally get it! you made me understand something in 2mins that my teacher couldn't do in 2 hours!

ReplyDeleteLol. Same here.

Deletesame!

Deleteagreed..:)

Deletesame here bro :D

Deleteits very simple example. please explain by taking different one

ReplyDeletegood

ReplyDeletevery correctly explained. please show example for while loop also

ReplyDeleteSimple and effective. Thanks!

ReplyDeleteGiven the Following program

ReplyDeleteIF X = Z

THEN Statement 2;

END

McCabe’s Cyclomatic Complexity is :

a. 2

b. 3

c. 4

d. 5

What is an ans of this question.

Following the rule, it must be 1+1=2.

Delete3 nodes; 2 edges; P=1 => (E-N) +2p => (3-2) +2*1 => 1+2 = 3. PLEASE COEECT ME IF I AM WRONG

DeleteCyclomatic Complexity = 2; ( 3- 3 + 2*1 )

DeleteE=3; N=3; P=1 [ Pis the Number of discontinuous paths; no sub routines here]

a.2

DeleteGood One

ReplyDeletegive an example with for loop

ReplyDeleteloop

DeleteHow did you got the value of P?

ReplyDeleteP is exit node, in above case its value is 1

Deleteso formula E-N+2P =M

8-7+2(1)=3

Cyclomatic Complexity = 2; ( 3- 3 + 2*1 )

ReplyDeleteE=3; N=3; P=1; Number of discontinuous paths; (no sub routines here)

can you write the c++ code for counting cyclomatic complexity???

DeleteCould you please explain this with another complex example?

ReplyDeleteThis comment has been removed by the author.

ReplyDeletecould you please explain McCabe’s Cyclomatic Complexity for this question?

ReplyDeleteIF X <>= Z

THEN Statement 2;

END

<>= symbol is take as two conditions here ?

answer would be 2+1 based on your explanation right?

ya u r correct @ aparna

Deletehttp://www.sqaforums.com/showflat.php?Number=649568

DeleteAs per the equation Cyclomatic Complexity is M= (E-N)+2P

DeleteHere in this question there are 3 Nodes 2 Edges and 2 Paths if we put it in a control flow diagram .so the answer would be (2-3)+2*2 = -1+4= 3

What does it mean with the condition <>= as the case above: IF X<>=Z?

DeleteCould some one please find the CP for the below code ?

ReplyDeleteRegards

Sanjay

IF X < Y

THEN Statement 1;

ELSE IF Y >= Z

THEN Statement 2;

END

decision points + 1 = 2+1 = 3

DeleteCP is 3

4

DeleteWhich formula is correct for calculating Cyclometic complexity?

ReplyDeleteCyclometic complexity = # of Edges - # of Nodes + # of predicate nodes

M = E - N + P

or

Cyclometic complexity = # of Edges - # of Nodes + 2(# of predicate nodes)

M = E - N + 2P

Thanks Much.

if(x>y)

ReplyDeletethen print x

please tell me, what will be the Cyclometic complexity?

Should be 2;

ReplyDelete(Decision points) + 1= 1+1 = 2

its copy of rex balck book......

ReplyDeleteplz read rex black book for ISTQB

Oh thank you , this helped me so much , i missed the lecture which talking about cyclomatic complexity, and now i got it :D

ReplyDeleteVery True

ReplyDeletethank you.........you made me understand this whole comlex thing in 1 second

ReplyDeletecan any one write the c++ code for counting cyclomatic complexity???

ReplyDeletenice. We train Software Testing for free to poor graduates. If you know anybody please refer. The link for that is http://payilagam.com/free-software-training-courses-in-chennai

ReplyDeleteHi! Just wanted to know for clarification, where this two numbers 2 and 1 comes from?

ReplyDeleteAre they constants no matter how many decision nodes are present in the function?

Great post!

ReplyDeleteheres another post on Cyclomatic Complexity in ISTQB : http://www.idyllic-software.com/blog/cyclomatic-complexity-in-istqb/

Can any one give ans for this pseudo c code snippet?

ReplyDeleteIf(c1 & c2) s1;

Else if(c3 || c4) s2;

Else s3;

S4;

S5;

End

Here c1,2,3,4 are conditions and s1,2,3,4,5 are statements

Thanx!!

Since decision point = 2

Deletecc = 2+1=3

If two code segments have Cyclomatic complexities of N1 and N2 respectively, what will be the Cyclomatic complexity of the juxtaposition of the two code segments?

ReplyDeletea) N1+N2

b) N1+N2+1

c) N1+N2-1

d) N1*N2

The answer is C. Can somebody explain how?

N1 = E1 - N1 + 2, N2 = E2 - N2 + 2. As juxtaposition of the two programs increases 1 edge, we subtract it from the sum. Therefore, N1 + N2 -1.

DeleteWhat would be the Cyclomatic complexity of the following program?

ReplyDeleteint find-maximum(int i,int j, int k){

int max;

if(i>j) then

if(i>k) then max=i;

else max=k;

else if(j>k) max=j

else max=k;

return(max);

}

a) 2

b) 3

c) 4

d) 5

anserc

DeleteIf branch coverage has been achieved on a unit under test, which of the following is coverage is implicitly implied?

ReplyDeletea) Path coverage

b) Multiple condition coverage

c) Statement coverage

d) Data flow coverage