You are on page 1of 18

FIT3SQA – Software Quality Assurance

Lecture 10

Software Measurement Techniques


Formal Methods

Faculty of Information Technology


Hanoi University
Cyclomatic Complexity
• A decision control (such as the if statement)
introduces branches to the program’s flow graph.
• Cyclomatic Complexity is the measure of a
program’s control complexity.
Calculating Cyclomatic Complexity
𝑉 𝐺 =𝐸−𝑁+2=𝑃+1
• E: number of edges.
• N: number of nodes.
• P: number of nodes that contain conditions.
 V(G) is the number of independent
paths through the flow graph.

• Example:
– Nodes with condition: 1, 2, 3.
– Cyclomatic Complexity: 4.
Cyclomatic complexity and testing
• 1 - 4: low complexity – easy to test
• 5 - 7: moderate complexity – tolerable
• 8 - 10: high complexity – refactoring should be
considered to ease testing
• 11+: very high complexity – very difficult to
test
Example: calculate the Cyclomatic Complexity of the following Flow Graph

• Using the number of nodes and edges:


V(G) = 10 - 9 + 2 = 3
• Using the number of (binary) decision nodes:
V(G) = 2 + 1 = 3
Example: calculate the Cyclomatic Complexity of the following Flow Graph

• Count the number of simple decisions:


V(G) = 2 + 1 = 3
Halstead’s Metrics
• Complexity metrics based on the number of
operators and operands in a program.
Halstead’s Metrics
• Used as a predictor of maintenance effort.
• No predictive power for development effort.
Weighted Methods per Class
• Weights are often defaulted to 1, in which
case it is the number of methods per class.
– If combined with other metrics such as Cyclomatic
Complexity, it equals the sum of the complexity
for all methods within a class.
• It gives an indicator of the amount of effort
required to implement and test a class.
– High values suggest that the class is too large and
should be split.
Depth of Inheritance Tree
• Calculation: the number of inheritance layers
that make up a given class hierarchy.
• Large values imply more design complexity,
but also more reuse.
Number of Children
• The number of immediate successors of a
class.
• Large values imply:
– The class has been divided too much.
– Increased need for testing
– More reuse potential
– The class should be redesigned
Coupling Between Object Classes
• The measure of how many other classes rely
on the class and vice versa.
– Two classes are considered coupled when
methods declared in one class use methods or
instance variables of the other class.
• Large values imply:
– More complexity
– Reduced maintainability
– Reduced reusability
Formal Methods
• Various mathematical techniques used for the
formal specification and development of
software.
• Formal languages are precise, unlike natural
languages which are ambiguous.
• Some approaches:
– Model-oriented: VDM, Z notation
– Calculus-based: Communicating Sequential Processes
(CSP), Calculus of Communicating Systems (CCS)
Finite State Machine
• A finite state machine (FSM) is an abstract
mathematical machine that consists of a finite
number of states. It includes a start state q0 in
which the machine is in initially; a finite set of
states Q; an input alphabet Σ; a state
transition function δ; and a set of final
accepting states F (where F ⊂ Q).
• Among formal methods, FSM is useful and
relatively easy to learn.
Finite State Machine
• Q: a finite set of states.
• ∑: the alphabet of input characters.
• δ: a mapping function that describes the
conditions to change from one state to
another.
• q0: the initial state (q0 ∈ Q).
• F: the set of accepted final states (F ⊆ Q) – an
FSM terminates when it reaches one of the
states in F.
Finite State Machine
• The input of an FSM is a string.
– The input string must only contain characters from
the alphabet ∑.
• One character is fed into the FSM at a time.
• The transition function δ can be specified
using a table. For example:
State a b c
q0 q1 q0 q0
q1 q0 q2 q0
q2 q0 q2 q3
q3 q0 q0 q0
FSM Example
• An FSM that accepts either “hello” or “hi”.
• Q = {q0, q1, q2, q3, q4, q5, q6}
• ∑ = {e, h, i, l, o}
• F = {q5}
State e h i l o
q0 q6 q1 q6 q6 q6
q1 q2 q6 q5 q6 q6
q2 q6 q6 q6 q3 q6
q3 q6 q6 q6 q4 q6
q4 q6 q6 q6 q6 q5
q5 q6 q6 q6 q6 q6
q6 q6 q6 q6 q6 q6
FSM Graph
• A graph can be used to describe the state
transition function.

State 0 1
a a b
b c a
c b c

You might also like