Measure twice, cut once
SOFTWARE METRICS
@matthiasmullie
Software metrics
Make software measurable
instead of relying on intuition.
Software metrics
Presentation titleSoftware metrics
Presentation titleSoftware metrics
Presentation titleSoftware metrics
Software metrics
COMPLEXITY
Every code path adds complexity
Counts # of code paths
Software metrics » Complexity
Cyclomatic complexity
• Analyze control flow
• Start at 1
• Increase at every decision path

(if, for, case, …)
Software metrics » Complexity
Cyclomatic complexity
if ($user->isLoggedIn()) {
echo 'Welcome back, ' . $user->getName();
} else {
echo 'Hi there, stranger!';
}
Cyclomatic complexity = 2
Software metrics » Complexity
Cyclomatic complexity
Every code path adds complexity
Counts # possible outcomes
Software metrics » Complexity
NPath complexity
Software metrics » Complexity
NPath complexity
if ($user->getSalutation()) {
echo $user->getSalutation().' ';
}
if ($user->getName()) {
echo $user->getName();
} else {
echo 'anon';
}
Outcomes: • $salutation anon
• anon
• $salutation $name
• $name
Every operation adds complexity
Software metrics » Complexity
Halstead measures
• Analyze vocabulary
• Count amount of operators

(+, -, =, &&, ... and reserved words,

like if and for)
• Count amount of operands

(values, variables & function names)
Software metrics » Complexity
Halstead measures
if ($user->isLoggedIn()) {
echo 'Welcome back, ' . $user->getName();
} else {
echo 'Hi there, stranger!';
}
Operands = 4 (4 unique)
Software metrics » Complexity
Halstead measures
if ($user->isLoggedIn()) {
echo 'Welcome back, ' . $user->getName();
} else {
echo 'Hi there, stranger!';
}
Operators = 11 (7 unique)
Software metrics » Complexity
Halstead measures
Engineered to match human analysis
Software metrics » Complexity
Maintainability index
Combination of:
• Cyclomatic complexity
• Halstead measures
• Lines of code
Software metrics » Complexity
Maintainability index
Metrics for matthiasmullie/minify, MatthiasMullieMinifyJS::stripWhitespace
See https://www.cauditor.org/matthiasmullie/minify/c17eb048daa44b43fa98bfa405147e77a040df76/metrics
Software metrics » Complexity
Maintainability index
Questions?
Software metrics » Complexity
Software metrics
COUPLING
Amount of incoming dependencies
= other classes using this one
Common for core functionality
Software metrics » Coupling
Afferent coupling
class Afferent
{
function __construct() {}
}
class Efferent
{
function __construct(Afferent $a) {}
}
Afferent: 1, Efferent: 0
Software metrics » Coupling
Afferent coupling
Changes here may break things elsewhere
Must be stable & thoroughly tested
Software metrics » Coupling
Afferent coupling
Amount of outgoing dependencies
= other classes being used
Common for controllers
Software metrics » Coupling
Efferent coupling
class Afferent
{
function __construct() {}
}
class Efferent
{
function __construct(Afferent $a) {}
}
Afferent: 0, Efferent: 1
Software metrics » Coupling
Efferent coupling
• Harder to read/maintain
• Harder to reuse
• Harder to test in isolation
• Brittle
Software metrics » Coupling
Efferent coupling
Changes elsewhere may break things here
Probably violates single responsibility
principle
Software metrics » Coupling
Efferent coupling
= efferent coupling / total coupling
= efferent coupling / (efferent + afferent)
Resilience to change
Software metrics » Coupling
Instability
High efferent coupling
= likely to be impacted by changes in

= other classes
= not stable
Software metrics » Coupling
Instability
Low afferent coupling
= change is easy because there isn’t much

= that depends on this
= not stable
Software metrics » Coupling
Instability
Stable = good
… as long as you don’t need to change
the implementation!
Software metrics » Coupling
Instability
Aim for extremes, make classes as stable
or unstable as possible!
Unstables can be refactored/decomposed
later. Better than having dependencies
everywhere.
Software metrics » Coupling
Instability
Counts # of ancestors
Software metrics » Coupling
Depth of inheritance
class Ancestor {}
DIT: 0
class Descendant1 extends Ancestor {}
DIT: 1
class Descendant2 extends Descendant1 {}
DIT: 2
Software metrics » Coupling
Depth of inheritance
Inheritance is not bad!
• Code reuse
• Polymorphism
Software metrics » Coupling
Depth of inheritance
But too much is!
• Less stable: change in ancestor can
affect descendants
• More complexity: methods of all
ancestors are inherited
Software metrics » Coupling
Depth of inheritance
Questions?
Software metrics » Coupling
WHY WOULD I CARE?
Software metrics
Presentation titleSoftware metrics » Usage
Which would you rather maintain?
Presentation titleSoftware metrics » Usage
Software metrics » Usage
Complexity
This guy has
something going on!
Let’s see…
It’s not too crazy! But:
• Recursion
• 3 loops
• 4 conditions
• Callback function
Software metrics » Usage
Complexity
Method does too much:
• Data transformation
• Processing
• Cleanup
Hard to test & prove right!
Software metrics » Usage
Complexity
Software metrics » Usage
Complexity
This guy
has a lot
going on…
Presentation titleSoftware metrics
Software metrics » Usage
Coupling
• Lots of outgoing
dependencies
• Some inheritance
Very brittle!
Changes in any of these
could affect this class!
Software metrics » Usage
Coupling
Software metrics » Usage
Refactoring
• 1000+ LOC class
• Pretty complex
• Very unstable
• Self-contained, barely
reusable
Software metrics » ObservationsSoftware metrics » Usage
Reduced
complexity
everywhere
Decreased +
increased
stability
Software metrics » Observations
Refactoring
• Greatly reduced
“monster class”
• Split into multiple
reusable pieces
Software metrics » Usage
Questions?
Software metrics » Usage
OBSERVATIONS
Software metrics
Software metrics » Observations
Spikes!
Complexity often caused by a few commits!
Watch out with new features & rewrites.
Software metrics » Observations
Spikes!
Presentation titleSoftware metrics » Observations
Software metrics » Observations
No evolution
• No change over time
• No change across projects
Do developers have a “signature”?
Software metrics » Observations
No evolution
Software metrics » Observations
Personalities?
2 guys working on same project:
• One was very fast (but messier)
• One was a lot more thorough
Who’s who?
Software metrics » Observations
Personalities?
Software metrics » Observations
Personalities?
M
essy
Thorough
Software metrics » Observations
Personalities?
Can metrics show personality traits?
Please help me figure that out!
https://www.cauditor.org/user/feedback
Presentation title
Questions?
Software metrics
mullie.eu
• mullie.eu/measuring-software-complexity
• mullie.eu/measuring-software-coupling
• cauditor.org/help/metrics
Software metrics
Resources

Software metrics