Friday 21 June 2013

Code Complexity

Code Complexity is influenced by the factors :
  • Average Hierarchy Height
  • Average Number of Derived Classes
  • Afferent Coupling (no. of other packages that depend upon classes within a package )
  • Efferent Coupling ( no.of other packages that classes from this package depend upon )
  • Number of “if” condition statements and many more.........!
Cyclomatic Complexity (MCC)
It was developed by Thomas J. McCabe in 1976 and is used to indicate the complexity of a program. It directly measures the number of linearly independent paths through a program's source code.

Cyclomatic complexity is computed using the control flow graph of the program.
The cyclomatic complexity of a section of source code is the count of the number of linearly independent paths through the source code.
For ex : If the source code contained no decision points such as IF statements or FOR loops, the complexity would be 1, since there is only a single path through the code.
If the code had a single IF statement containing a single condition there would be two paths through the code, one path where the IF statement is evaluated as TRUE and one path where the IF statement is evaluated as FALSE.

Mathematically the complexity M is then defined as :
M = E − N + 2 P
where
E = no. of edges of the graph
N = no. of nodes of the graph
P = no. of connected components (exit nodes)

A simpler method of computing the MCC is demonstrated in the equation below. If D is the number of decision points in the program, then
M = D + 1 (Since, each decision point normally has two possible paths)

Some standard values of Cyclomatic Complexity are shown below:
M = D + 1
Assessment
1-10
not much risk
11-20
moderate risk
21-50   
high risk
51+
Not testable, very high risk

0 comments: