Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Does Your Code Measure Up

Adam Culp
September 12, 2015

Does Your Code Measure Up

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 PHP 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.

Adam Culp

September 12, 2015
Tweet

More Decks by Adam Culp

Other Decks in Programming

Transcript

  1. 2 Does Your Code Measure Up? • About me –

    PHP 5.3 Certified – Consultant at Zend Technologies – Organizer SoFloPHP (South Florida) – Organized SunshinePHP (Miami) – Long distance (ultra) runner – Judo Black Belt Instructor
  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. 6 Does Your Code Measure Up? • How To Measure?

    – More accurate, quantifiable
  4. 8 Does Your Code Measure Up? • Why Measure? –

    Highlight bugs – Improve quality • Easier onboarding • Less reading, more writing • Testable
  5. 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
  6. 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
  7. 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
  8. 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)
  9. 14 Does Your Code Measure Up? • What To Measure?

    – Cyclomatic complexity – Duplicate code • Rule of 3
  10. 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
  11. 16 Does Your Code Measure Up? • What To Measure?

    – Cyclomatic complexity – Duplicate code – Long classes – Class Complexity • Less than 50
  12. 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
  13. 18 Does Your Code Measure Up? • What To Measure?

    – Cyclomatic complexity – Duplicate code – Long classes – Class complexity – Long methods – Unused variables
  14. 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
  15. 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
  16. 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
  17. 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
  18. 24 Does Your Code Measure Up? • Code “smells” –

    What are “smells”? • Indications of spoiled code nearby • Not conclusive • The “smell” is not bad
  19. 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
  20. 30 Does Your Code Measure Up? • PHP_Depend Result –

    Graphs and XML output ANDC AHH NOP NOC NOM LOC CYCLO NOM CALLS FANOUT 9891 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_Depend Low Average High
  21. 31 Does Your Code Measure Up? • Tools – PHPqatools.org

    • PHPLoc • PHP_Depend • PHP Copy/Paste Detector
  22. 33 Does Your Code Measure Up? • Tools – PHPqatools.org

    • PHPLoc • PHP_Depend • PHP Copy/Paste Detector • PHP Mess Detector – Codesize
  23. 35 Does Your Code Measure Up? • Tools – PHPqatools.org

    • PHPLoc • PHP_Depend • PHP Copy/Paste Detector • PHP Mess Detector – Codesize – Unused
  24. 36 Does Your Code Measure Up? • Tools – PHPqatools.org

    • PHPLoc • PHP_Depend • PHP Copy/Paste Detector • PHP Mess Detector – Codesize – Unused • PHP Dead Code Detector – Searches for code no longer used
  25. 37 Does Your Code Measure Up? • Tools – PHPqatools.org

    – PHPMetrics • http://phpmetrics.org
  26. 39 Does Your Code Measure Up? • Tools – PHPqatools.org

    – PHPMetrics – PHP_Codesniffer • Create rules/sniffs to ensure standards are followed • From CLI, IDE, or via SCM hooks
  27. 40 Does Your Code Measure Up? • Tools – PHPqatools.org

    – PHPMetrics – PHP_Codesniffer – Code Climate • Build from Git/Github repo – Open = free – Private = $ • GPA – like in high school, simple
  28. 42 Does Your Code Measure Up? • Tools – PHPqatools.org

    – PHPMetrics – PHP_Codesniffer – Code Climate – Zend Server and Z-Ray • Debugging, Code tracing, Error reporting
  29. 44 Does Your Code Measure Up? • Tools – PHPqatools.org

    – PHPMetrics – PHP_Codesniffer – Code Climate – Zend Server and Z-Ray – Apache Bench (apache2-utils in apt) • How much traffic can you handle?
  30. 46 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!
  31. 47 • Thank you! – http://phpqatools.org – http://phpmetrics.org – http://zend.com

    – http://codeclimate.com – Please rate at: https://joind.in/15185 Adam Culp http://www.geekyboy.com http://RunGeekRadio.com Twitter @adamculp