understanding software metrics
TRANSCRIPT
Tushar Sharma
Agenda
OO metrics and their
classification
Using metrics in practice
Advanced stuff…
Metrics: What and
why
Software Metrics - What & Why?A software metric is a measure of some property of a software system.
Why?
“Measurement is the first step that leads to control and eventually to improvement. If you can’t measure
something, you can’t understand it.” (by James Harrington)
Tushar Sharma
Object-oriented Metrics
Size
Complexity
Cohesion & Coupling
Project Namespace Type Method
LOC NOC NON
LOC NOC
LOC NOM NOF DIT NC RFC
LOC NP
WMC CC
LCOM CBO
Fan-in/Fan-out
Tushar Sharma
OO Metrics - Size
• Large entities make the comprehension difficult.
• Large entities indicate presence of various smells.
LOC Lines Of Code NON Number Of Namespaces NOC Number Of Classes NOM Number Of Methods NOF Number Of Fields
Tushar Sharma
OO Metrics - SizeThe response set of a class is a set of methods that can potentially be executed in response to a message received by an object of that class.
• Larger the number of methods that can be invoked, the greater the complexity of the class.
• If a large number of methods can be invoked in response to a message, the testing and debugging of the class becomes more complicated .
RFC Response For Class
Tushar Sharma
OO Metrics - Size (Hierarchy)• Deep inheritance trees make
the comprehension difficult. • 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 behaviour.
• Again, wide inheritance trees make the comprehension difficult.
• A wide hierarchy indicates missing intermediate inheritance level that in turn may lead to code duplication.
DIT Depth of Inheritance Tree NC Number of Children
Tushar Sharma
OO Metrics - Cohesion and Coupling
Consider a class C with n methods M1, M2..., Mn. Let {Ij} = set of instance variables used by method Mi. There are n such sets I1 ,…, In
• P = {(Ii, Ij) | (Ii ∩ Ij ) = ∅} • Q = {(Ii, Ij) | (Ii ∩ Ij ) ≠ ∅} LCOM = |P| - |Q|, if |P| > |Q| = 0, otherwise
• High LCOM indicate the violation of the Single Responsibility Principle.
LCOM Lack of Cohesion Of Methods
Tushar Sharma
OO Metrics - Cohesion and CouplingCBO for a class is a count of the number of other classes to which it is coupled.
• Excessive coupling between object classes is detrimental 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 are likely to be. The higher the inter-object class coupling, the more rigorous the testing needs to be.
CBO Coupling Between Object classes
Tushar Sharma
OO Metrics - Complexity
• CC - It is a quantitative measure of the number of linearly independent paths through a program's source code.
• WMC – Sum of CC of each method in the class
• The number of methods and the complexity of methods involved is a predictor of how much time and effort is required to develop and maintain the class.
CC Cyclomatic Complexity WMC Weighted Methods per Class
Tushar Sharma
Using metrics in practice - tools
C/C++
CppDepend Infusion
Source Monitor
C#
Designite NDepend
Java
Infusion Source Monitor
Tushar Sharma
Using metrics in practice
• There is no one single widely accepted standard for metrics thresholds.
• There are some guidelines and rules of thumb!
Metric - Recommended Threshold
LOC (Method) - 50 LOC (File) - 500 NOC - 9 DIT - 5 LCOM - 0.80 NOM - 30 CBO - 6 CC - 10 WMC - 100 % Duplication - 4%
Metric thresholds
Tushar Sharma
Using metrics in practiceWhat if you want to perform an analysis with customized metric thresholds
http://www.designite-tools.com
Tushar Sharma
What can be done with metrics data• Indicator of “Structural Quality” • Refactoring candidates
Lack of Cohesion
0
500
1000
1500
2000
2500
3000
3500
createFax
FaxSender
PrintSender
SASContext
SASFoldableTableCellWidget
SASFoldableTable
SASSortFoldableTable
SASFoldableDynamicColumnScrollTable
SASFoldableDynamicTable
SASSortTable
SASLaterality
SASProcedureTree
SASSiriusLPA
SASProcedureTreeUserObject
PortalApplication
PortalRISApplication
Radiologist
SASServerHessianProxy
ClientConfig
UserPreferences
SASCriteria
SASSpeechTextPane
Dispatcher
SpeechAudioUtils
Classes
Lack
of c
ohes
ion
valu
e
0 120 180
Threshold
• Trend analysis • Proof – Improved quality
Tushar Sharma
What can be done with metrics data• Detect code/design smells:
✦ Insufficient Modularization - WMC or NOM ✦ Hub-like Modularization – Fan-in/Fan-out ✦ Deep Hierarchy – DIT ✦ Multifaceted Abstraction – LCOM ✦ …
Some more metrics❖ Mood (Metrics for Object-Oriented Design)
✦ Method Hiding Factor (MHF)
✦ ratio of sum of the invisibilities of all methods defined in all classes to the total number of methods
✦ Attribute Hiding Factor (AHF)
✦ Method Inheritance Factor (MIF)
✦ ratio of the sum of the inherited methods in all classes to the total number of available methods (locally defined plus inherited) for all classes
✦ Attribute Inheritance Factor (AIF)
✦ …
Tushar Sharma
Abstractness Vs Instability• Dependency
• say class Copy depends on KeyboardReader
• “Good dependency”? • where target of the dependency is stable
• stable - does not depend on anything, not going to change
• Responsible class • many classes depend on the class
• it’s harder to change • and therefore, must be stable
Tushar Sharma
• Instability • Ca - Afferent coupling (Fan-in) • Ce - Efferent coupling (Fan-out)
Abstractness Vs Instability
• So, shouldn’t we have all classes as stable classes? • No. System will be unchangeable.
• Therefore, we need some portion of the design to be flexible to withstand change. • How? Abstract classes
• Abstractness = #abstract classes / #total classes in the group
Tushar Sharma
Abstractness Vs Instability
(1, 0) - Max instable and
concrete
(0, 0) - Max stable and concrete
(0.5, 0.5) - striking the
balance
(0, 1) - Max stable and
abstract
Zone of pain
Zone of uselessness
Main sequence
Abstractness
Instability
(1, 1) - abstract and max
instable (no dependents)