Software Engineering: Experiment No-9
Software Engineering: Experiment No-9
1. Objective (s):
After completing this experiment, student will be able to:
Identify the basic blocks in a program module, and draw its control flow
graph (CFG).
Identify the linearly independent paths from CFG
Determine the cyclomatic complexity of a module in a program
2. Theory:
Control Flow Graph
A control flow graph (CFG) is a directed graph where the nodes represent
different instructions of a program, and the edges define the sequence of
execution of such instructions. Figure 1 shows a small snippet of code (compute
the square of an integer) along with it's CFG. For simplicity, each node in the
CFG has been labeled with the line numbers of the program containing the
instructions. A directed edge from node #1 to node #2 in figure 1 implies that
after execution of the first statement, the control of execution is transferred to the
second instruction.
A real life application seldom could be written in a few lines. In fact, it might
consist of thousand of lines. A CFG for such a program is likely to become very
large, and it would contain mostly straight-line connections. To simplify such a
graph different sequential statements could be grouped together to form a basic
block. A basic block is a [ii, iii] maximal sequence of program instructions I1,
I2, ..., In such that for any two adjacent instructions Ik and Ik+1, the following
holds true:
The size of a CFG could be reduced by representing each basic block with a node.
To illustrate this, let's consider the following example.
Figure-03
The CFG with basic blocks is shown for the above code in figure 4.
The first statement of a basic block is termed as leader. Any node x in a CFG is
said to dominate another node y (written as x dom y) if all possible execution
paths that goes through node y must pass through node x. The node x is said to be
a dominator. In the above example, line #s 1, 3, 4, 6, 7, 9, 10 are leaders. The
node containing lines 7, 8 dominate the node containing line # 10. The block
containing line #s 1, 2 is said to be the entry block; the block containing line # 10
is said to be the exit block.
If any block (or sub-graph) in a CFG is not connected with the sub-graph
containing the entry block, that signifies the concerned block contains code,
which is unreachable while the program is executed. Such unreachable code can
be safely removed from the program. To illustrate this, let's consider a modified
version of our previous code:
Figure-05
Terminologies
Path
A path in a CFG is a sequence of nodes and edges that starts from the initial node
(or entry block) and ends at the terminal node. The CFG of a program could have
more than one terminal nodes.
A linearly independent path is any path in the CFG of a program such that it
includes at least one new edge not present in any other linearly independent path.
A set of linearly independent paths give a clear picture of all possible paths that a
program can take during it's execution. Therefore, path-coverage testing of a
program would suffice by considering only the linearly independent paths. In
figure 3 we can find four linearly independent paths:
1 - 3 - 6 - (7, 8) - 10
1 - 3 - 6 - (7, 8) - 9 - 10
1 - 3 - (4, 5) - 6 - (7, 8) - 10
1 - 3 - (4, 5) - 6 - (7, 8) - 9 - 10
Note that 1 - 3 - (4, 5) - 3 - (4, 5) - 6 - (7, 8) - 10, for instance, won't qualify as a
linearly independent path because there is no new edge not already present in any
of the above four linearly independent paths.
Let G be a a given CFG. Let E denote the number of edges, and N denote the
number of nodes. Let V(G) denote the Cyclomatic complexity for the CFG. V(G)
can be obtained in either of the following three ways:
Method #2: V(G) could be directly computed by a visual inspection of the CFG:
V(G) = Total number of bounded areas + 1 It may be noted here that structured
programming would always lead to a planar CFG.
where COUNT( V(Gi) ) gives the total number of procedures (methods) in the
program (class).
A set of threshold values for Cyclomatic complexity has been presented in , which
we reproduce below.
It has been suggested that the Cyclomatic complexity of any module should not
exceed 10 [vi], [4]. Doing so would make a module difficult to understand for
humans. If any module is found to have Cyclomatic complexity greater than 10,
the module should be considered for redesign. Note that, a high value of V(G) is
possible for a given module if it contains multiple cases in C like switch-case
statements. McCabe had exempted such modules from the limit of V(G) as 10
[vi].
Merits
Demerits
3. Outcome of Study:
4. Output: