Professional Documents
Culture Documents
Software Metrics
Software Metrics
Software metrics:
Complexity
Alexander Serebrenik
Assignments
Number of
files, classes
Amount of
functionality
Metrics
• Control
x flow
Structure
Data flow
Modularity
/ SET / W&I 18/03/15 PAGE 4
Complexity metrics: Halstead (1977)
• Operators:
• traditional (+,++, >), keywords (return, if, continue)
• Operands
• identifiers, constants
• Length: N = N1 + N2
• Vocabulary: n = n1 + n2
• Volume: V = N log2n
• Insensitive to lay-out
• VerifySoft:
− 20 ≤ Volume(function) ≤ 1000
− 100 ≤ Volume(file) ≤ 8000
/ SET / W&I 18/03/15 PAGE 6
Halstead metrics: Example
void sort ( int *a, int n ) { • Ignore the function definition
int i, j, t; • Count operators and operands
if ( n < 2 ) return;
for ( i=0 ; i < n-1; i++ ) {
for ( j=i+1 ; j < n ; j++ ) {
if ( a[i] > a[j] ) {
t = a[i];
a[i] = a[j];
a[j] = t;
} Total Unique
} Operators N1 = 50 n1 = 17
} Operands N2 = 30 n2 = 7
} V = 80 log2(24) ≈ 392
/ SET / W&I Inside the boundaries [20;1000]
18/03/15 PAGE 7
Total Unique
Further Halstead metrics Operators N1 n1
Operands N2 n2
• Volume: V = N log2n
• Difficulty: D = ( n1 / 2 ) * ( N2 / n2 )
• Sources of difficulty: new operators and repeated
operands
• Example: 17/2 * 30/7 ≈ 36
• Effort: E = V * D
• Time to understand/implement (sec): T = E/18
• Running example: 793 sec ≈ 13 min
• Does this correspond to your experience?
• Bugs delivered: E2/3/3000
• For C/C++: known to underapproximate
• Running example: 0.19
• Syntactic sugar:
• Structural complexity:
• Control flow Commonly Graph-
• Data flow represented based
as graphs metrics
• Modularity
• Number of vertices
• Number of edges
• Maximal length
(depth)
In general
• v(G) = #edges - #vertices + 2
• Linux kernel
• Multiple
versions and
variants
• Production
(blue dashed)
• Development
(red)
• Current 2.6
(green)
• Halstead’s V ≈ 392
if ( n < 2 ) return;
for ( i=0 ; i < n-1; i++ ) { • McCabe’s v(G) = 5
for ( j=i+1 ; j < n ; j++ ) {
if ( a[i] > a[j] ) { • LOC = 14
t = a[i];
a[i] = a[j]; • MI1 ≈ 96
a[j] = t;
} • Easy to maintain!
}
}
}
/ SET / W&I 18/03/15 PAGE 18
Comments?
[Liso 2001]
50 sin 2.46 perCM
Peaks:
• 25% (OK),
• 1% and
81% - ???
Better:
• 0.12 ≤ K ≤ 0.2
50
• Percentage as a fraction
40
[0;1] – [Thomas 2008, Ph.D. thesis]
MI contribution
30
20
• The more comments – the
better?
10
0
0.0 0.2 0.4 0.6 0.8 1.0
percentage of comments
• Size, Halstead
volume and
McCabe
complexity
decrease
• % comments
decreases as well
• BUT they use the
[0;1] definition, so
the impact is
limited
A B
Cohesion Lo Hi
Coupling Hi Lo
• Squares are modules, lines are calls,
ends of the lines are functions.
• Which design is better?
• Fan-in of M: number of
modules calling functions
in M
• Fan-out of M: number of
modules called by M
• Dashed - #changed
20
complexity range
0
Henry-Kafura complexity
• Highly complex procedures
are difficult to change but
they are changed often!
• Complexity comes from the
three largest procedures
• Mozilla
• Shepperd
version
• Above: Σ
the metrics
over all
modules
• Below: 3
largest
modules
• What does
this tell?
• Complexity metrics
• Halstead’s effort Bugs
• McCabe (cyclomatic)
• Henry Kafura/Shepperd 0.95 0.96
(information flow) 0.89
0.38 0.84
• Harry,Kafura,Harris 1981
• 165 Unix procedures
Halstead
• What does this tell us?
Number of
files, classes
Amount of
functionality
Metrics
• Control
x flow
Structure
Data flow
Modularity
/ SET / W&I 18/03/15 PAGE 29
From imperative to OO
• WMC/unity
• Dark: Basili et al.
• Light: Gyimothy
et al. [Mozilla 1.6]
• Red: High-
quality NASA
system
/ SET / W&I 18/03/15 PAGE 32
Chidamber and Kemerer
• WMC/unity
• Gyimothy et al.
• Average
java.awt.C java.awt.F
omponent rame
java.lang.
JFrame MyFrame
Object
/ SET / W&I 18/03/15 PAGE 34
DIT – what is good and what is bad?
• NOC – number of
children
• CBO – coupling between
object classes
• RFC - #methods that can
be executed in response
to a message being Significance of CK metrics to
received by an object of predict the number of faults
that class.
• More or less
“exponentially”
distributed
Software metrics:
More metrics
Alexander Serebrenik
Assignments
• Chidamber Kemerer:
[BBM] 180 classes
⎧ P − Q if P > Q
LCOM (C ) = ⎨
Discriminative ability
⎩ 0 otherwise
is insufficient
where
• P = #pairs of distinct methods in C What about methods
that do not share instance that use get/set
variables instead of direct
• Q = #pairs of distinct methods in C access?
that share instance variables
/ SET / W&I 18/03/15 PAGE 41
First solution: LCOMN
LCOMN (C ) = P − Q
LCOM LCOMN
• m – number of methods
⎛ 1 v ⎞
⎜ ∑ m(Vi ) ⎟ − m
• v – number of variables (attrs) ⎝ v i =1 ⎠
• m(Vi) - #methods that access Vi 1− m
• Cohesion is maximal: all methods access all variables
m(V ) = m and LCOM = 0
i
Sato, Goldman,
Kon 2007
• LCOM values
• 0, no methods
• 1, cohesive component
• 2 or more, lack of cohesion
Level Metrics
Method LOC, McCabe
Class WMC, NOC, DIT, LCOM (and
variants), LCC/TCC
Packages ???
PAGE 55
Aggregation techniques
• Metrics-independent
• Applicable for any metrics to be aggregated
− Traditional: mean, median...
− “By no means”
− Econometric: inequality indices
• Metrics-dependent
• Produce more precise results
• BUT: need to be redone for any new metrics
• Based on fitting probability distributions
• Lorenz curve:
• % of income shared by the lower % of the population
• Gini = A/(A+B)
A • Since A+B = 0.5
B Gini = 2A
• Metrics-independent
• Applicable for any metrics to be aggregated
• Are the results also metrics-independent?
• Based on econometrics
Ø Metrics-dependent
• Produces more precise results
• BUT: needs to be redone for any new metrics
• Based on fitting probability distributions
Robles et al. 2006: LOC in Debian 2.0 (left) and 3.0 (right)
• Advantages
• Statistically more sound (no dependency on the end-
points of the bins)
• Produces smooth curves
• Disadvantages
• Statistically more complex
• Parameter tuning might be a challenge
-4 -2 0 2 4 6 8
Histogram: every value is
a rectangle.
Shape is a “sum” of the
rectangles.
/ SET / W&I 18/03/15 PAGE 69
Kernel density estimation: Formally
n
1 ⎛ x − xi ⎞
f ( x) = ∑ K ⎜ ⎟
nh i =1 ⎝ h ⎠
Where
• n – number of observations
• h – a smoothing parameter, the “bandwidth”
• K – a weighting function, the “kernel”
-1 0 1
3
4
Density
3
2
Density
1
1
0
0
6
5
4
Density
3
2
1
0
0.0 0.2 0.4 0.6 0.8
Dn
Tamai, Nakatani.
S, Roubtsov, vd Brand
Negative binomial
Exponential distribution
distribution
• Family of distributions is chosen based on shape
• If the parameters fitting is not good enough try a
different one!
/ SET / W&I 18/03/15 PAGE 73
Sometimes well-known distributions do not
really seem to match
• Exponential distribution:
− λx
f ( x ) = λe Distribution
of Dn.
• However, support is [0;1] rather than
[0;∞)! 1
• Since ∫ f ( x )dx = 1 − e −λ
0 f ( x)
• we normalize: g ( x) = 1
∫ f ( x)dx
0
2
X = 2
n
(Oi − Ei )
• The test statistic
where i =1 Ei
∑
• O – observed frequency of the result i
• E – expected frequency of the result i
• Visual inspection
• Is this a real “trend” or just
noise?
• Time-series analysis
• Simplest form: linear
regression with time
2DD23 - Time
series analysis
Strong and forecasting
• Aggregation:
• Metrics-independent
− Applicable for any metrics to be aggregated
− Traditional: mean, median...
− “By no means”
− Econometric: inequality indices
• Metrics-dependent
− Produce more precise results
− BUT: need to be redone for any new metrics
− Based on fitting probability distributions
Level Metrics
Method LOC, McCabe
Class WMC, NOC, DIT, LCOM (and
variants), LCC/TCC
Packages Aggregation
Direct metrics???
• Size:
• number of classes/interfaces
• number of classes in the subpackages
• Dependencies
• visualization
• à la fan-in and fan-out
− Marchesi’s UML metrics
− Martin’s Dn: abstractness-instability balance or “the
normalized distance from the main sequence”
− PASTA
• Do you still remember aggregations of class metrics?
PK1 or R:
C e:
[Martin 1994] [JDepend] [Martin 2000]
PAGE 83
Fan-in
Sato,
Goldman,
Kon 2007
PAGE 87
What does balance mean?
PAGE 88
Dn – Distance from the main sequence
Abstractness =
#AbstrClasses/#Classes Dn =
| Abstractness +
zone of Instability – 1 |
1
uselessness
[R.Martin 1994]
main sequence
zone of pain
0 Instability =
1
Ce/(Ce+Ca)
PAGE 89
Normalized distance from the main sequence
[Hilton 2009]
JBoss
PAGE 91
PASTA [Hautus 2002]
• Metrics
• Similarly “fan-in/fan-out”: based on dependencies
between packages
• Go beyond calculating numbers of dependencies
• No empirical validation of
the metrics
• Recall…
Absolute: Relative:
Churned LOC, Deleted LOC,
File Count, Weeks of Churn,
Churn Count, Files Churned
• Construct a statistical
model
• Training set: 2/3 of the
Windows Set binaries
• Check the quality of the
prediction
• Test set: remaining
binaries
• Three models
• Right: all relative churn
metrics are taken into
account
• Package metrics
• Directly defined: Dn, Marchesi metrics, PASTA
• Results of aggregation
• Churn metrics