Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
#phpday          Dica trentatrè <?php echo ’33’ ?>           Controllare lo stato di salute di una applicazione con le    ...
#phpday          Code Metrics
Measure of some property of a piece#phpday               of software (Wikipedia)
#phpday          Who Cares?
#phpday          How long will it take to add feature X?
#phpday          Internal vs External Quality
Code Metrics as a mean to expose#phpday                  internal quality
#phpday          How long will it take to add feature X?
#phpday          Classic
Lines and Numbers          Lines                  LOC: Lines Of Code#phpday           ELOC: Executable LOC                ...
Lines and Numbers                            1    <?php                            2           Lines            3    names...
Lines and Numbers                            1    <?php                            2           Lines            3    names...
Lines and Numbers                            1    <?php                            2           Lines            3    names...
Lines and Numbers                            1    <?php                            2           Lines            3    names...
Lines and Numbers                            1    <?php                            2           Lines            3    names...
Lines and Numbers                            1    <?php                            2           Lines            3    names...
Lines and Numbers                            1    <?php                            2           Lines            3    names...
Lines and Numbers                            1    <?php                            2           Lines            3    names...
#phpday                       phploc          http://sebastianbergmann.github.com/phploc/
#phpday
#phpday          Complexity
Complexity#phpday          Control Structures Complexity           if, elseif, for, while, foreach, catch, case, xor,     ...
Ciclomatic Complexity                              1    <?php                              2          # decision points   ...
Ciclomatic Complexity                                  1    <?php                                  2          # decision p...
Ciclomatic Complexity                                  1    <?php                                  2          # decision p...
Ciclomatic Complexity                                  1    <?php                                  2          # decision p...
Ciclomatic Complexity                                  1    <?php                                  2          # decision p...
CCN Range Values#phpday         1 - 4: low               5-7: medium                8-10: high                11+: wtf
NPath Complexity                            1    <?php                            2          # acyclic paths   3    class ...
NPath Complexity                                1    <?php                                2          # acyclic paths      ...
NPath Complexity                                1    <?php                                2          # acyclic paths      ...
NPath Complexity                                1    <?php                                2          # acyclic paths      ...
NPath Complexity                                1    <?php                                2          # acyclic paths      ...
NPath Treshold#phpday             Critical if > 200
#phpday          PHP Depend           http://pdepend.org
#phpday
Overview Pyramid#phpday
#phpday          OO Metrics
Afferent/Efferent Coupling#phpday          Ca: no. of unique incoming dependencies          Ce: no. of unique outgoing dep...
Afferent/Efferent Coupling                         Shape                         area()#phpday               Circle       ...
Afferent/Efferent Coupling                            Shape                             area()#phpday               Circle...
Afferent/Efferent Coupling                               Shape                                area()#phpday          Ca: 0...
Afferent/Efferent Coupling                               Shape     Ca: 2                                area()   Ce: 0#php...
Afferent/Efferent Coupling                               Shape     Ca: 2                                area()   Ce: 0#php...
#phpday          $ pdepend --jdepend-chart=j.png .
Instability Chart#phpday          A = AC / (CC + AC)                               D = | A + I | -1          I = Ce / (Ce ...
#phpday          PHP MD          http://phpmd.org
PHP MD#phpday    Code Size Rules            Design Rules            Naming Rules          Unused Code Rules
#phpday             $ phpmd src/ xml          codesize,design,naming,unusedcode --                   reportfile pmd.xml
#phpday
#phpday          Non Static Analysis
C.R.A.P           Change Risk Analysis Predictions or#phpday                  “Your code is crap”          CRAP = CCN^2 * ...
#phpday          Putting it all together
#phpday             PHP_CodeBrowser          https://github.com/mayflowergmbh/PHP_CodeBrowser.git
$ phpcb --log logs --source src --output#phpday                      code-browser
#phpday             PHP Project Wizard          https://github.com/sebastianbergmann/php-project-wizard.git
Code Metrics as a mean to expose#phpday                  internal quality
Further Reading          http://manuel-pichler.de/#phpday          http://pdepend.org          http://phpmd.org          h...
Questions?              @_orso_#phpday            mo@ideato.it          http://joind.in/2989
Upcoming SlideShare
Loading in …5
×

Dica trentatrè. &lt;?php echo "33"; ?>. Controllare lo stato di salute di una applicazione con le metriche del codice

1,787 views

Published on

Presentation from phpday 2011 about software metrics and tools in php

Abstract:
Esiste un modo per decidere se una porzione di codice è migliore di un altra? Le metriche del codice forniscono misure oggettive sulla qualità del codice stesso. Nel talk verranno introdotte le principali metriche, gli strumenti che permettono di ricavarle e il loro utilizzo nel flusso di sviluppo.

Published in: Technology
  • Login to see the comments

Dica trentatrè. &lt;?php echo "33"; ?>. Controllare lo stato di salute di una applicazione con le metriche del codice

  1. 1. #phpday Dica trentatrè <?php echo ’33’ ?> Controllare lo stato di salute di una applicazione con le metriche del codice Michele Orselli (@_orso_)
  2. 2. #phpday Code Metrics
  3. 3. Measure of some property of a piece#phpday of software (Wikipedia)
  4. 4. #phpday Who Cares?
  5. 5. #phpday How long will it take to add feature X?
  6. 6. #phpday Internal vs External Quality
  7. 7. Code Metrics as a mean to expose#phpday internal quality
  8. 8. #phpday How long will it take to add feature X?
  9. 9. #phpday Classic
  10. 10. Lines and Numbers Lines LOC: Lines Of Code#phpday ELOC: Executable LOC CLOC: Comment LOC NCLOC: Non-Comment LOC Numbers NOC: Number Of Classes NOM: Number Of Methods NOP: Number Of Packages
  11. 11. Lines and Numbers 1 <?php 2 Lines 3 namespace shapestwodimensional; 4 5 abstract class shape { 6 /* Returns the area of the shape */#phpday 7 abstract function area(); 8 } 9 10 class circle extends shape { 11 public function area() {} 12 13 /* Ret the diameter of the circle */ Numbers 14 public function diameter() {} 15 } 16 17 class triangle extends shape { 18 19 public function area() {} 20 } 21 22 ?>
  12. 12. Lines and Numbers 1 <?php 2 Lines 3 namespace shapestwodimensional; 4 LOC 22 5 abstract class shape { 6 /* Returns the area of the shape */#phpday 7 abstract function area(); 8 } 9 10 class circle extends shape { 11 public function area() {} 12 13 /* Ret the diameter of the circle */ Numbers 14 public function diameter() {} 15 } 16 17 class triangle extends shape { 18 19 public function area() {} 20 } 21 22 ?>
  13. 13. Lines and Numbers 1 <?php 2 Lines 3 namespace shapestwodimensional; 4 LOC 22 5 abstract class shape { 6 /* Returns the area of the shape */#phpday CLOC 2 7 abstract function area(); 8 } 9 10 class circle extends shape { 11 public function area() {} 12 13 /* Ret the diameter of the circle */ Numbers 14 public function diameter() {} 15 } 16 17 class triangle extends shape { 18 19 public function area() {} 20 } 21 22 ?>
  14. 14. Lines and Numbers 1 <?php 2 Lines 3 namespace shapestwodimensional; 4 LOC 22 5 abstract class shape { 6 /* Returns the area of the shape */#phpday CLOC 2 7 abstract function area(); 8 } NCLOC 20 9 10 class circle extends shape { 11 public function area() {} 12 13 /* Ret the diameter of the circle */ Numbers 14 public function diameter() {} 15 } 16 17 class triangle extends shape { 18 19 public function area() {} 20 } 21 22 ?>
  15. 15. Lines and Numbers 1 <?php 2 Lines 3 namespace shapestwodimensional; 4 LOC 22 5 abstract class shape { 6 /* Returns the area of the shape */#phpday CLOC 2 7 abstract function area(); 8 } NCLOC 20 9 10 class circle extends shape { ELOC 3 11 public function area() {} 12 13 /* Ret the diameter of the circle */ Numbers 14 public function diameter() {} 15 } 16 17 class triangle extends shape { 18 19 public function area() {} 20 } 21 22 ?>
  16. 16. Lines and Numbers 1 <?php 2 Lines 3 namespace shapestwodimensional; 4 LOC 22 5 abstract class shape { 6 /* Returns the area of the shape */#phpday CLOC 2 7 abstract function area(); 8 } NCLOC 20 9 10 class circle extends shape { ELOC 3 11 public function area() {} 12 13 /* Ret the diameter of the circle */ Numbers 14 public function diameter() {} 15 } NOP 1 16 17 class triangle extends shape { 18 19 public function area() {} 20 } 21 22 ?>
  17. 17. Lines and Numbers 1 <?php 2 Lines 3 namespace shapestwodimensional; 4 LOC 22 5 abstract class shape { 6 /* Returns the area of the shape */#phpday CLOC 2 7 abstract function area(); 8 } NCLOC 20 9 10 class circle extends shape { ELOC 3 11 public function area() {} 12 13 /* Ret the diameter of the circle */ Numbers 14 public function diameter() {} 15 } NOP 1 16 17 class triangle extends shape { NOC 3 18 19 public function area() {} 20 } 21 22 ?>
  18. 18. Lines and Numbers 1 <?php 2 Lines 3 namespace shapestwodimensional; 4 LOC 22 5 abstract class shape { 6 /* Returns the area of the shape */#phpday CLOC 2 7 abstract function area(); 8 } NCLOC 20 9 10 class circle extends shape { ELOC 3 11 public function area() {} 12 13 /* Ret the diameter of the circle */ Numbers 14 public function diameter() {} 15 } NOP 1 16 17 class triangle extends shape { NOC 3 18 19 public function area() {} NOM 4 20 } 21 22 ?>
  19. 19. #phpday phploc http://sebastianbergmann.github.com/phploc/
  20. 20. #phpday
  21. 21. #phpday Complexity
  22. 22. Complexity#phpday Control Structures Complexity if, elseif, for, while, foreach, catch, case, xor, and, or, &&, ||, ?: Ciclomatic Complexity (CCN) NPath Complexity
  23. 23. Ciclomatic Complexity 1 <?php 2 # decision points 3 class polygon { 4 5 public function perimeter() 6 {#phpday CCN 7 if (!is_array($this->vertices)) { 8 return false; 9 } 10 11 $perimeter = 0; 12 13 foreach ($this->vertices as $vertex) { 14 $perimenter += $vertex; 15 } 16 17 return $perimeter; 18 } 19 } 20 ?>
  24. 24. Ciclomatic Complexity 1 <?php 2 # decision points 3 class polygon { 4 5 public function perimeter() 6 {#phpday CCN 1 7 if (!is_array($this->vertices)) { 8 return false; 9 } 10 11 $perimeter = 0; 12 13 foreach ($this->vertices as $vertex) { 14 $perimenter += $vertex; 15 } 16 17 return $perimeter; 18 } 19 } 20 ?>
  25. 25. Ciclomatic Complexity 1 <?php 2 # decision points 3 class polygon { 4 5 public function perimeter() 6 {#phpday CCN 2 7 if (!is_array($this->vertices)) { 8 return false; 9 } 10 11 $perimeter = 0; 12 13 foreach ($this->vertices as $vertex) { 14 $perimenter += $vertex; 15 } 16 17 return $perimeter; 18 } 19 } 20 ?>
  26. 26. Ciclomatic Complexity 1 <?php 2 # decision points 3 class polygon { 4 5 public function perimeter() 6 {#phpday CCN 3 7 if (!is_array($this->vertices)) { 8 return false; 9 } 10 11 $perimeter = 0; 12 13 foreach ($this->vertices as $vertex) { 14 $perimenter += $vertex; 15 } 16 17 return $perimeter; 18 } 19 } 20 ?>
  27. 27. Ciclomatic Complexity 1 <?php 2 # decision points 3 class polygon { 4 5 public function perimeter() 6 {#phpday CCN 4 7 if (!is_array($this->vertices)) { 8 return false; 9 } 10 11 $perimeter = 0; 12 13 foreach ($this->vertices as $vertex) { 14 $perimenter += $vertex; 15 } 16 17 return $perimeter; 18 } 19 } 20 ?>
  28. 28. CCN Range Values#phpday 1 - 4: low 5-7: medium 8-10: high 11+: wtf
  29. 29. NPath Complexity 1 <?php 2 # acyclic paths 3 class polygon { 4 5 public function perimeter() 6 {#phpday NPath 7 if (!is_array($this->vertices)) { 8 return false; 9 } 10 11 $perimeter = 0; 12 13 foreach ($this->vertices as $vertex) { 14 $perimenter += $vertex; 15 } 16 17 return $perimeter; 18 } 19 } 20 ?>
  30. 30. NPath Complexity 1 <?php 2 # acyclic paths 3 class polygon { 4 5 public function perimeter() 6 {#phpday NPath 1 7 if (!is_array($this->vertices)) { 8 return false; 9 } 10 11 $perimeter = 0; 12 13 foreach ($this->vertices as $vertex) { 14 $perimenter += $vertex; 15 } 16 17 return $perimeter; 18 } 19 } 20 ?>
  31. 31. NPath Complexity 1 <?php 2 # acyclic paths 3 class polygon { 4 5 public function perimeter() 6 {#phpday NPath 2 7 if (!is_array($this->vertices)) { 8 return false; 9 } 10 11 $perimeter = 0; 12 13 foreach ($this->vertices as $vertex) { 14 $perimenter += $vertex; 15 } 16 17 return $perimeter; 18 } 19 } 20 ?>
  32. 32. NPath Complexity 1 <?php 2 # acyclic paths 3 class polygon { 4 5 public function perimeter() 6 {#phpday NPath 3 7 if (!is_array($this->vertices)) { 8 return false; 9 } 10 11 $perimeter = 0; 12 13 foreach ($this->vertices as $vertex) { 14 $perimenter += $vertex; 15 } 16 17 return $perimeter; 18 } 19 } 20 ?>
  33. 33. NPath Complexity 1 <?php 2 # acyclic paths 3 class polygon { 4 5 public function perimeter() 6 {#phpday NPath 4 7 if (!is_array($this->vertices)) { 8 return false; 9 } 10 11 $perimeter = 0; 12 13 foreach ($this->vertices as $vertex) { 14 $perimenter += $vertex; 15 } 16 17 return $perimeter; 18 } 19 } 20 ?>
  34. 34. NPath Treshold#phpday Critical if > 200
  35. 35. #phpday PHP Depend http://pdepend.org
  36. 36. #phpday
  37. 37. Overview Pyramid#phpday
  38. 38. #phpday OO Metrics
  39. 39. Afferent/Efferent Coupling#phpday Ca: no. of unique incoming dependencies Ce: no. of unique outgoing dependencies
  40. 40. Afferent/Efferent Coupling Shape area()#phpday Circle Triangle area() area() Math PI
  41. 41. Afferent/Efferent Coupling Shape area()#phpday Circle Triangle area() area() Math Ca: 1 PI Ce: 0
  42. 42. Afferent/Efferent Coupling Shape area()#phpday Ca: 0 Circle Triangle Ce: 2 area() area() Math Ca: 1 PI Ce: 0
  43. 43. Afferent/Efferent Coupling Shape Ca: 2 area() Ce: 0#phpday Ca: 1 Circle Triangle Ce: 1 area() area() Math Ca: 1 PI Ce: 0
  44. 44. Afferent/Efferent Coupling Shape Ca: 2 area() Ce: 0#phpday Ca: 1 Circle Triangle Ca: 0 Ce: 1 area() area() Ce: 1 Math Ca: 1 PI Ce: 0
  45. 45. #phpday $ pdepend --jdepend-chart=j.png .
  46. 46. Instability Chart#phpday A = AC / (CC + AC) D = | A + I | -1 I = Ce / (Ce + Ca)
  47. 47. #phpday PHP MD http://phpmd.org
  48. 48. PHP MD#phpday Code Size Rules Design Rules Naming Rules Unused Code Rules
  49. 49. #phpday $ phpmd src/ xml codesize,design,naming,unusedcode -- reportfile pmd.xml
  50. 50. #phpday
  51. 51. #phpday Non Static Analysis
  52. 52. C.R.A.P Change Risk Analysis Predictions or#phpday “Your code is crap” CRAP = CCN^2 * (1 – COV/100)^3 + CCN
  53. 53. #phpday Putting it all together
  54. 54. #phpday PHP_CodeBrowser https://github.com/mayflowergmbh/PHP_CodeBrowser.git
  55. 55. $ phpcb --log logs --source src --output#phpday code-browser
  56. 56. #phpday PHP Project Wizard https://github.com/sebastianbergmann/php-project-wizard.git
  57. 57. Code Metrics as a mean to expose#phpday internal quality
  58. 58. Further Reading http://manuel-pichler.de/#phpday http://pdepend.org http://phpmd.org http://www.artima.com/weblogs/viewpost.jsp?thread=215899
  59. 59. Questions? @_orso_#phpday mo@ideato.it http://joind.in/2989

×