Friday, September 7, 2012

Cyclomatic complexity






Cyclomatic complexity (or conditional complexity) is a software metric (measurement). It was developed by Thomas J. McCabe, Sr. in 1976 and is used to measure the complexity of a program. It directly measures the number of linearly independent paths through a program's source code.

Cyclomatic Complexity metric is based on the number of decision in a program. 

Cyclomatic Complexity :- The number of independent paths through a program. 

Cyclomaticcomplexity is defined as: L – N + 2P 

where- 

L = the number of edges/links in a graph- 

N = the number of nodes in a graph-

P = the number of disconnected parts of the graph (e.g. a called graph or subroutine) 

It doesn't take place in Manual testing .  Neither it is a part of White Box...   In fact It is the Part of Verification Static testing , It is done with the help of Static Analysis tools..

   Cyclomatic Complexity can be calculated using either of two rules
        1) Formal Formula 
        2 ) Decision Point rule 
Decision point rule : Which says CC = Total number of Decision Point + 1.
Ex 1 :-  void test::Test3()
                    {
                        string a,b,c,d,e,f;
                        if(((a==b)&&(e==f))||(f==a)||(a==b))
                                c=d;
                            else
                                c=f;
                    }

 To find the cyclomatic Complexity of the above program :-   

 Cyclomatic Complexity :- Total number of Condition + 1. 

 hence in this Example:-   there is only one if condition so the Cyclomatic Complexity is : 1+ 1 = 2.

Ex 2:-

void func1()
{
        int a,b,c,d;
        while (1)
        {
            if (a>b)
            print a
    else if (b>c)
            print b
    else
            print c
    }

}

 Now what is a Decision Point ..?

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.

    hence in your program above , the Cyclomatic Complexity is 3 + 1= 4.

Ex 3:-

        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 below flow chart.

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.



No comments:

Post a Comment

Thread

Native Thread Demon Thread Non-Demon Thread Native Thread: - Any Method/Thread which is mapped to OS is called Native Thread or Method. Demo...