06 - Object-Oriented Design Metrics
06 - Object-Oriented Design Metrics
S. R. Chidamber and C. F. Kemerer, "A Metrics Suite for Object Oriented Design," IEEE Transactions on Software Engineering vol. 20, pp. 476-493, 1994.
W. Li, "Another metric suite for object-oriented programming," The Journal of Systems and Software, vol. 44, pp. 155-162, 1998.
Objectives
2
SWE 439
4
WMC: viewpoints
The larger the number of methods in a class the greater the potential
impact on children, since children will inherit all the methods defined in the
class.
5
Depth of Inheritance Tree (DIT)
Definition: Depth of inheritance of the class is the DIT metric for the class.
In cases involving multiple inheritance, the DIT will be the maximum length
from the node to the root of the tree.
A
DIT(A) = 0
B C DIT(B) = 1
DIT(C) = 0
D
DIT(D) = 2
E DIT(E) = 3
DIT(F) = 4
DIT (C211) = 3 F
Starting the root as 0 6
DIT: viewpoints
The deeper a class is in the hierarchy, the greater the number of methods
it is likely to inherit, making it more complex to predict its behavior.
The deeper a particular class is in the hierarchy, the greater the potential
reuse of inherited methods.
7
Number of Children (NOC)
A
NOC(A) = 1
B C NOC(B) = 1
NOC(C) = 1
D NOC(D) = 2
NOC(E) = 1
E
NOC(F) = 0
DIT (C2) = 3
F
Class C2 has three children—
subclasses C21, C22, and C23 8
NOC: viewpoints
The number of children gives an idea of the potential influence a class has on
the design. If a class has a large number of children, it may require more
testing of the methods in that class.
9
Coupling Between Object classes (CBO)
Definition: CBO for a class is a count of the number of classes that are
coupled to a particular class
Where the methods of one class call the methods or access the variables of the other.
These calls need to be counted in both directions, so the CBO of class A is the size of the
set of classes that class A references and those classes that reference class A.
Since this is a set - each class is counted only once even if the reference operates in
both directions i.e., if A references B and B references A, B is only counted once.
10
CBO Example 1
CBO (A) = 2
B C D
11
CBO Example 2
Book
Customer -orders Order -books
-title : String
-name : String -id : String -author : String
+computeCost() : double * +computeCost() : double * #price : double
+computeCost() : double
1 -creditCard
for (Order o: orders)
RegularBook EBook
o.computeCost(); CreditCardInfo
-shippingCost : double -discount : double
-number : long -sizeMB : int
+computeCost() : double
-expiration : Date +computeCost() : double
Excessive coupling between object classes is harmful to modular design and prevents
reuse. The more independent a class is, the easier it is to reuse it in another application.
A measure of coupling is useful to determine how complex the testing of various parts of
a design is likely to be. The higher the inter-object class coupling, the more rigorous the
testing needs to be.
13
Response For a Class (RFC)
That means all the methods in the class and all the methods that are called
by methods in that class. As it is a set each called method is only
counted once no matter how many times it is called.
16
RFC: viewpoints
The larger the number of methods that can be invoked from a class, the
greater the complexity of the class.
17
Lack of Cohesion in Methods (LCOM)
Cohesion metrics measure how well the methods of a class are related to
each other.
LCOM:
18
LCOM: Example 1
Consider a class C with three methods M1,M2 and M3. Let {I1} = { a , b , c , d ,
e } and {I2} = { a , b , e } and {I3} = {x, y, z}. {I1} ∩ {I2} is nonempty, but {I1} ∩
{I3} and {I2} ∩ {I3} are null sets.
LCOM = max(2-1,0) = 1
19
LCOM: Example 2
a1 a4
m1 m2 m3 m4
20
LCOM: More examples
LCOM =
number of null intersections = {M1,M3}, {M2,M3} = 2
number of nonempty intersections = {M1,M2} = 1
LCOM = max(2-1,0) = 1
LCOM =
number of null intersections = {M1,M3} = 1
number of nonempty intersections = {M1,M2}, {M2,M3} = 2
LCOM = max(1-2,0) = 0
Lack of cohesion implies classes should probably be split into two or more
subclasses.
22
SWE 439
24
Evaluation of DIT metric (Cont’d)
NAC measures the total number of ancestor classes from which a class
inherits in the class inheritance hierarchy.
26
Evaluation of NOC metric
It is not clear why only the immediate subclasses of a class are counted
because a class has influence over all its subclasses, immediate or
nonimmediate.
27
Number of Descendent Classes (NDC)
28
Evaluation of WMC metric
The Weighted Method per Class (WMC) metric is defined as the sum of the complexity of a class’ local
methods.
WMC is used with two intentions: (1) the count of local methods, and (2) the sum of the internal
complexity of all local methods.
But these are two independent attributes of a class.
If the WMC metric is used to measure the number of local methods of a class, the unit for WMC should
be “method”.
If WMC is used to measure the total complexity of all local methods, the WMC metric should have the
same unit as the method‘s internal complexity
E.g., Lines of Code (LOC) or number of independent paths in a method, if McCabe‘s Cyclomatic Complexity is
used to measure a method's complexity.
29
Number of Local Methods (NLM)
The NLM metric is the number of the local methods defined in a class.
30
Class Method Complexity (CMC)
The CMC metric is the summation of the internal structural complexity of all
local methods.
The unit for the CMC metric depends on the unit used to measure the
structural complexity of a method.
31
Evaluation of CBO metric
32
Coupling Through Abstract Data Type (CTA)
The CTA metric is the total number of classes that are used as abstract data types in the
data-attribute declaration of a class.
Two classes are coupled when one class uses the other class as an abstract data type.
Consider the example in the figure below. Class B is coupled with class A through the use of
abstract data type because class B uses class A in its data-attribute declaration.
CTA (A) = 0
CTA (B) = 1
33
Coupling Through Message Passing (CTM)
The CTM metric measures the number of different messages sent out from a class to
other classes excluding the messages sent to the objects created as local objects in the
local methods of the class.
Two classes can be coupled because one class sends a message to an object of another
class, without involving the two classes through inheritance or abstract data types.
Consider the example in the figure below. Both classes A and B are in the same OO design,
class A is coupled with class B because one of A's methods sends a message to B's object.
CTM (A) = 1
CTM (B) = 0
34
SWE 439
a1 a4
m1 m2 m3 m4
m1 m2 m3 m4
𝑳𝑪𝑶𝑴 =𝟑
W. Li, and S. Henry, "Object-Oriented Metrics that Predict Maintainability," Journal of Systems
and Software, vol. 23, no. 2, pp. 111-122, 1993. 36
LCOM (Hitz & Montazeri, 1995)
a1 a4
m1 m2 m3 m4
m1 m2 m3 m4
𝑳𝑪𝑶𝑴 =𝟏
M. Hitz, B. Montazeri, "Measuring Coupling and Cohesion In Object-Oriented Systems," Proc.
37
Int. Symposium on Applied Corporate Computing, Monterrey, Mexico, Oct. 1995.
LCOM (Henderson-Sellers, 1996)
a1 a2 a3 a4
m1 m2 m3 m4
B. Henderson-Sellers, L. Constantine, and I. Graham, "Coupling and Cohesion (Towards a Valid Metrics Suite for
Object-Oriented Analysis and Design)," Object-Oriented Systems, vol. 3, no. 3, pp. 143-158, 1996. 38
Coh (Briand et al., 1998)
a1 a2 a3 a4
m1 m2 m3 m4
Briand, L. C., Daly, J. W., & Wüst, J. (1998). A unified framework for cohesion measurement in object-
oriented systems. Empirical Software Engineering, 3(1), 65–117. 39
Tight Class Cohesion (Bieman & Kang, 1995)
The relative number of directly connected pairs of methods. Two methods are
directly connected if they are directly connected to an attribute.
A method m is directly connected to an attribute, when the attribute appears
within m's body or
within the body of a method that is directly or transitively invoked by method m.
m1 m2 m3 m4 Access a3
J. M. Bieman, and B.-K. Kang, "Cohesion and reuse in an object-oriented system," Proc.
40
Symposium on Software reusability (SSR '95), pp. 259-262, 1995.
Loose Class Cohesion (Bieman & Kang, 1995)
j i
m X
J. M. Bieman, and B.-K. Kang, "Cohesion and reuse in an object-oriented system," Proc.
41
Symposium on Software reusability (SSR '95), pp. 259-262, 1995.
TCC vs. LCC example
Access a1 Access a3 𝟐
𝑻𝑪𝑪 =
𝟔
a1 a2 a3 a4
m1 m2 m3 m4
m1 m2 m3 m4
m1 m2 m3 m4
𝟑
𝑳𝑪𝑪=
𝟔
m2 accesses both a1 and a3
m3 is indirectly connected to a1
m1 is indirectly connected to a3 42
Class Cohesion (Bonja & Kidanmariam, 2006)
𝟏
𝟐 𝟏
𝑪𝑪 = =
C. Bonja, and E. Kidanmariam, "Metrics for class cohesion and similarity between methods," 𝟔 𝟏𝟐
ACM Southeast Regional Conference, pp. 91-95, 2006.
43
Cohesion Metrics
J. Al Dallal, "Measuring the Discriminative Power of Object-Oriented Class Cohesion Metrics," in IEEE Transactions on Software Engineering, vol. 37, no. 6, pp. 788-804,
Nov.-Dec. 2011 44
Coupling Metrics
Jehad Al Dallal, “Empirical Exploration for the Correlation between Class Object-Oriented Connectivity-Based Cohesion and Coupling”, International Journal of Computer
and Information Engineering, Vol:9, No:4, 2015 45
Coupling relation taxonomy
Enrico Fregnan, Tobias Baum, Fabio Palomba, Alberto Bacchelli, “A survey on software coupling relations and tools”, Information and Software
Technology, Volume 107, 2019, Pages 159-178, 46
Summary
47