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.

Does Your Code Measure Up?

1,214 views

Published on

After days, weeks, or months of coding many developers don't know how to gauge the quality of their code. Adam Culp will introduce tools to grade, benchmark, and analyze code in an automated fashion allowing developers to write better quality software. He will explain key metrics to help understand what may need to be refactored, and use code smells to point out bugs before end-users discover them. Attendees will see how to use these tools, know where to find them, and be able to implement them in their own workflows.

Published in: Technology

Does Your Code Measure Up?

  1. 1. Does Your Code Measure Up? By: Adam Culp Twitter: @adamculp
  2. 2. 3 Does Your Code Measure Up? ● Fan of iteration – Pretty much everything requires iteration to do well: ● Long distance running ● Judo ● Development ● Evading project managers ● Code Quality!
  3. 3. 4 Does Your Code Measure Up? ● How To Measure? – Not productive
  4. 4. 5 Does Your Code Measure Up? ● Bad Music – Not a song writer
  5. 5. 6 Does Your Code Measure Up? ● How To Measure? – More accurate, quantifiable
  6. 6. 7 Does Your Code Measure Up? ● Why Measure? – Highlight bugs
  7. 7. 8 Does Your Code Measure Up? ● Why Measure? – Highlight bugs – Improve quality ● Easier onboarding ● Less reading, more writing ● Testable
  8. 8. 9 Does Your Code Measure Up? ● Why Measure? – Highlight bugs – Improve quality ● Easier onboarding ● Less reading, more writing ● Testable – Satisfied customers ● Faster development ● Less broken
  9. 9. 10 Does Your Code Measure Up? ● Why Measure? – Highlight bugs – Improve quality ● Easier onboarding ● Less reading, more writing ● Testable – Satisfied customers ● Faster development ● Less broken – Personal pride
  10. 10. 11 Does Your Code Measure Up? ● Why Measure? – Highlight bugs – Improve quality ● Easier onboarding ● Less reading, more writing ● Testable – Satisfied customers ● Faster development ● Less broken – Personal pride – Higher salary
  11. 11. 12 Does Your Code Measure Up? ● What To Measure? – Cyclomatic complexity ● “The count of the number of linearly independent paths through the source code.” - wikipedia ● Decision points ● Less than 10 (personally less than 6)
  12. 12. 13 Does Your Code Measure Up? ● Cyclomatic Complexity Example
  13. 13. 14 Does Your Code Measure Up? ● What To Measure? – Cyclomatic complexity – Duplicate code ● Rule of 3
  14. 14. 15 Does Your Code Measure Up? ● What To Measure? – Cyclomatic complexity – Duplicate code – Long classes ● Less than 1,000 lines ● Classes solve a (1) problem
  15. 15. 16 Does Your Code Measure Up? ● What To Measure? – Cyclomatic complexity – Duplicate code – Long classes – Class Complexity ● Less than 50
  16. 16. 17 Does Your Code Measure Up? ● What To Measure? – Cyclomatic complexity – Duplicate code – Long classes – Class complexity – Long methods ● Less than 100 (personally less than 20) ● Method should do one thing
  17. 17. 18 Does Your Code Measure Up? ● What To Measure? – Cyclomatic complexity – Duplicate code – Long classes – Class complexity – Long methods – Unused variables
  18. 18. 19 Does Your Code Measure Up? ● What To Measure? – Cyclomatic complexity – Duplicate code – Long classes – Class complexity – Long methods – Unused variables – Lack or overuse of comments ● Clear, concise, not explain bad code
  19. 19. 20 Does Your Code Measure Up? ● Comment Example
  20. 20. 21 Does Your Code Measure Up? ● What To Measure? – Cyclomatic complexity – Duplicate code – Long classes – Class complexity – Long methods – Unused variables – Lack or overuse of comments – Heavy global usage
  21. 21. 22 Does Your Code Measure Up? ● What To Measure? – Cyclomatic complexity – Duplicate code – Long classes – Class complexity – Long methods – Unused variables – Lack or overuse of comments – Heavy global usage – Npath complexity ● Possible paths through code ● Less than 200 paths
  22. 22. 23 Does Your Code Measure Up? ● What To Measure? – Cyclomatic complexity – Duplicate code – Long classes – Class complexity – Long methods – Unused variables – Lack or overuse of comments – Heavy global usage – Npath complexity – Much, much, more ● Code smells
  23. 23. 24 Does Your Code Measure Up? ● Code “smells” – What are “smells”? ● Indications of spoiled code nearby ● Not conclusive ● The “smell” is not bad
  24. 24. 25 Does Your Code Measure Up? ● Common Code “smells” – “Smells” hinting a refactor may be needed: ● Duplicate Code (rule of 3) ● Long Methods ● Large Class ● Long Parameter (argument) List ● Switch Statements – sacrifice polymorphism ● Temporary Field/Variable ● Comments – where comments cover bad code
  25. 25. 26 Does Your Code Measure Up? ● Tools – PHPqatools.org – DEPRECATED
  26. 26. 27 Does Your Code Measure Up? ● Tools – PHPqatools.org – DEPRECATED – https://github.com/exakat/php-static-analysis-tools
  27. 27. 28 Does Your Code Measure Up? ● Tools – PHPqatools.org – DEPRECATED ● PHPLoc
  28. 28. 29 Does Your Code Measure Up? ● PHPLoc Results
  29. 29. 30 Does Your Code Measure Up? ● Tools – PHPqatools.org – DEPRECATED ● PHPLoc ● PHP_Depend - https://pdepend.org/
  30. 30. 31 Does Your Code Measure Up? ● PHP_Depend Result – Graphs and XML output ANDC AHH NOP NOC NOM LOC CYCLO NOM CALLS FANOUT9891 96646 66812 395815 23359 342 3192 0.585 0.442 9.333 7.318 16.945 0.169 0.102 4.137 Generated by PHP_DependLow Average High
  31. 31. 32 Does Your Code Measure Up? ● Tools – PHPqatools.org – DEPRECATED ● PHPLoc ● PHP_Depend ● PHP Copy/Paste Detector
  32. 32. 33 Does Your Code Measure Up? ● PHP Copy/Paste Detector Result
  33. 33. 34 Does Your Code Measure Up? ● Tools – PHPqatools.org – DEPRECATED ● PHPLoc ● PHP_Depend ● PHP Copy/Paste Detector ● PHP Mess Detector – Codesize
  34. 34. 35 Does Your Code Measure Up? ● PHP Mess Detector Result (codesize)
  35. 35. 36 Does Your Code Measure Up? ● Tools – PHPqatools.org – DEPRECATED ● PHPLoc ● PHP_Depend ● PHP Copy/Paste Detector ● PHP Mess Detector – Codesize – Unused
  36. 36. 37 Does Your Code Measure Up? ● Tools – PHPqatools.org – DEPRECATED ● PHPLoc ● PHP_Depend ● PHP Copy/Paste Detector ● PHP Mess Detector – Codesize – Unused ● PHP Dead Code Detector – Searches for code no longer used
  37. 37. 38 Does Your Code Measure Up? ● Tools – PHPqatools.org – DEPRECATED – PHPMetrics ● http://phpmetrics.org
  38. 38. 39 Does Your Code Measure Up? ● PHPMetrics Results
  39. 39. 40 Does Your Code Measure Up? ● Tools – PHPqatools.org – DEPRECATED – PHPMetrics – PHP_Codesniffer ● Create rules/sniffs to ensure standards are followed ● From CLI, IDE, or via SCM hooks
  40. 40. 41 Does Your Code Measure Up? ● Containerized Tools – PHPLoc – PHPDepend – PHPMessDetector – PHPCopyPasteDetector – PHPMetrics – PHP_Codesniffer https://github.com/adamculp/php-code-quality https://hub.docker.com/u/adamculp/
  41. 41. 42 Does Your Code Measure Up? ● Docker Hub https://hub.docker.com/u/adamculp/
  42. 42. 43 Does Your Code Measure Up? ● Commercial Tools – Code Climate ● Build from Git/Github repo – Open = free – Private = $ ● GPA – like in high school, simple
  43. 43. 44 Does Your Code Measure Up? ● Commercial Tools – Code Climate – Scrutinizer
  44. 44. 45 Does Your Code Measure Up? ● Commercial Tools – Code Climate – Scrutinizer – Zend Server and Z-Ray ● Debugging, Code tracing, Error reporting
  45. 45. 46 Does Your Code Measure Up? ● Performance Tools – Apache Bench (apache2-utils in apt) ● How much traffic can you handle?
  46. 46. 47 Does Your Code Measure Up? ● Apache Bench Result
  47. 47. 48 Does Your Code Measure Up? ● Realtime profiling – Z-Ray
  48. 48. 49 Does Your Code Measure Up? ● Conclusion – Measure all the things! – Don't fear results, share them – Reduce complexity – Leave code cleaner than you got it – Learn to “smell” problems – Use refactoring to fix shortcomings – Love iteration!
  49. 49. 50 ● Thank you! – http://phpqatools.org https://github.com/exakat/php-static-analysis-tools – http://phpmetrics.org – https://github.com/adamculp/php-code-quality – http://zend.com – http://codeclimate.com – Please rate Adam Culp http://www.geekyboy.com http://RunGeekRadio.com Twitter @adamculp

×