Successfully reported this slideshow.
Magic behind the                            numbers                       Software metrics in                            p...
@proofek•  a Pole living in Sheffield•  over 10 years PHP   experience•  co-author of 2 PHP books•  big fan of process   a...
AgendaWhat I will be talking about•  Code metrics•  Design metricsWhat I will not be talking about•  Project metrics      ...
Most effective code quality measure
What is a metric?  “It is the mapping of a particularcharacteristic of a measured entity to          a numerical value”   ...
Software design “Good design quality metrics are notnecessarily indicative of good designs. Likewise, bad design quality m...
System maintenance•    Obsolete documentation•    Convoluted design•    Intensive patch mechanism (hacking)•    Large size...
Simple metrics•    CLOC – comment lines of code•    ELOC – executable lines of code•    LOC – lines of code•    NCLOC – no...
Cyclomatic Complexity (CYCLO)   Cyclomatic complexity measures theamount of decision logic in a single software           ...
Cyclomatic Complexity Number (CCN)Conditional statements:•  ?•  case•  elseif•  for•  foreach•  if•  while
CCN2Conditional statements:•  ?                         •    &&•  case                      •    ||•  elseif              ...
Cyclomatic complexityConditional statements:•  ?                      •    &&•  case                   •    ||•  elseif   ...
Cyclomatic complexity - exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1...
Cyclomatic complexity - exampleclass Foo {    public function example() {             1        if ($a == $b) {            ...
Cyclomatic complexity - exampleclass Foo {    public function example() {             1        if ($a == $b) {            ...
Cyclomatic complexity - exampleclass Foo {    public function example() {             1        if ($a == $b) {            ...
Cyclomatic complexity - exampleclass Foo {    public function example() {             1        if ($a == $b) {            ...
Cyclomatic complexity - exampleclass Foo {    public function example() {             1        if ($a == $b) {            ...
Cyclomatic complexity - exampleclass Foo {    public function example() {             1        if ($a == $b) {            ...
Cyclomatic complexity - exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1...
phplocphploc 1.6.1 by Sebastian Bergmann.		Directories:                                      6	Files:                     ...
Cyclomatic complexity - thresholds                Low     Avg      High    V.HighComplexity      1-4     5-7      8-10    ...
JAVAMetric                Low               Avg                High               V.HighCYCLO/Line of code    0.16        ...
C++Metric                Low               Avg                High              V.HighCYCLO/Line of code    0.20          ...
WMC and AMWWeighted Method Count – total complexity of a classAverage Method Weight – average complexity of a method
JAVAMetric       Low               Avg                High               V.HighWMC          5                 14          ...
C++Metric       Low               Avg                High              V.HighWMC          4                 23            ...
Coverage report
C.R.A.P
C.R.A.PChangeRiskAnalysis and  Change Risk Analysis and PredictionsPredictions
C.R.A.P formulaCode coverage = 100% Code coverage = 0%
NPATH – acyclic execution path complexityNPATH is an objective measure of software complexity related to the ease with whi...
NPATH – acyclic execution path complexityexpressions         Number of && and || operators in expression		if              ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {                     2            if ($a1 ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {                     2            if ($a1 ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
NPATH – exampleclass Foo {    public function example() {        if ($a == $b) {            if ($a1 == $b1) {             ...
PHP DependPHP_Depend 0.10.6 by Manuel Pichler		Parsing source files:	....................                                 ...
PHP Mess Detector
Overview pyramid
Size and complexity – direct metrics
Size and complexity – computed proportions
System coupling – direct metrics
System coupling – computed proportions
System inheritance
Complete Overview Pyramid
PHPMetric         Low          Avg       HighCYCLO/LOC      0.16         0.20      0.24LOC/NOM        7            10     ...
Metrics visualisation with Sonar
Metrics visualisation with Sonar
Violations reporting
SIG Maintainability Model--   Very bad-    Bad0    Average+    Good++   Very good
Technical Debt
Summary“We believe that software metrics, in general, are just tools. No  single metric can tell the whole story; it’s jus...
Resources•    PHP Depend - http://pdepend.org/•    PHP Mess Detector - http://phpmd.org/•    Manuel’s home page - http://m...
QuestionsQuestions?https://joind.in/4758
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
Nächste SlideShare
Wird geladen in …5
×

PHP Benelux 2012: Magic behind the numbers. Software metrics in practice

2.446 Aufrufe

Veröffentlicht am

We use static code analysis tools more often these days that create great reports and funky graphs. But do we understand what it all means?

Software metrics tends to be magic numbers for a lot of people, but they don’t really have to be. Seb will introduce you to a few basic, the most popular software metrics and tools. He will explain to you what they mean and how you can use them to produce better software.

Veröffentlicht in: Technologie, Unterhaltung & Humor
  • Als Erste(r) kommentieren

PHP Benelux 2012: Magic behind the numbers. Software metrics in practice

  1. 1. Magic behind the numbers Software metrics in practiceSebastian Marek, Software Architect
  2. 2. @proofek•  a Pole living in Sheffield•  over 10 years PHP experience•  co-author of 2 PHP books•  big fan of process automation•  TDD and CI•  occasionally contributes to open source projects•  wants to be a knight
  3. 3. AgendaWhat I will be talking about•  Code metrics•  Design metricsWhat I will not be talking about•  Project metrics https://joind.in/4758
  4. 4. Most effective code quality measure
  5. 5. What is a metric? “It is the mapping of a particularcharacteristic of a measured entity to a numerical value” Source: Object-Oriented Metrics in Practice
  6. 6. Software design “Good design quality metrics are notnecessarily indicative of good designs. Likewise, bad design quality metrics are not necessarily indicative of bad designs” Source: Jdepend Docs
  7. 7. System maintenance•  Obsolete documentation•  Convoluted design•  Intensive patch mechanism (hacking)•  Large size•  Severe duplication•  Obsolete parts (dead code)•  Long build times•  Loss of original developers
  8. 8. Simple metrics•  CLOC – comment lines of code•  ELOC – executable lines of code•  LOC – lines of code•  NCLOC – non comment lines of code•  NOP – number of packages•  NOC – number of classes•  NOM – number of methods
  9. 9. Cyclomatic Complexity (CYCLO) Cyclomatic complexity measures theamount of decision logic in a single software module.
  10. 10. Cyclomatic Complexity Number (CCN)Conditional statements:•  ?•  case•  elseif•  for•  foreach•  if•  while
  11. 11. CCN2Conditional statements:•  ? •  &&•  case •  ||•  elseif •  or•  for •  and•  foreach •  xor•  if•  while
  12. 12. Cyclomatic complexityConditional statements:•  ? •  &&•  case •  ||•  elseif •  or•  for •  and•  foreach •  xor•  if •  catch•  while
  13. 13. Cyclomatic complexity - exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } elseif ($a2 == $b2) { fiddle(); } else { fiddle(); } } elseif ($e == $f) { for ($n = 0; $n < $h; $n++) { fiddle(); } } else { fiddle(); } }}
  14. 14. Cyclomatic complexity - exampleclass Foo { public function example() { 1 if ($a == $b) { if ($a1 == $b1) { fiddle(); } elseif ($a2 == $b2) { fiddle(); } else { fiddle(); } } elseif ($e == $f) { for ($n = 0; $n < $h; $n++) { fiddle(); } } else { fiddle(); } }}
  15. 15. Cyclomatic complexity - exampleclass Foo { public function example() { 1 if ($a == $b) { 2 if ($a1 == $b1) { fiddle(); } elseif ($a2 == $b2) { fiddle(); } else { fiddle(); } } elseif ($e == $f) { for ($n = 0; $n < $h; $n++) { fiddle(); } } else { fiddle(); } }}
  16. 16. Cyclomatic complexity - exampleclass Foo { public function example() { 1 if ($a == $b) { 2 if ($a1 == $b1) { 3 fiddle(); } elseif ($a2 == $b2) { fiddle(); } else { fiddle(); } } elseif ($e == $f) { for ($n = 0; $n < $h; $n++) { fiddle(); } } else { fiddle(); } }}
  17. 17. Cyclomatic complexity - exampleclass Foo { public function example() { 1 if ($a == $b) { 2 if ($a1 == $b1) { 3 fiddle(); } elseif ($a2 == $b2) { 4 fiddle(); } else { fiddle(); } } elseif ($e == $f) { for ($n = 0; $n < $h; $n++) { fiddle(); } } else { fiddle(); } }}
  18. 18. Cyclomatic complexity - exampleclass Foo { public function example() { 1 if ($a == $b) { 2 if ($a1 == $b1) { 3 fiddle(); } elseif ($a2 == $b2) { 4 fiddle(); } else { fiddle(); } } elseif ($e == $f) { 5 for ($n = 0; $n < $h; $n++) { fiddle(); } } else { fiddle(); } }}
  19. 19. Cyclomatic complexity - exampleclass Foo { public function example() { 1 if ($a == $b) { 2 if ($a1 == $b1) { 3 fiddle(); } elseif ($a2 == $b2) { 4 fiddle(); } else { fiddle(); } } elseif ($e == $f) { 5 for ($n = 0; $n < $h; $n++) { 6 fiddle(); } } else { fiddle(); } }}
  20. 20. Cyclomatic complexity - exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } elseif ($a2 == $b2) { fiddle(); } else { fiddle(); } 6 } elseif ($e == $f) { for ($n = 0; $n < $h; $n++) { fiddle(); } } else { fiddle(); } }}
  21. 21. phplocphploc 1.6.1 by Sebastian Bergmann. Directories: 6 Files: 20 Lines of Code (LOC): 5478 Cyclomatic Complexity / Lines of Code: 0.13 Comment Lines of Code (CLOC): 2143 Non-Comment Lines of Code (NCLOC): 3335 Namespaces: 0 Interfaces: 1 Classes: 18 Abstract: 2 (11.11%) Concrete: 16 (88.89%) Average Class Length (NCLOC): 191 Methods: 151 Scope: Non-Static: 143 (94.70%) Static: 8 (5.30%) Visibility: Public: 116 (76.82%) Non-Public: 35 (23.18%) Average Method Length (NCLOC): 22 Cyclomatic Complexity / Number of Methods: 3.72 Anonymous Functions: 0 Functions: 2 Constants: 4 Global constants: 3 Class constants: 1
  22. 22. Cyclomatic complexity - thresholds Low Avg High V.HighComplexity 1-4 5-7 8-10 > 11
  23. 23. JAVAMetric Low Avg High V.HighCYCLO/Line of code 0.16 0.20 0.24 0.36LOC/Method 7 10 13 19.5NOM/Class 4 7 10 15 Source: Object-Oriented Metrics in Practice (based on 45 Java projects)
  24. 24. C++Metric Low Avg High V.HighCYCLO/Line of code 0.20 0.25 0.30 0.45LOC/Method 5 10 16 24NOM/Class 4 9 15 22.5 Source: Object-Oriented Metrics in Practice (based on 37 C++ projects)
  25. 25. WMC and AMWWeighted Method Count – total complexity of a classAverage Method Weight – average complexity of a method
  26. 26. JAVAMetric Low Avg High V.HighWMC 5 14 31 47AMW 1.1 2.0 3.1 4.7LOC/Class 28 70 130 195 Source: Object-Oriented Metrics in Practice (based on 45 Java projects)
  27. 27. C++Metric Low Avg High V.HighWMC 4 23 72 108AMW 1.0 2.5 4.8 7.0LOC/Class 20 90 240 360 Source: Object-Oriented Metrics in Practice (based on 37 C++ projects)
  28. 28. Coverage report
  29. 29. C.R.A.P
  30. 30. C.R.A.PChangeRiskAnalysis and Change Risk Analysis and PredictionsPredictions
  31. 31. C.R.A.P formulaCode coverage = 100% Code coverage = 0%
  32. 32. NPATH – acyclic execution path complexityNPATH is an objective measure of software complexity related to the ease with which software can be comprehensively tested Edgar H. Sibley
  33. 33. NPATH – acyclic execution path complexityexpressions Number of && and || operators in expression if NP(<if-range>)+NP(<expr>)+1 if-else NP(<if-range>)+NP(<else-range>)+NP(<expr>) while NP(<while-range>)+NP(<expr>)+1 for NP(<for-range>)+NP(<expr1>)+NP(<expr2>)+ NP(<expr3>)+1 break 1 continue 1 return 1 sequential 1 function call 1
  34. 34. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } else { fiddle(); } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); } } return true; }}
  35. 35. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); 1 } else { fiddle(); } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); } } return true; }}
  36. 36. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); 1 } else { fiddle(); 2 } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); } } return true; }}
  37. 37. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { 2 fiddle(); 1 } else { fiddle(); 2 } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); } } return true; }}
  38. 38. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } else { 2 fiddle(); } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); } } return true; }}
  39. 39. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } else { 2 fiddle(); } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); } } return true; }}
  40. 40. NPATH – exampleclass Foo { public function example() { if ($a == $b) { 2 if ($a1 == $b1) { fiddle(); } else { 2 fiddle(); } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); } } return true; }}
  41. 41. NPATH – exampleclass Foo { public function example() { if ($a == $b) { 2 if ($a1 == $b1) { fiddle(); } else { 2 fiddle(); } } 3 if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); } } return true; }}
  42. 42. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } else { 3 fiddle(); } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); } } return true; }}
  43. 43. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } else { 3 fiddle(); } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); 1 } } return true; }}
  44. 44. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } else { 3 fiddle(); } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { 1 fiddle(); 1 } } return true; }}
  45. 45. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } else { 3 fiddle(); } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { 1 fiddle(); 1 } 2 } return true; }}
  46. 46. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } else { 3 fiddle(); } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); 2 } } return true; }}
  47. 47. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } else { 3 fiddle(); } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); 2 } } return true; }}
  48. 48. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } else { 3 fiddle(); } } if ($e == $f && $g == $z) { 3 for ($n = 0; $n < $h; $n++) { fiddle(); 2 } } return true; }}
  49. 49. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } else { 3 fiddle(); } } if ($e == $f && $g == $z) { 3 for ($n = 0; $n < $h; $n++) { fiddle(); 2 } } 4 return true; }}
  50. 50. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } else { 3 fiddle(); } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); 4 } } return true; }}
  51. 51. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } else { 3 fiddle(); } } if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); 4 } } return true; 1 }}
  52. 52. NPATH – exampleclass Foo { public function example() { if ($a == $b) { if ($a1 == $b1) { fiddle(); } else { fiddle(); } } 12 if ($e == $f && $g == $z) { for ($n = 0; $n < $h; $n++) { fiddle(); } } return true; }}
  53. 53. PHP DependPHP_Depend 0.10.6 by Manuel Pichler Parsing source files: .................... 20 Executing CyclomaticComplexity-Analyzer: ............. 261 Executing ClassLevel-Analyzer: ............ 247 Executing CodeRank-Analyzer: . 28 Executing Coupling-Analyzer: ............. 267 Executing Hierarchy-Analyzer: ............ 246 Executing Inheritance-Analyzer: . 30 Executing NPathComplexity-Analyzer: .............. 283 Executing NodeCount-Analyzer: ........ 174 Executing NodeLoc-Analyzer: .......... 205 Generating pdepend log files, this may take a moment. Time: 00:05; Memory: 25.50Mb
  54. 54. PHP Mess Detector
  55. 55. Overview pyramid
  56. 56. Size and complexity – direct metrics
  57. 57. Size and complexity – computed proportions
  58. 58. System coupling – direct metrics
  59. 59. System coupling – computed proportions
  60. 60. System inheritance
  61. 61. Complete Overview Pyramid
  62. 62. PHPMetric Low Avg HighCYCLO/LOC 0.16 0.20 0.24LOC/NOM 7 10 13NOM/NOC 4 7 10NOC/NOP 6 17 26CALLS/NOM 2.01 2.62 3.2FANOUT/CALLS 0.56 0.62 0.68ANDC 0.25 0.41 0.57AHH 0.09 0.21 0.32 http://pdepend.org/
  63. 63. Metrics visualisation with Sonar
  64. 64. Metrics visualisation with Sonar
  65. 65. Violations reporting
  66. 66. SIG Maintainability Model-- Very bad- Bad0 Average+ Good++ Very good
  67. 67. Technical Debt
  68. 68. Summary“We believe that software metrics, in general, are just tools. No single metric can tell the whole story; it’s just one more data point. ““Metrics are meant to be used by developers, not the other way around – the metric should work for you, you should not have to work for the metric. ““Metrics should never be an end unto themselves. Metrics are meant to help you think, not to do the thinking for you.” Alberto Savoia
  69. 69. Resources•  PHP Depend - http://pdepend.org/•  PHP Mess Detector - http://phpmd.org/•  Manuel’s home page - http://manuel-pichler.de/•  PHPUnit - http://www.phpunit.de/•  phploc - http://sebastianbergmann.github.com/phploc/•  Sonar - http://www.sonarsource.org/•  “Object-Oriented Metrics in Practice” by Michele Lanza and Radu Marinescu (ISBN 978-3540244295)
  70. 70. QuestionsQuestions?https://joind.in/4758

×