Cyclomatic Complexity , How to Calculate Cyclomatic Complexity , Properties of Cyclomatic complexity ,Use of Cyclomatic Complexity , Advantages Disadvantages of Cyclomatic Complexity

Cyclomatic complexity is a software metric used to measure the complexity of a program. Thomas J. McCabe developed this metric in 1976.McCabe interpre

 

Cyclomatic Complexity

Cyclomatic complexity is a software metric used to measure the complexity of a program. Thomas J. McCabe developed this metric in 1976.McCabe interprets a computer program as a set of a strongly connected directed graph. Nodes represent parts of the source code having no branches and arcs represent possible control flow transfers during program execution. The notion of program graph has been used for this measure, and it is used to measure and control the number of paths through a program. The complexity of a computer program can be correlated with the topological complexity of a graph.

How to Calculate Cyclomatic Complexity?

McCabe proposed the cyclomatic number, V (G) of graph theory as an indicator of software complexity. The cyclomatic number is equal to the number of linearly independent paths through a program in its graphs representation. For a program control graph G, cyclomatic number, V (G), is given as:

              V (G) = E - N + 2 * P

E = The number of edges in graphs G

N = The number of nodes in graphs G

P = The number of connected components in graph G.

Example:

Cyclomatic Complexity

Properties of Cyclomatic complexity:

Following are the properties of Cyclomatic complexity:

  1. V (G) is the maximum number of independent paths in the graph
  2. V (G) >=1
  3. G will have one path if V (G) = 1
  4. Minimize complexity to 10

M = E – N + 2P 

where, 
E = the number of edges in the control flow graph 
N = the number of nodes in the control flow graph 
P = the number of connected components 

  • Construction of graph with nodes and edges from code.
  • Identification of independent paths.
  • Cyclomatic Complexity Calculation
  • Design of Test Cases

Let a section of code as such: 
 

A = 10
   IF B > C THEN
      A = B
   ELSE
      A = C
   ENDIF
Print A
Print B
Print C

 

Control Flow Graph of above code

 

cyclomatic-complexity

The cyclomatic complexity calculated for above code will be from control flow graph. The graph shows seven shapes(nodes), seven lines(edges), hence cyclomatic complexity is 7-7+2 = 2. 

Use of Cyclomatic Complexity: 
 

  • Determining the independent path executions thus proven to be very helpful for Developers and Testers.
  • It can make sure that every path have been tested at least once.
  • Thus help to focus more on uncovered paths.
  • Code coverage can be improved.
  • Risk associated with program can be evaluated.
  • These metrics being used earlier in the program helps in reducing the risks.

Advantages of Cyclomatic Complexity:.

  • It can be used as a quality metric, gives relative complexity of various designs.
  • It is able to compute faster than the Halstead’s metrics.
  • It is used to measure the minimum effort and best areas of concentration for testing.
  • It is able to guide the testing process.
  • It is easy to apply.

Disadvantages of Cyclomatic Complexity:

  • It is the measure of the programs’s control complexity and not the data complexity.
  • In this, nested conditional structures are harder to understand than non-nested structures.
  • In case of simple comparisons and decision structures, it may give a misleading figure.


Post a Comment

© Software Engineering. The Best Codder All rights reserved. Distributed by