17-Object-Oriented Metrics
17-Object-Oriented Metrics
1
Cyclomatic Complexity
• Cyclomatic Complexity is the quantitative measure of the number of
linearly independent paths in it.
• It is a software metric used to describe the complexity of a program.
• Complexity is computed as:
▪ Cyclomatic complexity V(G) for a flow graph G is defined as
V(G) = E − N + 2
where E is the number of flow graph edges and N is the number of
flow graph nodes.
Cyclomatic Complexity
1
• Cyclomatic Complexity:
V(G) = 10 edges − 10 nodes + 2 = 2.
White Box Testing
Control Flow Testing
• There are two independent paths: 1
• Path 1: 1-2-3-4-5-6-8-9-10
• Path 2: 1-2-3-4-5-7-8-9-10
CK Metrics
• Depth of inheritance tree (DIT)
• DIT is the length of the longest path from a given class to the root class in the
inheritance hierarchy.
• As DIT increases, the lower classes in the hierarchy inherit a greater number
of data and methods, thus making their behavior more difficult to understand
and causing testing to require more effort.
• A large DIT value implies greater design complexity, but also greater reuse.
• Example: Consider a class hierarchy where Animal is the root class, Mammal
inherits from Animal, Dog inherits from Mammal, and Bulldog inherits from
Dog. Here, Bulldog has a DIT of 3.
CK Metrics
• Number of children (NOC)
• NOC is the number of immediate subclasses inheriting from a particular
class.
• Higher NOC values suggest more complexity, as the class must support more
inheriting classes.
• High NOC can be a sign of over-generalization or it may indicate that a class is
designed for extensibility.
• Example: Suppose we have a Shape class, and three classes inherit from it:
Circle, Square, and Triangle. Here, NOC for Shape is 3.
CK Metrics
• Coupling between object classes (CBO)
• CBO measures the number of other classes to which a given class is coupled,
either by using their methods or properties.
• High coupling makes the code less modular and harder to maintain or test,
as changes in one class may impact other classes.
• Example: If a Customer class in a banking application calls methods from
Account, Loan, and Transaction classes, CBO for Customer would be 3.
CK Metrics
• Response for a class (RFC)
• The number of methods that can potentially be executed in response to a
message received by an object of a given class.
RFC = M + R
• M = number of methods in the class
• R = number of remote methods directly called by methods of the class
• As the RFC value increases, testing effort and design complexity also increase.
• RFC should be kept low.
CK Metrics
• Lack of cohesion in methods (LCOM)
• Lack of Cohesion in Methods (LCOM) measures how closely related the
methods in a class are, based on the instance variables (attributes) they
access.
• One common approach to calculate LCOM is based on method pairs, which
calculates cohesion by examining how frequently methods share attributes.
CK Metrics
• Lack of cohesion in methods (LCOM)
• Identify pairs of methods.
• For each pair:
• Count it as "connected" if they access at least one common attribute.
• Count it as "disconnected" if they do not share any attributes.
• Calculate LCOM as follows:
LCOM = Number of disconnected method pairs − Number of connected method pairs
• If LCOM is positive, it indicates a lack of cohesion (i.e., methods are
disconnected).
• If LCOM is zero or negative, the class has good cohesion.