Slide 1

Slide 1 text

Codequalität messen EnterJS, 01. Juli 2014

Slide 2

Slide 2 text

Andy Grunwald • Software Engineer bei @trivago
 • Open Source
 • @andygrunwald
 • @PHPUGDus
 • andygrunwald

Slide 3

Slide 3 text

Code | qualität | messen

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

–Helmut Balzert „Unter Softwarequalität versteht man die Gesamtheit der Merkmale … eines Softwareprodukts, …, festgelegte oder vorausgesetzte Erfordernisse zu erfüllen.“

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

(Software)-Metriken

Slide 8

Slide 8 text

Frontend Backend

Slide 9

Slide 9 text

Frontend HTTP Requests Navigation Timing DOM Elemente Browser Repaints … Backend

Slide 10

Slide 10 text

Frontend HTTP Requests Navigation Timing DOM Elemente Browser Repaints … Backend Request-Zeit Cache-Miss / -Hits Ressource-Zeit Business-Logik …

Slide 11

Slide 11 text

Frontend HTTP Requests Navigation Timing DOM Elemente Browser Repaints … Backend Request-Zeit Cache-Miss / -Hits Ressource-Zeit Business-Logik … Source Code

Slide 12

Slide 12 text

Source Code

Slide 13

Slide 13 text

Source Code Functions count Nested depth Halstead Maintainability index LOC Parameter count Cyclomatic Complexity nPath Lint errors CLOC LLOC Your metric here

Slide 14

Slide 14 text

Source Code Functions count Nested depth Halstead Maintainability index LOC Parameter count Cyclomatic Complexity nPath Lint errors CLOC LLOC Your metric here

Slide 15

Slide 15 text

// Is a given array, string, … // An "empty" object has … _.isEmpty = function(obj) { if (obj == null) { return true; } ! if (_.isArray(obj)) { return obj.length === 0; } ! for (var key in obj) { if (_.has(obj, key)) { return false; } } ! return true; }; _.isEmpty(e);

Slide 16

Slide 16 text

// Is a given array, string, … // An "empty" object has … _.isEmpty = function(obj) { if (obj == null) { return true; } ! if (_.isArray(obj)) { return obj.length === 0; } ! for (var key in obj) { if (_.has(obj, key)) { return false; } } ! return true; }; _.isEmpty(e); • LOC: 19

Slide 17

Slide 17 text

// Is a given array, string, … // An "empty" object has … _.isEmpty = function(obj) { if (obj == null) { return true; } ! if (_.isArray(obj)) { return obj.length === 0; } ! for (var key in obj) { if (_.has(obj, key)) { return false; } } ! return true; }; _.isEmpty(e); • LOC: 19
 • CLOC: 2

Slide 18

Slide 18 text

// Is a given array, string, … // An "empty" object has … _.isEmpty = function(obj) { if (obj == null) { return true; } ! if (_.isArray(obj)) { return obj.length === 0; } ! for (var key in obj) { if (_.has(obj, key)) { return false; } } ! return true; }; _.isEmpty(e); • LOC: 19
 • CLOC: 2
 • NCLOC: 17

Slide 19

Slide 19 text

_.isEmpty(e); • LOC: 19
 • CLOC: 2
 • NCLOC: 17
 • LLOC: 9 // Is a given array, string, … // An "empty" object has … _.isEmpty = function(obj) { if (obj == null) { return true; } ! if (_.isArray(obj)) { return obj.length === 0; } ! for (var key in obj) { if (_.has(obj, key)) { return false; } } ! return true; };

Slide 20

Slide 20 text

Source Code Functions count Nested depth Halstead Maintainability index LOC Parameter count Cyclomatic Complexity nPath Lint errors CLOC LLOC Your metric here

Slide 21

Slide 21 text

Source Code Functions count Nested depth Halstead Maintainability index LOC Parameter count Cyclomatic Complexity nPath Lint errors CLOC LLOC Your metric here

Slide 22

Slide 22 text

Cyclomatic Complexity / McCabe Anzahl von Entscheidungspunkten innerhalb einer Funktion (if, switch, for, while, …)

Slide 23

Slide 23 text

function nestedFunction(x, y, z) { if (x > 0) { if (y > 0) { if (z > 0) { console.log(z); } } } }

Slide 24

Slide 24 text

function nestedFunction(x, y, z) { if (x > 0) { if (y > 0) { if (z > 0) { console.log(z); } } } }

Slide 25

Slide 25 text

function nestedFunction(x, y, z) { if (x > 0) { if (y > 0) { if (z > 0) { console.log(z); } } } }

Slide 26

Slide 26 text

function nestedFunction(x, y, z) { if (x > 0) { if (y > 0) { if (z > 0) { console.log(z); } } } }

Slide 27

Slide 27 text

function nestedFunction(x, y, z) { if (x > 0) { if (y > 0) { if (z > 0) { console.log(z); } } } }

Slide 28

Slide 28 text

function nestedFunction(x, y, z) { if (x > 0) { if (y > 0) { if (z > 0) { console.log(z); } } } } Cyclomatic Complexity: 4

Slide 29

Slide 29 text

nPath Complexity Anzahl einzigartiger Ausführungspfade innerhalb einer Funktion

Slide 30

Slide 30 text

function nestedFunction(x, y, z) { if (x > 0) { if (y > 0) { if (z > 0) { console.log(z); } } } }

Slide 31

Slide 31 text

function nestedFunction(x, y, z) { if (x > 0) { if (y > 0) { if (z > 0) { console.log(z); } } } }

Slide 32

Slide 32 text

function nestedFunction(x, y, z) { if (x > 0) { if (y > 0) { if (z > 0) { console.log(z); } } } }

Slide 33

Slide 33 text

function nestedFunction(x, y, z) { if (x > 0) { if (y > 0) { if (z > 0) { console.log(z); } } } }

Slide 34

Slide 34 text

function nestedFunction(x, y, z) { if (x > 0) { if (y > 0) { if (z > 0) { console.log(z); } } } }

Slide 35

Slide 35 text

function nestedFunction(x, y, z) { if (x > 0) { if (y > 0) { if (z > 0) { console.log(z); } } } } nPath Complexity: 4

Slide 36

Slide 36 text

Cyclomatic Complexity === nPath?

Slide 37

Slide 37 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 38

Slide 38 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 39

Slide 39 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 40

Slide 40 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 41

Slide 41 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 42

Slide 42 text

Cyclomatic Complexity: 4 function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 43

Slide 43 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 44

Slide 44 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 45

Slide 45 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 46

Slide 46 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 47

Slide 47 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 48

Slide 48 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 49

Slide 49 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 50

Slide 50 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 51

Slide 51 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } }

Slide 52

Slide 52 text

function nPath(x, y, z) { if (x > 0) { console.log(x); } ! if (y > 0) { console.log(y); } ! if (z > 0) { console.log(z); } } nPath Complexity: 8

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

escomplex complexity-report plato Esprima yardstick jsmeter

Slide 56

Slide 56 text

escomplex complexity-report plato Esprima yardstick jsmeter

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

ScanJS JSHint jsprime DoctorJs JSWhiz WALA

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

+

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

Sprach-Features

Slide 63

Slide 63 text

function foo() { var a = [0, 1, 2, 3, 4]; for (index = 0; index < a.length; ++index) { console.log(a[index]); } }

Slide 64

Slide 64 text

function foo() { for (key in [0, 1, 2, 3, 4]) { console.log(key); } } function foo() { var a = [0, 1, 2, 3, 4]; for (index = 0; index < a.length; ++index) { console.log(a[index]); } }

Slide 65

Slide 65 text

function foo() { for (key in [0, 1, 2, 3, 4]) { console.log(key); } } function foo() { [0, 1, 2, 3, 4].forEach(function (i) { console.log(i); }); } function foo() { var a = [0, 1, 2, 3, 4]; for (index = 0; index < a.length; ++index) { console.log(a[index]); } }

Slide 66

Slide 66 text

JavaScript ist eine dynamische Sprache

Slide 67

Slide 67 text

var a = 'if(arguments[0] > 0) ' + 'console.log(arguments);'; new Function(a)(5);

Slide 68

Slide 68 text

var a = '(function() {' + 'if(arguments[0] > 0) ' + 'console.log(arguments);' + '})(6)'; eval(a); var a = 'if(arguments[0] > 0) ' + 'console.log(arguments);'; new Function(a)(5);

Slide 69

Slide 69 text

Referenzwerte / Schwellenwerte + Kontext

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Referenzwerte / Schwellenwerte Niedrig Normal Hoch Sehr hoch Cyclomatic Complexity 1-4 5-7 8-10 >= 11 nPath / / / >= 200

Slide 74

Slide 74 text

Java (45 Projekte) Metrik Niedrig Normal Hoch Sehr hoch CYCLO/LOC 0.16 0.20 0.24 0.36 LOC/Methode 7 10 13 19.5 NOM/Class 4 7 10 15

Slide 75

Slide 75 text

Java (45 Projekte) Metrik Niedrig Normal Hoch Sehr hoch CYCLO/LOC 0.16 0.20 0.24 0.36 LOC/Methode 7 10 13 19.5 NOM/Class 4 7 10 15 C++ (37 Projekte) Metrik Niedrig Normal Hoch Sehr hoch CYCLO/LOC 0.20 0.25 0.30 0.45 LOC/Methode 5 10 16 24 NOM/Class 4 9 15 22.5

Slide 76

Slide 76 text

Metriken

Slide 77

Slide 77 text

Source Code

Slide 78

Slide 78 text

Source Code Functions count Nested depth Halstead Maintainability index LOC Parameter count Cyclomatic Complexity nPath Lint errors CLOC LLOC Your metric here

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

Verwendete Bilder • „Ruler“ by Scott Akerman: https://www.flickr.com/ photos/sterlic/4299631538/ • „the JavaScript Code“ by Dmitry Baranovskiy: https:// www.flickr.com/photos/dmitry-baranovskiy/2378867408 • „Ignition“ by Zach Dischner: https://www.flickr.com/ photos/zachd1_618/3489625168 • „Tools IMG_0171“ by OZinOH: https://www.flickr.com/ photos/75905404@N00/7126146307

Slide 83

Slide 83 text

Verwendete Bilder • „Danger & Skull, Legoland“ by bixentro: https:// www.flickr.com/photos/bixentro/338433029 • „Baby“ by The Noun Project: http://thenounproject.com/ term/baby/47/ • „Man“ by The Noun Project: http://thenounproject.com/ term/man/2/ • „Sasquatch“ by Mike Wirth: http://thenounproject.com/ term/sasquatch/2680/

Slide 84

Slide 84 text

Verwendete Bilder • „Waking Up In Abbeyford Woods“ by Miles Wolstenholme: https://www.flickr.com/photos/ oaktorphotography/14444806464 • „Danke 102/365“ by Dennis Skley: https:// www.flickr.com/photos/dskley/13796815083/ • „Questions“ by Oberazzi: https://www.flickr.com/photos/ oberazzi/318947873/in/photostream/

Slide 85

Slide 85 text

Zitate und Tabellen • Helmut Balzert: Lehrbuch der Softwaretechnik. Band 2: Softwaremanagement, Software-Qualitätssicherung, Unternehmensmodellierung, Spektrum Akademischer Verlag, Heidelberg 1998, ISBN 3-8274-0065-1, S. 257 • Michele Lanza, Radu Marinescu: Object-Oriented Metrics in Practice: Using Software Metrics to Characterize, Evaluate, and Improve the Design of Object-Oriented Systems, Springer 2006, ISBN 3540244298