Object-Oriented Metrics Alex Evans Jonathan Jakse Cole Fleming Matt Keran Michael Ababio
What is OOM? Measurements ManagePredictImprove
LOCOM - Lack of CohesionWMC - Weighted Method CountDIT - Depth of Inheritance TreeNOC - Number of ChildrenRFC - Response for ClassCBO - Coupling Between Object Classes Chidamber-Kemerer (CK) Suite
Metrics of OO Design (MOOD) CF - Coupling FactorAHF - Attribute Hiding FactorMHF - Method Hiding FactorMIF - Method Inheritance FactorAIF - Attribute Inheritance FactorPF - Polymorphism Factor
Coupling Factor (CF) Low coupling Improves modular design Encourages reuse of components
Coupling Example Coupling Example Coupling Example Number of non-inheritance couplings: 7 Max Number of Couplings: (9 * 8) / 2 = 36 CF = 7/36 =
Lack of Cohesion (LOCOM)
Correct class subdivision Decrease complexity Reduced risk of errors Increased reusability Low LOCOM
Cohesion Example Number of methods in class: 4 %x = 2/4 = 0.5 %y = ¼ = 0.25 %z = ¼ = 0.25 LOCOM = 1 - ( )/4 = 0.75
Hide attributes Large AHF Prevent access Attribute Hiding Factor (AHF)
Hide methods Large MHF Prevent Access Method Hiding Factor (MHF)
Encapsulation Example
AHF Results AttributesNumber classes not visible in%invisibilityAHF GUIfield1685%57% GUIfield2685% GUIfield3685% P1field1571% P2field1685% P3field1685% P4field1571% DS1field % DS2field %
MHF Results AttributesNumber classes not visible in%invisibilityMHF GUImethod()685%77% P1method1()571% P1method2()685% P1method3()571% P2method1()457% P2method2()685% P3method1()571% P4method1()571% P4method2() %
Depth of Inheritance Tree (DIT) Max length from class node to parent Classes should be distributed evenly DIT
Low DIT Less complex Less reuse
Number of Children (NOC) Number of direct subclasses for each class NOC
Low NOC Reduced testing Decrease complexity Less risk of faults Less reuse
DIT and NOC Example
Some Results ClassInheritance DepthNumber of Children Entity03 Bullet12 Player10 Enemy13 Zombie20 Boss21 NeonCat30
Most of the classes are subclasses: Evenly Distributed DIT Results Analysis
Most of the classes have no children: Shallow NOC Results Analysis
McCabe’s Cyclomatic Complexity CC Necessary to compute WMC Evaluate complexity of algorithm Create Control Flow Graph
Low CC Decreased testing Increased Understanding
Weighted Method Count (WMC) Low WMC Increased usability Increased readability Improved Understanding
Edges: 8 Nodes: 7 CC: = 3 CC Example
Weighted Method Count Example Redo classes with WMC 25 and 29 Decrease complexity
Response For a Class RFC Number of methods invoked by class Methods executed in response to message
Low RFC Reduced complexity Increased understanding Improved readability
RFC for Class B
Class B can call: FooA(), FooB(), FooB1(), FooC() (4) FooA() calls FooA1() (1) FooC() calls FooC1() (1) FooB1() and FooC1() call System.out.print () (1) RFC for Class B = 7
Class B Flow Chart
RFC Graphical Representation
References