Monday, 12 December 2011

Cyclomatic Complexity with Example


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.




50 comments:

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

    ReplyDelete
  2. its very simple example. please explain by taking different one

    ReplyDelete
  3. very correctly explained. please show example for while loop also

    ReplyDelete
  4. Simple and effective. Thanks!

    ReplyDelete
  5. Given the Following program
    IF 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.

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

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

      Delete
    3. Cyclomatic Complexity = 2; ( 3- 3 + 2*1 )
      E=3; N=3; P=1 [ Pis the Number of discontinuous paths; no sub routines here]

      Delete
  6. give an example with for loop

    ReplyDelete
  7. How did you got the value of P?

    ReplyDelete
    Replies
    1. P is exit node, in above case its value is 1
      so formula E-N+2P =M
      8-7+2(1)=3

      Delete
  8. Cyclomatic Complexity = 2; ( 3- 3 + 2*1 )
    E=3; N=3; P=1; Number of discontinuous paths; (no sub routines here)

    ReplyDelete
    Replies
    1. can you write the c++ code for counting cyclomatic complexity???

      Delete
  9. Could you please explain this with another complex example?

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. could you please explain McCabe’s Cyclomatic Complexity for this question?

    IF X <>= Z
    THEN Statement 2;
    END

    <>= symbol is take as two conditions here ?

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

    ReplyDelete
    Replies
    1. http://www.sqaforums.com/showflat.php?Number=649568

      Delete
    2. As per the equation Cyclomatic Complexity is M= (E-N)+2P
      Here 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

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

      Delete
  12. Could some one please find the CP for the below code ?

    Regards
    Sanjay

    IF X < Y
    THEN Statement 1;
    ELSE IF Y >= Z
    THEN Statement 2;
    END

    ReplyDelete
    Replies
    1. decision points + 1 = 2+1 = 3

      CP is 3

      Delete
  13. Which formula is correct for calculating Cyclometic complexity?
    Cyclometic 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.

    ReplyDelete
  14. if(x>y)
    then print x

    please tell me, what will be the Cyclometic complexity?

    ReplyDelete
  15. Should be 2;
    (Decision points) + 1= 1+1 = 2

    ReplyDelete
  16. its copy of rex balck book......
    plz read rex black book for ISTQB

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

    ReplyDelete
  18. thank you.........you made me understand this whole comlex thing in 1 second

    ReplyDelete
  19. can any one write the c++ code for counting cyclomatic complexity???

    ReplyDelete
  20. nice. 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

    ReplyDelete
  21. Hi! Just wanted to know for clarification, where this two numbers 2 and 1 comes from?
    Are they constants no matter how many decision nodes are present in the function?

    ReplyDelete
  22. Great post!
    heres another post on Cyclomatic Complexity in ISTQB : http://www.idyllic-software.com/blog/cyclomatic-complexity-in-istqb/

    ReplyDelete
  23. Can any one give ans for this pseudo c code snippet?
    If(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!!

    ReplyDelete
    Replies
    1. Since decision point = 2
      cc = 2+1=3

      Delete
  24. 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?
    a) N1+N2
    b) N1+N2+1
    c) N1+N2-1
    d) N1*N2
    The answer is C. Can somebody explain how?

    ReplyDelete
    Replies
    1. 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.

      Delete
  25. What would be the Cyclomatic complexity of the following program?
    int 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

    ReplyDelete
  26. If branch coverage has been achieved on a unit under test, which of the following is coverage is implicitly implied?
    a) Path coverage
    b) Multiple condition coverage
    c) Statement coverage
    d) Data flow coverage

    ReplyDelete