0% found this document useful (0 votes)
32 views8 pages

Cyclomatic Complexity

Uploaded by

bixero6166
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)
32 views8 pages

Cyclomatic Complexity

Uploaded by

bixero6166
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/ 8

cyclomatic

complexity
Cyclomatic complexity is a software engineering metric that was introduced by Thomas J.
McCabe in 1976. This metric is a numerical score that represents the number of different
paths a program can take during its execution.

A higher score means more execution paths (higher complexity), while a lower score means
fewer paths (lower complexity).

What is Cyclomatic Complexity?


The cyclomatic complexity of a code section is the quantitative measure of the number of linearly independent
paths in it. It is a software metric used to indicate the complexity of a program. 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 the second command might immediately follow the first command.
For example, if the source code contains no control flow statement then its cyclomatic complexity will be 1, and the
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.
Formula for Calculating Cyclomatic Complexity
Mathematically, for a structured program, the directed graph inside the control flow is the edge
joining two basic blocks of the program as control may pass from first to second.
So, cyclomatic complexity M would be defined as,
M = E – N + 2*P
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
What is the formula for cyclomatic complexity?
The formula to calculate cyclomatic complexity is relatively straightforward. Start by observing the program’s control-flow
graph—a graphical representation of all paths that might be traversed through a program during its execution.
1.Control-flow graph examples of simple programming scenarios
(a) if-then-else statement
(b) while loop
(c) a natural loop with an if statement break in the middle
(d) a loop with two entry points
Source

Looking at a control-flow graph, we would represent the number of edges


in the graph as E, while N represents the number of nodes in the graph.

The formula for cyclomatic complexity C is:

C = E - N + 2P
•if (age > 60)
•else if (membershipDuration > 5).

Each decision point can lead to different outcomes:

•The condition age > 60 is true


•The first condition is false, but membershipDuration > 5 is true.
•Both conditions are false.

As we look at the control-flow graph, our total number of edges is 8.


Our total number of nodes is 7. And because the entire function is a
single connected component, P = 1. When we apply the formula, we
get this:

C = E - N + 2P
= 8 - 7 + 2(1)
=3
IF X = 300 THEN
IF Y > Z
THEN X = Y
ELSE X = Z
END IF END
IF PRINT X

V(G) = E - N + 2 * P = 8 - 7 + 2 * 1 = 3

You might also like