0% found this document useful (0 votes)
535 views17 pages

Halstead's Metrics

Halstead's software science uses metrics derived from counting operators and operands in source code to measure software properties like size, effort, and cost. It performs well for predicting software maintenance efforts compared to other techniques. McCabe's cyclomatic complexity defines an upper bound on the number of independent paths through code and can be calculated in different ways from a control flow graph or by counting decision statements. Both Halstead's metrics and McCabe's complexity provide quantitative measures of software properties.

Uploaded by

Prachi
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)
535 views17 pages

Halstead's Metrics

Halstead's software science uses metrics derived from counting operators and operands in source code to measure software properties like size, effort, and cost. It performs well for predicting software maintenance efforts compared to other techniques. McCabe's cyclomatic complexity defines an upper bound on the number of independent paths through code and can be calculated in different ways from a control flow graph or by counting decision statements. Both Halstead's metrics and McCabe's complexity provide quantitative measures of software properties.

Uploaded by

Prachi
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/ 17

Halstead’s Metrics

• Halstead’s software science can be used to


derive some interesting results starting with a
few simple assumptions.
• Halstead’s software science is especially useful
for estimating software maintenance efforts.
• In fact, it outperforms both empirical and
heuristic techniques when used for predicting
software maintenance efforts.
• Halstead’s software science is an analytical technique
– to measure size,
– development effort, and
– development cost of software products.
• Halstead used a few primitive program parameters to
develop the expressions for
– over all program length,
– potential minimum value,
– actual volume, effort, and
– development time.
Halstead’s Metrics: Definitions
According to Halstead:
• A computer program is an implementation
of an algorithm considered to be a collection
of tokens which can be classified as either
operators or operands.
• All Halstead’s metrics are functions of the counts of these
tokens:
By counting the tokens and determining which are
operators and which are operands, the following base
measures can be collected:
 n1 : Number of distinct operators.
 n2: Number of distinct operands.
 N1: Total number of occurrences of operators.
 N2: Total number of occurrences of operands.
 In addition to the above, Halstead defines the following:
 n1*: Number of potential operators.
 n2*: Number of potential operands.
Halstead refers to n1* and n2* as the minimum possible
number of operators and operands for a module and a
program respectively.
• Halstead's metrics are all defined based on its
set of base quantities (n1, n2, N1, N2, n1* and
n2*).
• The length (N) of a program P is :
– N = N1 + N2
• The vocabulary (n) of a program P is :
– n = n1 + n2
• The volume (V) of a program P is defined as :
– V = N * log2n
• Difficulty level (D) =   ( n1 / 2 ) * ( N2 / n2 )
• Program level (L) = 1/D
• Effort to implement (E) =   V * D
Cyclomatic complexity
• McCabe’s cyclomatic complexity defines an
upper bound for the number of linearly
independent paths through a program.
• McCabe’s metric does not directly identify the
linearly independent paths, but it informs
approximately how many paths to look for.
• There are three different ways to compute the
cyclomatic complexity.
• The answers computed by the three methods are
guaranteed to agree.
Cyclomatic complexity : Method 1
• Given a control flow graph G of a program, the
cyclomatic complexity V(G) can be computed as:

where N is the number of nodes of the control


flow graph and E is the number of edges in the
control flow graph

E=7 and N=6. Therefore, the


cyclomatic complexity = 7-6+2 = 3.
Cyclomatic complexity
Cyclomatic complexity : Method 2
• An alternative way of computing the cyclomatic complexity of a
program from an inspection of its control flow graph is as
follows:

•In the program’s control flow graph G, any region enclosed by


nodes and edges can be called as a bounded area.
•This is an easy way to determine the McCabe’s cyclomatic
complexity.
•The number of bounded areas increases with the number of
decision paths and loops.
•Therefore, the McCabe’s metric provides a quantitative
measure of testing difficulty and the ultimate reliability.
•Therefore the cyclomatic complexity, computing with
this method is also 2+1 = 3.
Cyclomatic complexity : Method 3
• The cyclomatic complexity of a program can also
be easily computed by computing the number of
decision statements of the program.
• If N is the number of decision statement of a
program, then the McCabe’s metric is equal to
N+1.
• Therefore the cyclomatic complexity, computing
with this method is also 2+1 = 3.
public void howComplex() {
int i=20;

while (i<10) {
System.out.printf("i is %d", i);
if (i%2 == 0) {
System.out.println("even");
} else {
System.out.println("odd");
}
}
}
V(G) = 2 enclosed area + 1 = 3
IF A = 10 THEN
IF B > C THEN
A=B
ELSE
A=C
ENDIF
ENDIF
Print A
Print B
Print C
The Cyclomatic
complexity is
calculated using
the above control
flow diagram that
shows seven
nodes(shapes) and
eight edges (lines),
hence the
cyclomatic
complexity is 8 - 7
+2=3

You might also like