Software Engineering Methodologies

OOP Metrics

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

Metrics for Object-Oriented Design

Depth of Inheritance Tree

DIT

  • DIT is the maximum length from a node to the root (base class)
  • Lower-level subclasses inherit a number of methods, making behavior harder to predict
  • However, more methods are reused in higher DIT trees

Number Of Children

NOC

  • NOC is the number of subclasses immediately subordinate to a class
  • As NOC grows, reuse increases
  • But the abstraction may be diluted

NOO

NOO

  • Number of Operations Overridden
  • A large number for NOO indicates possible problems with the design
  • Poor abstraction in the inheritance hierarchy

NOA

NOA

  • Number of Operations Added
  • The number of operations added by a subclass
  • As operations are added, it is farther away from the super class
  • As depth increases, NOA should decrease

LCOM

LCOM: Lack of Cohesion in Methods

  • Often poorly described
  • Class Ck with n methods M1,…, Mn
  • Ij is the set of instance variables used by Mj

LCOM1

  • There are n such sets I1, … , In
    • P = {(Ii, Ij) | (IiIj) = Ø }
    • Q = {(Ii, Ij) | (IiIj) ≠ Ø }
  • If all n sets Ii are Ø then P = Ø
  • LCOM1 = |P| - |Q|, if |P| > |Q|
  • LCOM1 = 0 otherwise

Example LCOM1

  • Let class C have methods M1, M2, M3

  • I1 = { a, b, c, d, e }

  • I2 = { a, b, e}

  • I3 = { x, y, z }

  • P = { (I1, I3), (I2, I3) }

  • Q = { (I1, I2) }

  • LCOM = |P| - |Q|

  •   = |{ (I1, I3), (I2, I3) }| - |{ (I1, I2) }|

  •   = 2 - 1

  •   = 1

LCOM Explanation

LCOM1 = |P| - |Q|, if |P| > |Q|

LCOM1 = 0 otherwise

  • LCOM is the number of empty intersections minus the number of non-empty intersections
  • This is a notion of the degree of similarity of methods.
  • If two methods use common instance variables, then they are similar
  • LCOM of zero is not maximally cohesive
  • |P| = |Q| or |P| < |Q|

CS: Class Size

  • Total number of operations (inherited, private, public)
  • Number of attributes (inherited, private, public)
  • May be an indication of too much responsibility for a class

Specialization Index

SI = [NOO * L] / Mtotal

  • L is the level in the class hierarchy
  • Mtotal is the total number of methods
  • Higher values indicate a class in a hierarchy that does not conform to the abstraction

Method Inheritance Factor

MIF = ∑ni=1 Mi(Ci) / ∑ni=1 Ma(Ci)

  • Ratio of inherited methods to the total number of methods in a class
  • Mi(Ci) is the number of methods inherited and not overridden in Ci
  • Ma(Ci) is the number of methods that can be invoked with Ci
  • Md(Ci) is the number of methods declared in Ci
  • All that can be invoked = new or overloaded + things inherited
  • MIF is [0,1]
  • MIF close to 1: little specialization
  • MIF close to 0: large change

Coupling Factor

CF = ∑ij is_client(Ci, Cj) / (TC2 - TC)

  • is_client(Ci, Cj) = 1 iff a relationship exists between the client class and the server class, 0 otherwise
  • (TC2 - TC) is the total number of relationships possible (Total Classes2 * diagonal)
  • CF is [0,1] with 1 meaning high coupling

Polymorphism Factor

PF = Mo(Ci) / ∑ki=1 Mn(Ci) * DC(Ci)

  • Mn() is the number of new methods
  • Mo() is the number of overriding methods
  • DC() number of descendent classes of a base class
  • The number of methods that redefine inherited methods, divided by the maximum number of possible distinct polymorphic situations