development by the numbers

132
Development By The Numbers

Upload: anthony-ferrara

Post on 18-May-2015

7.185 views

Category:

Technology


0 download

DESCRIPTION

Presentation from DrupalCon Portland: http://portland2013.drupal.org/node/1078

TRANSCRIPT

Page 1: Development by the numbers

DevelopmentBy The Numbers

Page 2: Development by the numbers
Page 3: Development by the numbers
Page 4: Development by the numbers
Page 5: Development by the numbers
Page 6: Development by the numbers

We Are Going ToMeasure Complexity

Page 7: Development by the numbers

Why Should We Care About Complexity?

Page 8: Development by the numbers

- Geer et al.

"The Central Enemy Of Reliability is Complexity"

Page 9: Development by the numbers

Complexity And Quality Are Strongly

Related

Page 10: Development by the numbers
Page 11: Development by the numbers

Basic Metrics

Page 12: Development by the numbers

CyclomaticComplexity

Page 13: Development by the numbers

Cyclomatic Complexity

Number Of "Decision Points"

In A Routine

Page 14: Development by the numbers

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

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

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

SimpleRight?

Page 18: Development by the numbers
Page 19: Development by the numbers

Cyclomatic Complexity(Single Method)

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

Page 20: Development by the numbers

Cyclomatic Complexity(Average Per Method)

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

Page 21: Development by the numbers

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

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

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
Page 25: Development by the numbers

N-PathComplexity

Page 26: Development by the numbers

N-Path Complexity

Number Of "Unique Paths"

In A Routine

Page 27: Development by the numbers

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

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

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

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

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

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

They Are The Same?

Page 34: Development by the numbers

Not Generally!

Page 35: Development by the numbers

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

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

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

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

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

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

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

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

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

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

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

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

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

N-Path Complexity

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

Page 49: Development by the numbers
Page 50: Development by the numbers

N-Path Complexity

Minimum Number Of Tests Required To Completely Test

A Routine

Page 51: Development by the numbers
Page 52: Development by the numbers
Page 53: Development by the numbers

N-Path Complexity

entity_load()

CC: N-Path:

Page 54: Development by the numbers

N-Path Complexity

entity_load()

CC: 2 N-Path:

Page 55: Development by the numbers

Cyclomatic Complexity

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

Page 56: Development by the numbers

N-Path Complexity

entity_load()

CC: 2 N-Path: 2

Page 57: Development by the numbers
Page 58: Development by the numbers

N-Path Complexity

drupal_http_request()

CC: N-Path:

Page 59: Development by the numbers
Page 60: Development by the numbers
Page 61: Development by the numbers
Page 62: Development by the numbers
Page 63: Development by the numbers

N-Path Complexity

drupal_http_request()

CC: 41 N-Path:

Page 64: Development by the numbers

Cyclomatic Complexity

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

Page 65: Development by the numbers

N-Path Complexity

drupal_http_request()

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

Page 66: Development by the numbers
Page 67: Development by the numbers

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

Would Require

2 TerabytesWorth Of Tests

Page 68: Development by the numbers

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

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

Would Require

670k DrupalsWorth Of Tests

Page 70: Development by the numbers

And That's Not The Worst One!

Page 71: Development by the numbers
Page 72: Development by the numbers

N-Path Complexity

_date_repeat_rrule_process()

CC: N-Path:

Page 73: Development by the numbers

N-Path Complexity

_date_repeat_rrule_process()

CC: 81 N-Path:

Page 74: Development by the numbers

N-Path Complexity

_date_repeat_rrule_process()

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

Page 75: Development by the numbers

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

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

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

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 79: Development by the numbers
Page 80: Development by the numbers

CRAP

Page 81: Development by the numbers
Page 82: Development by the numbers

CRAP(Change Risk Analysis Predictions)

Page 83: Development by the numbers

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

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

Page 84: Development by the numbers
Page 85: Development by the numbers

CRAP

Relates ComplexityAnd Test Coverage

Page 86: Development by the numbers

CRAP

Increasing Test Coverage Lowers CRAPDecreasing Complexity Lowers CRAP

Page 87: Development by the numbers

CRAP

A Low Complexity MethodWith No Tests

Is Good

Page 88: Development by the numbers

CRAP

A Low Complexity MethodWith Good Tests

Is Great

Page 89: Development by the numbers

CRAP

A Moderate Complexity MethodWith Good Tests

Is OK

Page 90: Development by the numbers

CRAP

A Moderate Complexity MethodWith No Tests

Is CRAP

Page 91: Development by the numbers

CRAP

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

Page 92: Development by the numbers
Page 93: Development by the numbers

How Do WeApply These

Metrics?

Page 94: Development by the numbers
Page 95: Development by the numbers
Page 96: Development by the numbers
Page 97: Development by the numbers

Sebastian Bergmann

PHPUnitDbUnitPHPLOCPHPCPDPHPCOVhphpa

www.phpqatools.orgwww.jenkins-php.org

Page 98: Development by the numbers

PHPLOC

Page 99: Development by the numbers

PHPLOC

By Sebastian Bergmann

Page 100: Development by the numbers

PHPLOC

By Sebastian Bergmann

Command Line Tool

Page 101: Development by the numbers

PHPLOC

By Sebastian Bergmann

Command Line Tool

Summarizes An Entire Codebase

Page 102: Development by the numbers

$ 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 103: Development by the numbers

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

Page 104: Development by the numbers

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 105: Development by the numbers

PDepend

Page 106: Development by the numbers

PDepend

By Manuel Pichler(Also German)

Page 107: Development by the numbers

PDepend

By Manuel Pichler(Also German)

Like PHPLOC, But Granular

Page 108: Development by the numbers

PDepend

By Manuel Pichler(Also German)

Like PHPLOC, But Granular

Lower Level Analysis

Page 109: Development by the numbers
Page 110: Development by the numbers

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 111: Development by the numbers

PHPMD(Mess Detector)

Page 112: Development by the numbers

PHPMD

By Manuel Pichler(German)

Page 113: Development by the numbers

PHPMD

By Manuel Pichler(German)

Finds "Messy" Parts Of Code

Page 114: Development by the numbers

PHPMD

By Manuel Pichler(German)

Finds "Messy" Parts Of Code

Finds Rule Violations

Page 115: Development by the numbers

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 116: Development by the numbers
Page 117: Development by the numbers

Prevent Complex CodeFrom Even Getting In!

Page 118: Development by the numbers

By ThemselvesUseful

Page 119: Development by the numbers

Over Time

Page 120: Development by the numbers

Over TimeInvaluable

Page 121: Development by the numbers

Drupal 8.x BranchNon-Comment Lines Of Code

Page 122: Development by the numbers

Drupal 8.x BranchNumber Of Classes

Page 123: Development by the numbers

Drupal 8.x BranchCyclomatic Complexity Per Method

Page 124: Development by the numbers

Drupal 8.x BranchCyclomatic Complexity Per Line

Page 125: Development by the numbers

Drupal 8.x Branch

Page 126: Development by the numbers
Page 127: Development by the numbers
Page 128: Development by the numbers
Page 129: Development by the numbers

One More ThingTo Keep In Mind

Page 130: Development by the numbers
Page 131: Development by the numbers
Page 132: Development by the numbers

Anthony Ferrara@ircmaxell

[email protected]@nbcuni.com

blog.ircmaxell.comgithub.com/ircmaxell

youtube.com/ircmaxell