development by the numbers - confoo edition

162
Development By The Numbers

Upload: anthony-ferrara

Post on 18-May-2015

923 views

Category:

Technology


3 download

DESCRIPTION

In this talk, we'll explore some of the tools available for measuring software quality. We'll dive into some of the theory behind the metrics that they analyze while looking at some real world applications of those metrics. We'll also explore how to use these tools to gain valuable insight into legacy codebases.

TRANSCRIPT

Page 1: Development By The Numbers - ConFoo Edition

Development

By The Numbers

Page 2: Development By The Numbers - ConFoo Edition
Page 3: Development By The Numbers - ConFoo Edition
Page 4: Development By The Numbers - ConFoo Edition
Page 5: Development By The Numbers - ConFoo Edition
Page 6: Development By The Numbers - ConFoo Edition

We Are Going ToMeasure Complexity

Page 7: Development By The Numbers - ConFoo Edition

Why Should We Care About Complexity?

Page 8: Development By The Numbers - ConFoo Edition

- Geer et al.

"The Central Enemy Of Reliability is Complexity"

Page 9: Development By The Numbers - ConFoo Edition

Complexity And Quality Are Strongly

Related

Page 10: Development By The Numbers - ConFoo Edition
Page 11: Development By The Numbers - ConFoo Edition

Basic Metrics

Page 12: Development By The Numbers - ConFoo Edition

CyclomaticComplexity

Page 13: Development By The Numbers - ConFoo Edition

Cyclomatic Complexity

Number Of "Decision Points"

In A Routine

Page 14: Development By The Numbers - ConFoo Edition

function foo($a, $b) { $c = 0; if ($a) { $c = $a; } elseif ($b) { $c = $b; } if ($a && $b) { $c = $a + $b; } return $c;}

Page 15: Development By The Numbers - ConFoo Edition

function foo($a, $b) { $c = 0; if ($a) { $c = $a; } elseif ($b) { $c = $b; } if ($a && $b) { $c = $a + $b; } return $c;}

Page 16: Development By The Numbers - ConFoo Edition

function foo($a, $b) { $c = 0; if ($a) { $c = $a; } elseif ($b) { $c = $b; } if ($a && $b) { $c = $a + $b; } return $c;}

4

Page 17: Development By The Numbers - ConFoo Edition

SimpleRight?

Page 18: Development By The Numbers - ConFoo Edition
Page 19: Development By The Numbers - ConFoo Edition

Cyclomatic Complexity(Single Method)

1 - 4: Low Complexity5 - 7: Moderate Complexity8 - 10: High Complexity11+: Very High Complexity

Page 20: Development By The Numbers - ConFoo Edition

Cyclomatic Complexity(Average Per Method)

1 - 2: Low Complexity2 - 4: Moderate Complexity4 - 6: High Complexity6+: Very High Complexity

Page 21: Development By The Numbers - ConFoo Edition

Compare:Average CC per Method

Wordpress: 6.28Drupal 7: 3.02Drupal 8: 2.10Symfony 2: 1.81Zend Framework 2: 2.62 Laravel: 1.79

Page 22: Development By The Numbers - ConFoo Edition

Cyclomatic Complexity(Average Per Line Of Code)

.01 - .05: Low Complexity

.05 - .10: Moderate Complexity

.10 - .15: High Complexity

.15+: Very High Complexity

Page 23: Development By The Numbers - ConFoo Edition

Compare:Average CC per LOC

Wordpress: 0.20Drupal 7: 0.04Drupal 8: 0.07Symfony 2: 0.06Zend Framework 2: 0.10 Laravel: 0.07

Page 24: Development By The Numbers - ConFoo Edition
Page 25: Development By The Numbers - ConFoo Edition

N-PathComplexity

Page 26: Development By The Numbers - ConFoo Edition

N-Path Complexity

Number Of "Unique Paths"

In A Routine

Page 27: Development By The Numbers - ConFoo Edition

function foo($a, $b) { $c = 0; if ($a) { $c = $a; } elseif ($b) { $c = $b; } if ($a && $b) { $c = $a + $b; } return $c;}

Page 28: Development By The Numbers - ConFoo Edition

function foo($a, $b) { $c = 0; if ($a) { $c = $a; } elseif ($b) { $c = $b; } if ($a && $b) { $c = $a + $b; } return $c;}

Page 29: Development By The Numbers - ConFoo Edition

function foo($a, $b) { $c = 0; if ($a) { $c = $a; } elseif ($b) { $c = $b; } if ($a && $b) { $c = $a + $b; } return $c;}

Page 30: Development By The Numbers - ConFoo Edition

function foo($a, $b) { $c = 0; if ($a) { $c = $a; } elseif ($b) { $c = $b; } if ($a && $b) { $c = $a + $b; } return $c;}

Page 31: Development By The Numbers - ConFoo Edition

function foo($a, $b) { $c = 0; if ($a) { $c = $a; } elseif ($b) { $c = $b; } if ($a && $b) { $c = $a + $b; } return $c;}

Page 32: Development By The Numbers - ConFoo Edition

function foo($a, $b) { $c = 0; if ($a) { $c = $a; } elseif ($b) { $c = $b; } if ($a && $b) { $c = $a + $b; } return $c;}

4

Page 33: Development By The Numbers - ConFoo Edition

They Are The Same?

Page 34: Development By The Numbers - ConFoo Edition

Not Generally!

Page 35: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; if ($a) { $d += $a; } if ($b) { $d += $b; } if ($c) { $d += $c; } return $d;}

CC: NPath:

Page 36: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; if ($a) { $d += $a; } if ($b) { $d += $b; } if ($c) { $d += $c; } return $d;}

CC: NPath:

Page 37: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; if ($a) { $d += $a; } if ($b) { $d += $b; } if ($c) { $d += $c; } return $d;}

CC: 4 NPath:

Page 38: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; if ($a) { $d += $a; } if ($b) { $d += $b; } if ($c) { $d += $c; } return $d;}

CC: 4 NPath:

Page 39: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; if ($a) { $d += $a; } if ($b) { $d += $b; } if ($c) { $d += $c; } return $d;}

CC: 4 NPath:

Page 40: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; if ($a) { $d += $a; } if ($b) { $d += $b; } if ($c) { $d += $c; } return $d;}

CC: 4 NPath:

Page 41: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; if ($a) { $d += $a; } if ($b) { $d += $b; } if ($c) { $d += $c; } return $d;}

CC: 4 NPath:

Page 42: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; if ($a) { $d += $a; } if ($b) { $d += $b; } if ($c) { $d += $c; } return $d;}

CC: 4 NPath:

Page 43: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; if ($a) { $d += $a; } if ($b) { $d += $b; } if ($c) { $d += $c; } return $d;}

CC: 4 NPath:

Page 44: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; if ($a) { $d += $a; } if ($b) { $d += $b; } if ($c) { $d += $c; } return $d;}

CC: 4 NPath:

Page 45: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; if ($a) { $d += $a; } if ($b) { $d += $b; } if ($c) { $d += $c; } return $d;}

CC: 4 NPath:

Page 46: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; if ($a) { $d += $a; } if ($b) { $d += $b; } if ($c) { $d += $c; } return $d;}

CC: 4 NPath: 8

Page 47: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; if ($a) { $d += $a; } if ($b) { $d += $b; } if ($c) { $d += $c; } return $d;}

CC: 4 NPath: 8 2^(CC-1)

Page 48: Development By The Numbers - ConFoo Edition

N-Path Complexity

<16: Low Complexity17-128: Moderate Complexity129-1024: High Complexity1025+: Very High Complexity

Page 49: Development By The Numbers - ConFoo Edition
Page 50: Development By The Numbers - ConFoo Edition

N-Path Complexity

Minimum Number Of Tests Required To Completely Test

A Routine

Page 51: Development By The Numbers - ConFoo Edition
Page 52: Development By The Numbers - ConFoo Edition
Page 53: Development By The Numbers - ConFoo Edition

N-Path Complexity

entity_load()

CC: N-Path:

Page 54: Development By The Numbers - ConFoo Edition

N-Path Complexity

entity_load()

CC: 2 N-Path:

Page 55: Development By The Numbers - ConFoo Edition

Cyclomatic Complexity

1 - 4: Low Complexity5 - 7: Moderate Complexity8 - 10: High Complexity11+: Very High Complexity

Page 56: Development By The Numbers - ConFoo Edition

N-Path Complexity

entity_load()

CC: 2 N-Path: 2

Page 57: Development By The Numbers - ConFoo Edition
Page 58: Development By The Numbers - ConFoo Edition

N-Path Complexity

drupal_http_request()

CC: N-Path:

Page 59: Development By The Numbers - ConFoo Edition
Page 60: Development By The Numbers - ConFoo Edition
Page 61: Development By The Numbers - ConFoo Edition
Page 62: Development By The Numbers - ConFoo Edition
Page 63: Development By The Numbers - ConFoo Edition

N-Path Complexity

drupal_http_request()

CC: 41 N-Path:

Page 64: Development By The Numbers - ConFoo Edition

Cyclomatic Complexity

1 - 4: Low Complexity5 - 7: Moderate Complexity8 - 10: High Complexity11+: Very High Complexity

Page 65: Development By The Numbers - ConFoo Edition

N-Path Complexity

drupal_http_request()

CC: 41 N-Path: 25,303,344,960

Page 66: Development By The Numbers - ConFoo Edition

To Completely Testdrupal_http_request()At 1 Line Of Code Per Test

Would Require

2 TerabytesWorth Of Tests

Page 67: Development By The Numbers - ConFoo Edition
Page 68: Development By The Numbers - ConFoo Edition

To Completely Testdrupal_http_request()At 1 Line Of Code Per Test

Would Require

412 DVD's Worth Of Tests

Page 69: Development By The Numbers - ConFoo Edition

To Completely Testdrupal_http_request()At 1 Line Of Code Per Test

Would Require

670k DrupalsWorth Of Tests

Page 70: Development By The Numbers - ConFoo Edition

And That's Not The Worst One!

Page 71: Development By The Numbers - ConFoo Edition
Page 72: Development By The Numbers - ConFoo Edition

N-Path Complexity

_date_repeat_rrule_process()

CC: N-Path:

Page 73: Development By The Numbers - ConFoo Edition

N-Path Complexity

_date_repeat_rrule_process()

CC: 81 N-Path:

Page 74: Development By The Numbers - ConFoo Edition

N-Path Complexity

_date_repeat_rrule_process()

CC: 81 N-Path: 19,781,719,256

Page 75: Development By The Numbers - ConFoo Edition

N-Path Complexity

_date_repeat_rrule_process()

CC: 81 N-Path: 19,781,719,256 ,250,000,000,000

Page 76: Development By The Numbers - ConFoo Edition

N-Path Complexity

_date_repeat_rrule_process()

CC: 81 N-Path: 19,781,719,256 ,250,000,000,000 ,000,000,000

Page 77: Development By The Numbers - ConFoo Edition

To Completely Test_date_repeat_rrule_process()

At 1 Line Of Code Per Test

Would Require

336T 2009'sWorth Of Tests

Page 78: Development By The Numbers - ConFoo Edition
Page 79: Development By The Numbers - ConFoo Edition

MicroSD Card

● About 387GB / cm^3○ Current Density Record

Page 80: Development By The Numbers - ConFoo Edition

MicroSD Card

● About 387GB / cm^3○ Current Density Record

● About 184PB / Butt-Load○ The text from every piece of written material in the

world...

Page 81: Development By The Numbers - ConFoo Edition

MicroSD Card

● About 387GB / cm^3○ Current Density Record

● About 184PB / Butt-Load○ The text from every piece of written material in the

world...● About 387PB / m^3

○ About 20 “MegaUpload.com Sites”

Page 82: Development By The Numbers - ConFoo Edition

MicroSD Card

● About 387GB / cm^3○ Current Density Record

● About 184PB / Butt-Load○ The text from every piece of written material in the

world...● About 387PB / m^3

○ About 20 “MegaUpload.com Sites”● About 387YB / km^3

○ About 100,000 Google’s

Page 83: Development By The Numbers - ConFoo Edition

To Completely Test_date_repeat_rrule_process()

At 1 Line Of Code Per Test

Would Require1 Greenland Ice Cap of

microSD cardsWorth Of Tests

Page 84: Development By The Numbers - ConFoo Edition
Page 85: Development By The Numbers - ConFoo Edition

N-Path Complexity

compose_pm()

CC: N-Path:

Page 86: Development By The Numbers - ConFoo Edition

N-Path Complexity

compose_pm()

CC: 331 N-Path:

Page 87: Development By The Numbers - ConFoo Edition

Because Sending Private Messages Is Hard...

Page 88: Development By The Numbers - ConFoo Edition

N-Path Complexity

compose_pm()

CC: 331 N-Path:

Page 89: Development By The Numbers - ConFoo Edition

N-Path Complexity

compose_pm()

CC: 331 N-Path: 12,334,686,665

Page 90: Development By The Numbers - ConFoo Edition

N-Path Complexity

compose_pm()

CC: 331 N-Path: 12,334,686,665 ,165,904,892,028

Page 91: Development By The Numbers - ConFoo Edition

N-Path Complexity

compose_pm()

CC: 331 N-Path: 12,334,686,665 ,165,904,892,028 ,680,955,918,269

Page 92: Development By The Numbers - ConFoo Edition

N-Path Complexity

compose_pm()

CC: 331 N-Path: 12,334,686,665 ,165,904,892,028 ,680,955,918,269 ,333,860,397,338

Page 93: Development By The Numbers - ConFoo Edition

N-Path Complexitycompose_pm()

CC: 331 N-Path: 12,334,686,665 ,165,904,892,028 ,680,955,918,269 ,333,860,397,338 ,867,187,500,000

Page 94: Development By The Numbers - ConFoo Edition

N-Path Complexity

compose_pm()

CC: 331 N-Path: 12,334,686,665 ,165,904,892,028 ,680,955,918,269 ,333,860,397,338 ,867,187,500,000 ,000,000,000,000

Page 95: Development By The Numbers - ConFoo Edition

N-Path Complexity

pm_compose()

CC: 331 N-Path: 12,334,686,665 ,165,904,892,028 ,680,955,918,269 ,333,860,397,338 ,867,187,500,000 ,000,000,000,000 ,000,000,000,000

Page 96: Development By The Numbers - ConFoo Edition

To Completely Testcompose_pm()

At 1 Line Of Code Per Test

Would Require

1B BlackHolesWorth Of Tests

Page 97: Development By The Numbers - ConFoo Edition
Page 98: Development By The Numbers - ConFoo Edition

CRAP

Page 99: Development By The Numbers - ConFoo Edition
Page 100: Development By The Numbers - ConFoo Edition

CRAP(Change Risk Analysis Predictions)

Page 101: Development By The Numbers - ConFoo Edition

CC = Cyclomatic Complexity (method)COV = Test Coverage (percent)

CRAP = CC + (CC^2 * (1 - COV)^3)

Page 102: Development By The Numbers - ConFoo Edition
Page 103: Development By The Numbers - ConFoo Edition

CRAP

Relates ComplexityAnd Test Coverage

Page 104: Development By The Numbers - ConFoo Edition

CRAP

Increasing Test Coverage Lowers CRAPDecreasing Complexity Lowers CRAP

Page 105: Development By The Numbers - ConFoo Edition

CRAP

A Low Complexity MethodWith No Tests

Is Good

Page 106: Development By The Numbers - ConFoo Edition

CRAP

A Low Complexity MethodWith Good Tests

Is Great

Page 107: Development By The Numbers - ConFoo Edition

CRAP

A Moderate Complexity MethodWith Good Tests

Is OK

Page 108: Development By The Numbers - ConFoo Edition

CRAP

A Moderate Complexity MethodWith No Tests

Is CRAP

Page 109: Development By The Numbers - ConFoo Edition

CRAP

< 5: GREAT Code5 - 15: Acceptable Code15-30: Eih... Code30+: CRAPpy Code

Page 110: Development By The Numbers - ConFoo Edition

A Word On Refactoring

Page 111: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; if ($a) { $d += $a; } if ($b) { $d += $b; } if ($c) { $d += $c; } return $d;}

Page 112: Development By The Numbers - ConFoo Edition

function foo2($a, $b, $c) { $d = 0; $d += checkValue($a); $d += checkValue($b); $d += checkValue($c); return $d;}function checkValue($v) { if ($v) { return $v; } return 0;}

Page 113: Development By The Numbers - ConFoo Edition

Quality Is A Tradeoff Between

Complexity And Bloat

Page 114: Development By The Numbers - ConFoo Edition
Page 115: Development By The Numbers - ConFoo Edition

How Do WeMeasure These

Metrics?

Page 116: Development By The Numbers - ConFoo Edition
Page 117: Development By The Numbers - ConFoo Edition
Page 118: Development By The Numbers - ConFoo Edition

Quiz:

Only 2 Major QA ToolsNOT Made By Germans

What Are They?

Page 119: Development By The Numbers - ConFoo Edition

PHP-CodeSniffer

Behat

Page 120: Development By The Numbers - ConFoo Edition

Let’s Talk Tools

Page 121: Development By The Numbers - ConFoo Edition

PHPLOC

Page 122: Development By The Numbers - ConFoo Edition

PHPLOC

By Sebastian Bergmann

Page 123: Development By The Numbers - ConFoo Edition

PHPLOC

By Sebastian Bergmann

Command Line Tool

Page 124: Development By The Numbers - ConFoo Edition

PHPLOC

By Sebastian Bergmann

Command Line Tool

Summarizes An Entire Codebase

Page 125: Development By The Numbers - ConFoo Edition

$ phploc path/to/Drupal7/Directories: 73Files: 180

Lines of Code (LOC): 63347 Cyclomatic Complexity / Lines of Code: 0.04Comment Lines of Code (CLOC): 19321Non-Comment Lines of Code (NCLOC): 44026

Page 126: Development By The Numbers - ConFoo Edition

Namespaces: 0Interfaces: 1Traits: 0Classes: 38 Abstract: 2 (5.26%) Concrete: 36 (94.74%) Average Class Length (NCLOC): 197

Page 127: Development By The Numbers - ConFoo Edition

Methods: 433 Scope:

Non-Static: 378 (87.30%) Static: 55 (12.70%) Visibility:

Public: 255 (58.89%) Non-Public: 178 (41.11%) Average Method Length (NCLOC): 17 Cyclomatic Complexity / Number of Methods: 3.02

Anonymous Functions: 0Functions: 521

Constants: 22 Global constants: 15 Class constants: 7

Page 128: Development By The Numbers - ConFoo Edition

PDepend

Page 129: Development By The Numbers - ConFoo Edition

PDepend

By Manuel Pichler(Also German)

Page 130: Development By The Numbers - ConFoo Edition

PDepend

By Manuel Pichler(Also German)

Like PHPLOC, But Granular

Page 131: Development By The Numbers - ConFoo Edition

PDepend

By Manuel Pichler(Also German)

Like PHPLOC, But Granular

Lower Level Analysis

Page 132: Development By The Numbers - ConFoo Edition
Page 133: Development By The Numbers - ConFoo Edition

Fanout: Describes Outward Dependencies - Describes Dependence on Other Classes

ANDC: Average Num of Derived Classes - Describes How Much Inheritance Is Used

AHH: Average Hiearchy Height - Describes How Deep Of Inheritance Is Used

Page 134: Development By The Numbers - ConFoo Edition

PHPMD(Mess Detector)

Page 135: Development By The Numbers - ConFoo Edition

PHPMD

By Manuel Pichler(German)

Page 136: Development By The Numbers - ConFoo Edition

PHPMD

By Manuel Pichler(German)

Finds "Messy" Parts Of Code

Page 137: Development By The Numbers - ConFoo Edition

PHPMD

By Manuel Pichler(German)

Finds "Messy" Parts Of Code

Finds Rule Violations

Page 138: Development By The Numbers - ConFoo Edition

PHPMD RulesCodeSize - (CC, NPath, Number of Methods, Size of Methods, etc)

Design - (Eval, Goto, Exit(), Inheritance Depth)

Naming - (Short names, Inconsistent Names)

Unused CodeControversial - (Superglobal Access, Naming Conventions)

Page 139: Development By The Numbers - ConFoo Edition
Page 140: Development By The Numbers - ConFoo Edition

Prevent Complex CodeFrom Even Getting In!

Page 141: Development By The Numbers - ConFoo Edition

By ThemselvesUseful

Page 142: Development By The Numbers - ConFoo Edition

Over Time

Page 143: Development By The Numbers - ConFoo Edition

Over TimeInvaluable

Page 144: Development By The Numbers - ConFoo Edition

Jenkins-PHPjenkins-php.org

Page 145: Development By The Numbers - ConFoo Edition
Page 146: Development By The Numbers - ConFoo Edition

SonarSonarSource.Com

Page 147: Development By The Numbers - ConFoo Edition
Page 148: Development By The Numbers - ConFoo Edition

Insightinsight.sensiolabs.com

Page 149: Development By The Numbers - ConFoo Edition

Time For Some Fun!

Page 150: Development By The Numbers - ConFoo Edition

Drupal 8.x BranchNon-Comment Lines Of Code

Page 151: Development By The Numbers - ConFoo Edition

Drupal 8.x BranchNumber Of Classes

Page 152: Development By The Numbers - ConFoo Edition

Drupal 8.x BranchCyclomatic Complexity Per Method

Page 153: Development By The Numbers - ConFoo Edition

Drupal 8.x BranchCyclomatic Complexity Per Line

Page 154: Development By The Numbers - ConFoo Edition

Drupal 8.x Branch

Page 155: Development By The Numbers - ConFoo Edition
Page 156: Development By The Numbers - ConFoo Edition
Page 157: Development By The Numbers - ConFoo Edition

One More ThingTo Keep In Mind

Page 158: Development By The Numbers - ConFoo Edition
Page 159: Development By The Numbers - ConFoo Edition

Do Not Rely Solely On These Numbers

Page 160: Development By The Numbers - ConFoo Edition

Instead, Try To Understand At The

Bigger Picture

Page 161: Development By The Numbers - ConFoo Edition
Page 162: Development By The Numbers - ConFoo Edition

Anthony Ferrara@ircmaxell

[email protected]

github.com/ircmaxellyoutube.com/ircmaxell