Slide 1

Slide 1 text

Measure twice, cut once SOFTWARE METRICS

Slide 2

Slide 2 text

@matthiasmullie Software metrics

Slide 3

Slide 3 text

Make software measurable instead of relying on intuition. Software metrics

Slide 4

Slide 4 text

Presentation title Software metrics

Slide 5

Slide 5 text

Presentation title Software metrics

Slide 6

Slide 6 text

Presentation title Software metrics

Slide 7

Slide 7 text

Software metrics COMPLEXITY

Slide 8

Slide 8 text

Every code path adds complexity Counts # of code paths Software metrics » Complexity Cyclomatic complexity

Slide 9

Slide 9 text

• Analyze control flow • Start at 1 • Increase at every decision path
 (if, for, case, …) Software metrics » Complexity Cyclomatic complexity

Slide 10

Slide 10 text

if ($user->isLoggedIn()) { echo 'Welcome back, ' . $user->getName(); } else { echo 'Hi there, stranger!'; } Cyclomatic complexity = 2 Software metrics » Complexity Cyclomatic complexity

Slide 11

Slide 11 text

Every code path adds complexity Counts # possible outcomes Software metrics » Complexity NPath complexity

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Every operation adds complexity Software metrics » Complexity Halstead measures

Slide 14

Slide 14 text

• 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

Slide 15

Slide 15 text

if ($user->isLoggedIn()) { echo 'Welcome back, ' . $user->getName(); } else { echo 'Hi there, stranger!'; } Operands = 4 (4 unique) Software metrics » Complexity Halstead measures

Slide 16

Slide 16 text

if ($user->isLoggedIn()) { echo 'Welcome back, ' . $user->getName(); } else { echo 'Hi there, stranger!'; } Operators = 11 (7 unique) Software metrics » Complexity Halstead measures

Slide 17

Slide 17 text

Engineered to match human analysis Software metrics » Complexity Maintainability index

Slide 18

Slide 18 text

Combination of: • Cyclomatic complexity • Halstead measures • Lines of code Software metrics » Complexity Maintainability index

Slide 19

Slide 19 text

Metrics for matthiasmullie/minify, MatthiasMullie\Minify\JS::stripWhitespace See https://www.cauditor.org/matthiasmullie/minify/c17eb048daa44b43fa98bfa405147e77a040df76/metrics Software metrics » Complexity Maintainability index

Slide 20

Slide 20 text

Questions? Software metrics » Complexity

Slide 21

Slide 21 text

Software metrics COUPLING

Slide 22

Slide 22 text

Amount of incoming dependencies = other classes using this one Common for core functionality Software metrics » Coupling Afferent coupling

Slide 23

Slide 23 text

class Afferent { function __construct() {} } class Efferent { function __construct(Afferent $a) {} } Afferent: 1, Efferent: 0 Software metrics » Coupling Afferent coupling

Slide 24

Slide 24 text

Changes here may break things elsewhere Must be stable & thoroughly tested Software metrics » Coupling Afferent coupling

Slide 25

Slide 25 text

Amount of outgoing dependencies = other classes being used Common for controllers Software metrics » Coupling Efferent coupling

Slide 26

Slide 26 text

class Afferent { function __construct() {} } class Efferent { function __construct(Afferent $a) {} } Afferent: 0, Efferent: 1 Software metrics » Coupling Efferent coupling

Slide 27

Slide 27 text

• Harder to read/maintain • Harder to reuse • Harder to test in isolation • Brittle Software metrics » Coupling Efferent coupling

Slide 28

Slide 28 text

Changes elsewhere may break things here Probably violates single responsibility principle Software metrics » Coupling Efferent coupling

Slide 29

Slide 29 text

= efferent coupling / total coupling = efferent coupling / (efferent + afferent) Resilience to change Software metrics » Coupling Instability

Slide 30

Slide 30 text

High efferent coupling = likely to be impacted by changes in
 = other classes = not stable Software metrics » Coupling Instability

Slide 31

Slide 31 text

Low afferent coupling = change is easy because there isn’t much
 = that depends on this = not stable Software metrics » Coupling Instability

Slide 32

Slide 32 text

Stable = good … as long as you don’t need to change the implementation! Software metrics » Coupling Instability

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Counts # of ancestors Software metrics » Coupling Depth of inheritance

Slide 35

Slide 35 text

class Ancestor {} DIT: 0 class Descendant1 extends Ancestor {} DIT: 1 class Descendant2 extends Descendant1 {} DIT: 2 Software metrics » Coupling Depth of inheritance

Slide 36

Slide 36 text

Inheritance is not bad! • Code reuse • Polymorphism Software metrics » Coupling Depth of inheritance

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

Questions? Software metrics » Coupling

Slide 39

Slide 39 text

WHY WOULD I CARE? Software metrics

Slide 40

Slide 40 text

Presentation title Software metrics » Usage Which would you rather maintain?

Slide 41

Slide 41 text

Presentation title Software metrics » Usage

Slide 42

Slide 42 text

Software metrics » Usage Complexity This guy has something going on! Let’s see…

Slide 43

Slide 43 text

It’s not too crazy! But: • Recursion • 3 loops • 4 conditions • Callback function Software metrics » Usage Complexity

Slide 44

Slide 44 text

Method does too much: • Data transformation • Processing • Cleanup Hard to test & prove right! Software metrics » Usage Complexity

Slide 45

Slide 45 text

Software metrics » Usage Complexity This guy has a lot going on…

Slide 46

Slide 46 text

Presentation title Software metrics

Slide 47

Slide 47 text

Software metrics » Usage Coupling • Lots of outgoing dependencies • Some inheritance Very brittle!

Slide 48

Slide 48 text

Changes in any of these could affect this class! Software metrics » Usage Coupling

Slide 49

Slide 49 text

Software metrics » Usage Refactoring • 1000+ LOC class • Pretty complex • Very unstable • Self-contained, barely reusable

Slide 50

Slide 50 text

Software metrics » Observations Software metrics » Usage Reduced complexity everywhere Decreased + increased stability

Slide 51

Slide 51 text

Software metrics » Observations Refactoring • Greatly reduced “monster class” • Split into multiple reusable pieces Software metrics » Usage

Slide 52

Slide 52 text

Questions? Software metrics » Usage

Slide 53

Slide 53 text

OBSERVATIONS Software metrics

Slide 54

Slide 54 text

Software metrics » Observations Spikes!

Slide 55

Slide 55 text

Complexity often caused by a few commits! Watch out with new features & rewrites. Software metrics » Observations Spikes!

Slide 56

Slide 56 text

Presentation title Software metrics » Observations

Slide 57

Slide 57 text

Software metrics » Observations No evolution

Slide 58

Slide 58 text

• No change over time • No change across projects Do developers have a “signature”? Software metrics » Observations No evolution

Slide 59

Slide 59 text

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?

Slide 60

Slide 60 text

Software metrics » Observations Personalities?

Slide 61

Slide 61 text

Software metrics » Observations Personalities? M essy Thorough

Slide 62

Slide 62 text

Software metrics » Observations Personalities? Can metrics show personality traits? Please help me figure that out! https://www.cauditor.org/user/feedback

Slide 63

Slide 63 text

Presentation title

Slide 64

Slide 64 text

Questions? Software metrics

Slide 65

Slide 65 text

mullie.eu • mullie.eu/measuring-software-complexity • mullie.eu/measuring-software-coupling • cauditor.org/help/metrics Software metrics Resources