0% found this document useful (0 votes)
14 views18 pages

Cyclomatic Complexity (ST) Week 10

Cyclomatic complexity is a software metric that quantifies the complexity of a program by analyzing its control flow and the number of independent paths through the code. It is calculated using the formula M=E-N+2, where E is the number of edges and N is the number of nodes in the control flow graph. While it helps in improving code coverage and guiding testing efforts, it has limitations such as not accounting for data complexity and potentially misleading results in simple decision structures.

Uploaded by

Hamza Main
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views18 pages

Cyclomatic Complexity (ST) Week 10

Cyclomatic complexity is a software metric that quantifies the complexity of a program by analyzing its control flow and the number of independent paths through the code. It is calculated using the formula M=E-N+2, where E is the number of edges and N is the number of nodes in the control flow graph. While it helps in improving code coverage and guiding testing efforts, it has limitations such as not accounting for data complexity and potentially misleading results in simple decision structures.

Uploaded by

Hamza Main
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Cyclomatic Complexity

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.

• Here is control flow graph for “max” function


(1)
|
[max = x]
|
/ | \
(2) (3) (4)
| | |
[y > max] [z > max] |
| | |
[max = y] [max = z]|
| | |
(5) (6) (7)
| | |
[return max] [return max]
In this graph, the nodes represent the following:
1.The start of the function
2.The initialization of max to x
3.The first if statement comparing y to max
4.The second if statement comparing z to max
5.The assignment of y to max if y is greater than max
6.The assignment of z to max if z is greater than max
7.The return statement
• There are seven nodes and nine edges in the control flow graph, so the
cyclomatic complexity of this program is:
•M=E-N+2=9-7+2=4
• Based on the cyclomatic complexity of this program, we can determine that at
least four test cases are required to achieve full statement coverage. These test
cases would need to cover all independent paths through the code, including
the following:
• Test case 1: x = 1, y = 2, z = 3
• Test case 2: x = 2, y = 3, z = 1
• Test case 3: x = 3, y = 1, z = 2
• Test case 4: x = 1, y = 1, z = 1
• By using cyclomatic complexity to guide our testing, we can ensure
that all critical paths through the code are tested, and that we have a
high level of confidence in the correctness of the program.
EXAMPLE 2:

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 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.

You might also like