Cyclomatic Complexity (ST) Week 10
Cyclomatic Complexity (ST) Week 10
Week 10
OUTLINE OF LECTURE
• Definition of cyclomatic complexity
• Importance/uses of measuring cyclomatic complexity
• Mathematical formula for calculating cyclomatic
complexity
• Advantages/Disadvantages of Cyclomatic Complexity
Technique
Cyclomatic complexity Definition
• Cyclomatic complexity is a software metric that
measures the complexity of a program by
analyzing the number of independent paths
through the code. It provides a quantitative
measure of the complexity of a program based on
its control flow structure.
• Cyclomatic complexity was first introduced by
Thomas McCabe in 1976 as a way to measure the
complexity of code in terms of the number of decision
points and the number of independent paths through a
program. The higher the cyclomatic complexity, the
more difficult it is to understand, test, and maintain the
code.
• It is computed using the Control Flow Graph of the program. The nodes in the
graph indicate the smallest group of commands of a program, and a directed
edge in it connects the two nodes i.e. if second command might immediately
follow the first command.
• For example, if source code contains no control flow statement then its
cyclomatic complexity will be 1 and source code contains a single path in it.
Similarly, if the source code contains one if condition then cyclomatic
complexity will be 2 because there will be two paths one for true and the
other for false.
• Mathematically, for a structured program, the directed graph inside control
flow is the edge joining two basic blocks of the program as control may pass
from first to second.
FORMULA FOR CALCULATION
• Cyclomatic complexity is calculated based on the number of decision
points or control flow structures in the code, such as loops, conditionals,
and switches. Each decision point contributes to the cyclomatic
complexity, and the formula for calculating cyclomatic complexity is:
•M=E-N+2
Where:
• M is the cyclomatic complexity
• E is the number of edges in the control flow graph
• N is the number of nodes in the control flow graph
USES OF CYCLOMATIC
COMPLEXITITY TECHNIQUE
• 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 program’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.
Example
Let's consider a simple program that takes three inputs, x, y, and z, and computes the maximum
value among them:
public int max(int x, int y, int z) {
int max = x;
if (y > max) {
max = y;
}
if (z > max) {
max = z;
}
return max;
}
• To calculate the cyclomatic complexity of this program, we
can construct a control flow graph. The control flow graph is
a graphical representation of the program's control flow,
where nodes represent statements or decision points, and
edges represent control flow paths between them.
A = 10
IF B > C
THEN A = B
ELSE A = C
ENDIF
Print A
Print B
Print C
CONTROL FLOW OF GRAPH
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.