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

7,956 views

Published on

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.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,956
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
26
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

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

  1. 1. Magic  behind  the  numbers    So.ware  metrics  in  prac&ce  Sebas&an  Marek,  So.ware  Architect  
  2. 2. •  a  Pole  living  in  Sheffield   •  over  12  years  in   development   •  Pascal,  C++,  PHP,  perl,   python,  Java   •  co-­‐author  of  2  PHP  books     •  big  fan  of  process   automa&on   •  TDD  and  CI   •  occasionally  contributes  to   open  source  projects   •  wants  to  be  a  knight  @proofek  
  3. 3. • What  I  will  be  talking  about   •  Code  metrics   •  Design  metrics   • What  I  will  not  be  talking  about   •  Project  metrics  Agenda  
  4. 4. Most  effec&ve  code  quality  measure  
  5. 5. “It is the mapping of a particular characteristic of a measured entity to a numerical value” Source: Object-Oriented Metrics in PracticeWhat  is  a  metric?  
  6. 6. “Good design quality metrics are not necessarily indicative of good designs. Likewise, bad design quality metrics are not necessarily indicative of bad designs” Source: Jdepend DocsSo.ware  design  
  7. 7. •  Obsolete  documenta&on   •  Convoluted  design   •  Intensive  patch  mechanism   (hacking)   •  Large  size   •  Severe  duplica&on   •  Obsolete  parts  (dead  code)   •  Long  build  &mes   •  Loss  of  original  developers  System  maintenance    
  8. 8. •  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  Simple  metrics  
  9. 9. Cyclomatic complexity measures the amount of decision logic in a single software moduleCycloma&c  Complexity  (CYCLO)  
  10. 10. Condi3onal  statements:     •  ?   •  case   •  elseif   •  for   •  foreach   •  if   •  while  Cycloma&c  Complexity  Number  (CCN)  
  11. 11. Condi3onal  statements:     •  ?   •  &&   •  case   •  ||   •  elseif   •  for   •  or   •  foreach   •  and   •  if   •  xor   •  while  CCN2  
  12. 12. Condi3onal  statements:     •  ?   •  &&   •  case   •  ||   •  elseif   •  for   •  or   •  foreach   •  and   •  if   •  xor   •  while   •  catch  Cycloma&c  Complexity  
  13. 13. class 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(); } }}Cycloma&c  complexity  -­‐  example  
  14. 14. class 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(); } }}Cycloma&c  complexity  -­‐  example  
  15. 15. phploc 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  phploc  
  16. 16. Low   Avg   High   V.High   Complexity   1-­‐4   5-­‐7   8-­‐10   >  11  Cycloma&c  complexity  -­‐  thresholds  
  17. 17. Metric   Low   Avg   High   V.High   CYCLO/Line  of  code   0.16   0.20   0.24   0.36   LOC/Method   7   10   13   19.5   NOM/Class   4   7   10   15   Source:  Object-­‐Oriented  Metrics  in  Prac5ce  (based  on  45  Java  projects)  JAVA  
  18. 18. Metric   Low   Avg   High   V.High   CYCLO/Line  of  code   0.20   0.25   0.30   0.45   LOC/Method   5   10   16   24   NOM/Class   4   9   15   22.5   Source:  Object-­‐Oriented  Metrics  in  Prac5ce  (based  on  37  C++  projects)  C++  
  19. 19. Weighted  Method  Count  –  total  complexity  of  a  class   Average  Method  Weight  –  average  complexity  of  a  method  WMC  and  AMW  
  20. 20. Metric   Low   Avg   High   V.High   WMC   5   14   31   47   AMW   1.1   2.0   3.1   4.7   LOC/Class   28   70   130   195   Source:  Object-­‐Oriented  Metrics  in  Prac5ce  (based  on  45  Java  projects)  JAVA  
  21. 21. Metric   Low   Avg   High   V.High   WMC   4   23   72   108   AMW   1.0   2.5   4.8   7.0   LOC/Class   20   90   240   360   Source:  Object-­‐Oriented  Metrics  in  Prac5ce  (based  on  37  C++  projects)  C++  
  22. 22. Coverage  report  
  23. 23. Coverage  report  
  24. 24. C.R.A.P  
  25. 25. Change   Risk   Analysis  and     Predic&ons  C.R.A.P  
  26. 26. Code  coverage  =  100%   Code  coverage  =  0%  C.R.A.P  formula  
  27. 27. C.R.A.P  thresholds  
  28. 28. “NPATH is an objective measure of software complexity related to the ease with which software can be comprehensively tested” Edgar H. SibleyNPATH  –  acyclic  execu&on  path  complexity  
  29. 29. expressions 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 NPATH  –  acyclic  execu&on  path  complexity  
  30. 30. class 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; }}NPATH  –  example  
  31. 31. class 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 }}NPATH  –  example  
  32. 32. PHP_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  PHP  Depend  
  33. 33. PHP  Mess  Detector  
  34. 34. Overview  pyramid  
  35. 35. Size  and  complexity  –  direct  metrics  
  36. 36. Size  and  complexity  –  computed  propor&ons  
  37. 37. System  coupling  –  direct  metrics  
  38. 38. System  coupling  –  computed  propor&ons  
  39. 39. System  inheritance  
  40. 40. Complete  Overview  Pyramid  
  41. 41. Metric   Low   Avg   High   CYCLO/LOC   0.16   0.20   0.24   LOC/NOM   7   10   13   NOM/NOC   4   7   10   NOC/NOP   6   17   26   CALLS/NOM   2.01   2.62   3.2   FANOUT/CALLS   0.56   0.62   0.68   ANDC   0.25   0.41   0.57   AHH   0.09   0.21   0.32   hBp://pdepend.org/  PHP  
  42. 42. Metrics  visualisa&on  with  Sonar  
  43. 43. Metrics  visualisa&on  with  Sonar  
  44. 44. Viola&ons  repor&ng  
  45. 45. -­‐-­‐   Very  bad   -­‐   Bad   0   Average   +   Good   ++   Very  good    SIG  Maintainability  Model  
  46. 46. Technical  Debt  
  47. 47. “We  believe  that  soIware  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  Summary  
  48. 48. •  PHP  Depend  -­‐  hmp://pdepend.org/   •  PHP  Mess  Detector  -­‐  hmp://phpmd.org/   •  Manuel’s  home  page  -­‐  hmp://manuel-­‐pichler.de/   •  PHPUnit  -­‐  hmp://www.phpunit.de/   •  phploc  -­‐  hmp://sebas&anbergmann.github.com/phploc/   •  Sonar  -­‐  hmp://www.sonarsource.org/  Resources  
  49. 49. “Object-­‐Oriented  Metrics  in  Prac&ce”  by  Michele  Lanza  and   Radu  Marinescu  (ISBN  978-­‐3540244295)    Object-­‐Oriented  Metrics  in  Prac&ce  
  50. 50. •  hmp://www.flickr.com/photos/romainguy/102073478/   •  hmp://www.flickr.com/photos/shanafin/615680234/   •  hmp://www.flickr.com/photos/pahudson/3080270873/   •  hmp://www.flickr.com/photos/smohundro/2243554264/   •  hmp://www.flickr.com/photos/m_a_melendez/5956157581/   •  hmp://www.flickr.com/photos/elkit/7182541802/   •  hmp://www.flickr.com/photos/rickyromero/4209571303/    Thanks  to…  
  51. 51. Ques&ons?   hmps://joind.in/6445  Ques&ons?  

×