Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

Module-3

Measuring Internal Product Size


and Structure
Software Complexity metrics
How to represent program structure?
Questions?
Basic Control Structure
Basic Control Structure
Control Flow Graph
Control Flow Graph
Control Flow Graph
Control Flow Graph
Control Flow Graph
Control Flow Graph
Control Flow Graph
Control Flow Graph
Prime Flow Graphs
Sequencing and Nesting
Sequencing and Nesting
S-Structured Graph
S-Structured Graph
Prime Decomposition
Hierarchical Measure
Depth of Nesting
Example for Depth of Nesting
Cyclomatic Complexity
• Cyclomatic complexity is probably the most widely
used complexity metric in software engineering.
• Defined by Thomas McCabe in 1976 and based on
the control flow structure of a program.
• It is easy to understand, easy to calculate and it gives
useful results.
• It's a measure of the structural complexity of a
procedure.
• the cyclomatic complexity is calculated by measuring
the linearly independent path in the graph and is
represented by a single number.
Cyclomatic Complexity
CC Number
Complexity Number Meaning
Structured and well written code
1-10 High Testability
Cost and Effort is less
Complex Code
10-20 Medium Testability
Cost and effort is Medium
Very complex Code
20-40 Low Testability
Cost and Effort are high
Not at all testable
>40
Very high Cost and Effort
Calculating Cyclomatic Complexity-

Cyclomatic complexity is calculated using the control flow


representation of the program code.

In control flow representation of the program code,


 Nodes represent parts of the code having no branches.
 Edges represent possible control flow transfers during
program execution
There are 3 commonly used methods for calculating the cyclomatic complexity-
Method-01:

Cyclomatic Complexity = Total number of closed regions in the control flow graph + 1

Method-02:
V (G) is the maximum number of independent paths in the graph
V(G) = E - N + 2
Cyclomatic Complexity = E – N + 2

Here-
E = Total number of edges in the control flow graph
N = Total number of nodes in the control flow graph

Method-03:

Cyclomatic Complexity = P + 1

Here,
P = Total number of predicate nodes contained in the control flow graph

Note-
Predicate nodes are the conditional nodes.
They give rise to two branches in the control flow graph.
Flow graph notation for a program
Calculate cyclomatic complexity for the given code-
We draw the following control flow graph for
1.IF A = 354 the given code-
2.THEN IF B > C
3.THEN A = B
4.ELSE A = C
5.END IF
6.END IF
7.PRINT A
Using the above control flow graph, the cyclomatic complexity may be calculated as-

Method-01:
Cyclomatic Complexity

= Total number of closed regions in the control flow graph + 1

=2+1

=3

Method-02: Method-03:
Cyclomatic Complexity
Cyclomatic Complexity
=E–N+2
=P+1
=8–7+2
=2+1
=3
=3
Calculate cyclomatic complexity for the given flow graph
Example
01. insertion_procedure (int a[], int p [], int N)
02. {
03. int i,j,k;
04. for (i=0; i<=N; i++) p[i] = i;
05. for (i=2; i<=N; i++)
06. {
07. k = p[i];
08. j = 1;
09. while (a[p[j-1]] > a[k]) {p[j] = p[j-1]; j--}
10. p[j] = k;
11. }
12. }
Numbering the statements
01. insertion_procedure (int a[], int p [], int N)
02. {
03. (1) Int i,j,k;
04. (2) for ((2a)i=0; (2b)i<=N; (2c)i++)
05. (3) p[i] = i;
06. (4) for ((4a)i=2; (4b)i<=N; (4c)i++)
07. {
08. (5) k=p[i];j=1;
09. (6) while (a[p[j-1]] > a[k]) {
10. (7) p[j] = p[j-1];
11. (8) j--
12. }
13. (9) p[j] = k;
14. }
01. insertion_procedure (int a[], int p [], int N)

02. {

03. (1) Int i,j,k;

04. (2) for ((2a)i=0; (2b)i<=N; (2c)i++)

05. (3) p[i] = i;

06. (4) for ((4a)i=2; (4b)i<=N; (4c)i++)

07. {

08. (5) k=p[i];j=1;

09. (6) while (a[p[j-1]] > a[k]) {

10. (7) p[j] = p[j-1];

11. (8) j--

12. }

13. (9) p[j] = k;

14. }
Cyclomatic complexity

• calculate the cyclomatic complexity

– Count the number of regions on the graph: 4


– No. of predicates (red on graph) + 1 : 3 + 1 = 4
– No of edges – no. of nodes + 2: 14 – 12 + 2 = 4
Cyclomatic Complexity
Software Architecture
Morphology
Example
Tree Impurity
Tree Impurity

You might also like