Successfully reported this slideshow.
Your SlideShare is downloading. ×

Debugging Effectively - SymfonyLive San Francisco 2015

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 61 Ad

Debugging Effectively - SymfonyLive San Francisco 2015

Download to read offline

Software bugs are inevitable - it doesn't matter how experienced you are or how well you follow best-practices. Some bugs are especially difficult to track down, causing you to waste countless hours before throwing your hands up in defeat. It doesn't have to be this way though! The mental fatigue and wasted time can be avoided by implementing effective debugging strategies like: identifying & using the most-appropriate tool; taking a logical & objective approach; challenging assumptions; listening to variables; isolating the code path; rubber ducking; and reinforcing code with automated tests. Attendees will learn how to combine these techniques with the right mindset and attitude in order to debug their code effectively.

Software bugs are inevitable - it doesn't matter how experienced you are or how well you follow best-practices. Some bugs are especially difficult to track down, causing you to waste countless hours before throwing your hands up in defeat. It doesn't have to be this way though! The mental fatigue and wasted time can be avoided by implementing effective debugging strategies like: identifying & using the most-appropriate tool; taking a logical & objective approach; challenging assumptions; listening to variables; isolating the code path; rubber ducking; and reinforcing code with automated tests. Attendees will learn how to combine these techniques with the right mindset and attitude in order to debug their code effectively.

Advertisement
Advertisement

More Related Content

Similar to Debugging Effectively - SymfonyLive San Francisco 2015 (20)

More from Colin O'Dell (20)

Advertisement
Advertisement

Debugging Effectively - SymfonyLive San Francisco 2015

  1. 1. Photo by Joseph B // cc by-nc-nd 2.0 // https://flic.kr/p/7GAMBe Debugging Effectively Colin O’Dell
  2. 2. Colin O’Dell • Lead Web Developer at Unleashed Technologies • PHP developer since 2002 • Certifications: Symfony and Magento • PHP League Member • league/commonmark • league/html-to-markdown • What’s New in PHP 7 e-book • @colinodell / www.colinodell.com Photo by Joseph B // cc by-nc-nd 2.0 // https://flic.kr/p/7GAMBe
  3. 3. Overview I. Importance of debugging II. Debugging process III. Tools & Techniques IV. Q&A Photo by Roman Boed // cc by 2.0 // https://flic.kr/p/ngwcf1
  4. 4. Debugging is... Photo by Ewan Cross // cc by-nc-nd 2.0 // https://flic.kr/p/7MCt3m _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ (adjective)
  5. 5. Debugging is... important Photo by Ewan Cross // cc by-nc-nd 2.0 // https://flic.kr/p/7MCt3m
  6. 6. Debugging is... the single most important skill in programming. Photo by Ewan Cross // cc by-nc-nd 2.0 // https://flic.kr/p/7MCt3m
  7. 7. PlanningCoding Photo by florianric // cc by 2.0 // https://flic.kr/p/c4QJzC
  8. 8. Planning Testing & Debugging Coding Photo by florianric // cc by 2.0 // https://flic.kr/p/c4QJzC
  9. 9. Debugging is the process of finding and resolving bugs or defects that prevent correct operation of computer software or a system. – Wikipedia Photo by Stephen Dyrgas // cc by-nc-nd 2.0 // https://flic.kr/p/3eTfWU
  10. 10. Process is the foundation of effective debugging Process Photo by Matthew Paulson // cc by-nc-nd 2.0 // https://flic.kr/p/di35xa
  11. 11. Photo by Matthew Paulson // cc by-nc-nd 2.0 // https://flic.kr/p/di35xa Gain experience with tools and code Experience Process
  12. 12. Photo by Matthew Paulson // cc by-nc-nd 2.0 // https://flic.kr/p/di35xa Develop a “sixth sense” “Sixth Sense” Experience Process
  13. 13. Junior Developers • Try the “usual” steps • app/console cache:clear • composer install • chmod –R 777 * • Google the error • Try every solution • Ask somebody else • Co-worker • StackOverflow post • Give up Photo by Yoel Ben-Avraham, with changes // cc by--nd 2.0 // https://flic.kr/p/6pmtQL
  14. 14. XY Problem •I want to solve problem X •How do I solve X? Photo by Yoel Ben-Avraham, with changes // cc by--nd 2.0 // https://flic.kr/p/6pmtQL
  15. 15. XY Problem •I want to solve problem X •How do I solve X? •Solution Y might work •How can I do Y? Photo by Yoel Ben-Avraham, with changes // cc by--nd 2.0 // https://flic.kr/p/6pmtQL
  16. 16. Photo by quattrostagioni // cc by 2.0 // https://flic.kr/p/aGjVq8 1.Don’t parse HTML with regex 2.Solve problems the right way
  17. 17. “I don’t know why” “For some reason” “Doesn’t make sense” Photo by Stanley Yuu // cc by-nc-nd 2.0 // https://flic.kr/p/pMnfNu
  18. 18. Bugs are logical Photo by Photofest
  19. 19. “The bug is not moving around in your code, trying to trick or evade you. It is just siting in one place, doing the wrong thing in the same way every time.” – Nick Parlante, Debugging Zen Photo by GrahamC57 // cc by-nc-nd 2.0 // https://flic.kr/p/cwJi9Q
  20. 20. Assume your code is the problem Photo by Sergio Flores Rosales // cc by-nc-nd 2.0 // https://flic.kr/p/5UHkaW
  21. 21. Systematic Approach 1. Gather information 2. Replicate the issue 3. Identify the culprit 4. Fix it & re-test 5. Mitigate future occurrences Photo from EduSpiral // cc by-nc-nd 3.0
  22. 22. •Expected behavior vs. actual behavior •Error messages •Stack traces Photo from youmustdesireit.wordpress.com • Screenshots • Browser & OS • Date & time • Log entries 1. Gather Information
  23. 23. 2. Replicate the Issue Be able to replicate with 100% certainty Photo by Nick Royer // cc by-sa // https://flic.kr/p/d41ASC
  24. 24. • Be methodical • Make no assumptions • Understand the bug Photo by U.S. Navy // cc 2.0 // https://flic.kr/p/n6Wgks 3. Identify the Culprit
  25. 25. • Attempt to replicate again • Avoid XY problem • No temporary workarounds! • Add technical debt • May introduce other issues • Never get replaced with true solutions Photo by Jeff Eaton // cc by-sa 2.0 // https://flic.kr/p/b33rSx 4. Fix & Re-test
  26. 26. 5. Mitigate Future Occurrences •Add an automated test •Share your new knowledge • Project documentation • Blog post • StackOverflow •Submit patch upstream Photo by marcokalmann // cc by-nc-nd 2.0 // https://flic.kr/p/4CqLMQ
  27. 27. Recap 1. Gather information 2. Replicate the issue 3. Identify the culprit 4. Fix it & re-test 5. Mitigate future occurrences Photo from EduSpiral // cc by-nc-nd 3.0
  28. 28. Long-Term Results • Gain experience • Learn how the system works • Build heuristics • Boost confidence Photo by Glenn Beltz // cc by 2.0 // https://flic.kr/p/i7Csdx
  29. 29. Tools &Techniques Photo by Alan // cc by-nc-sa 2.0 // https://flic.kr/p/9azLii
  30. 30. Two essential tools • Integrated development environment (IDE) • Interactive debugger Photo by florianric // cc by 2.0 // https://flic.kr/p/c4QJzC
  31. 31. Integrated Development Environment • Minimum features: • Syntax highlighting • Auto-completion • Fast code navigation • Debugger Photo by florianric // cc by 2.0 // https://flic.kr/p/c4QJzC
  32. 32. Interactive Debugger • Pause code execution • Breakpoints • Conditional breakpoints • Step through execution • Examine variables • Explore call stack Photo by florianric // cc by 2.0 // https://flic.kr/p/c4QJzC
  33. 33. Photo by florianric // cc by 2.0 // https://flic.kr/p/c4QJzC
  34. 34. Techniques 1. Trace backwards from known issue 2. Divide & conquer 3. Use tools 4. Get help 5. Take a break Photo by Massmo Relsig // cc by-nc-nd 2.0 // https://flic.kr/p/pFrJCe
  35. 35. 1. Trace backwards •Use a debugger •Identify source of error •Establish context •Work backwards Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw
  36. 36. 1. Trace backwards a() b() c() d() Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw
  37. 37. 1. Trace backwards a() b() c() d() Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw
  38. 38. 2. Divide & Conquer • Identify different code sections • Set breakpoints at the boundaries • Isolate issue to one particular area • Focus efforts on that area
  39. 39. ✓ X
  40. 40. 3. Use tools • VarDumper • Debug toolbars • Console utility • Profilers • git bisect Photo by florianric // cc by 2.0 // https://flic.kr/p/c4QJzC •netcat •curl •strace •etc.
  41. 41. VarDumper Photo by Joseph B // cc by-nc-nd 2.0 // https://flic.kr/p/7GAMBe Twig: {{ dump(foo) }} {% dump foo %} PHP: dump($somevar);
  42. 42. Photo by Joseph B // cc by-nc-nd 2.0 // https://flic.kr/p/7GAMBe Debug toolbars
  43. 43. Photo by Joseph B // cc by-nc-nd 2.0 // https://flic.kr/p/7GAMBe Console Utility Symfony: app/console Drupal: drush Magento: n98-magerun.phar Laravel: artisan
  44. 44. Performance Profiling Identify slowness: • Bottlenecks • Resource hogs • Inefficient code Photo by Alan Stark // cc by-sa 2.0 // https://flic.kr/p/dn3qjx Tools: • Blackfire (freemium) • New Relic (freemium) • xhprof (open-source)
  45. 45. git bisect v1.7 ? ? ? ? ? HEAD Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm
  46. 46. git bisect v1.7 ? ? BAD ? ? HEAD Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm
  47. 47. git bisect v1.7 ? ? BAD BAD BAD HEAD Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm
  48. 48. git bisect v1.7 GOOD ? BAD BAD BAD HEAD Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm
  49. 49. git bisect v1.7 GOOD X BAD BAD BAD HEAD Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm abcd123 is the first bad commit
  50. 50. netcat Photo by Joseph B // cc by-nc-nd 2.0 // https://flic.kr/p/7GAMBe
  51. 51. 4. Get help •RTFM / RTFD •Project forums or issue queue •StackOverflow, IRC, etc. •Ask a colleague • Expert in that area • Senior developer •Rubber ducking Photo by Hiromitsu Morimoto // cc by-sa 2.0 // https://flic.kr/p/6Vzzaa
  52. 52. 5. Take a break • Clear your mind; start fresh • Forget invalid assumptions • Recharge your batteries • Let your subconscious work on it Photo by Kristina Alexadnerson // cc by-nc-nd 2.0 // https://flic.kr/p/cFEcDC
  53. 53. Four things to walk away with 1. Computers aren’t random, and neither are bugs 2. Persistence will always pay off 3. Don’t be afraid to dive deep 4. Don’t make assumptions or take things for granted Photo by Kristina Alexadnerson // cc by-nc-nd 2.0 // https://flic.kr/p/dvEpfY
  54. 54. Questions? Photo by Marcello Maria Perongini // cc by-nc-nd 2.0 // https://flic.kr/p/6KDtm
  55. 55. Learn More • https://web.duke.edu/cps001/notes/Debugging.pdf • http://www.fiveminutegeekshow.com/20 • http://blog.codeunion.io/2014/09/03/teaching-novices-how-to- debug-code/ • https://www.jetbrains.com/phpstorm/help/debugging.html • http://symfony.com/doc/current/components/var_dumper/introducti on.html • http://www.sitepoint.com/debugging-git-blame-bisect/ • http://unix.stackexchange.com/a/50099/80744 • http://codeception.com/docs/01-Introduction • http://chadfowler.com/blog/2014/01/26/the-magic-of-strace/ • http://c2.com/cgi/wiki?RubberDucking Photo by Samantha Marx // cc by 2.0 // https://flic.kr/p/8KrU1R
  56. 56. Thanks! Feedback? • https://joind.in/16056 • @colinodell Photo by Steve Rotman // cc by-nc-nd 2.0 // https://flic.kr/p/xiBK

Editor's Notes

  • PHP developer 13 years
    JS, Java, C#
  • What are some words

    Hard
    Time-consuming
    Annoying
  • Take this one step further

    Why is that?
  • How much time coding?

    2/3rds
  • According to the Mythical Man Month, 1/6th
    ½ is testing/debugging

    How much - actually writing code vs getting it working

    Area where it’s worthwhile to become efficient
  • READ SLOWLY

    Shouldn’t be done haphazardly
  • Immediate understanding of what type of problem, where it might be
    Like intuition, but not
    Understanding without proof, but based on logic and experience
    --
    Focus on the process
    Experience and “sixth sense” will come with time

    NEXT: Debugging process
  • And intermediate developers
  • When you give up on the proper approach
    Apply workaround instead

    Improper solutions will cause issues later

    NEXT - EXAMPLE
  • BE FAST
    Base85-encoded

    Don’t be clever
  • Magical thinking

    Code is not a magic black box
  • BE FAST
  • sneaky
  • Bugs are almost always based on faulty assumptions
    If you build on faulty assumptions

    95% in your code
  • 15 MIN IN / 25 MIN REMAINING
    OR SPEED UP!
  • Define the symptoms

    Collect everything you can
  • Automated tests preferred, but manual is okay too

    LEGOS
  • Understand the fundamental nature
    Exactly why
    We’ll get into the tools & techniques
  • 25 MIN IN / 15 MIN REMAINING
  • Other nice-to-haves:
    File syncing
    Git integration
    Run tools

    PhpStorm, Sublime Text, vim

    NOT Notepad++ or Dreamweaver
  • Debuggers are awesome tool

    Better than primitive approaches
    Examine in real time
    Xdebug
    Chrome developer tools
  • Advanced breakpoints
    Grouping
    Conditions
  • Stack trace
    Grep for error message
    Set breakpoint
    For example
  • Examine variables
    How did I get here? – call stack
  • Don’t have enough time to cover all of these
  • Works in Twig, PHP, CLI, etc.
    Better than vardump
    Handles:
    - Circular references
    - Deeply-nested objects
    Public vs protected vs private
    Custom casters to change representation
  • Q: How many people here are using Git? Familiar with Git bisect?
    Built-in tool
    Assists with running a binary search across your commits
  • Scales logarithmically – runs in O(log N)
    7 commits - 4 tests
    10x: 70 commits – 8 tests
    100x: 700 commits - 16 tests
  • Fifth technique
  • 1. There is ALWAYS a logical explanation
    3. Great learning opportunity
    4. Challenge everything
  • ?

    Other Questions? Talk afterwards or contact me
  • Thank you guys
    I’d appreciate feedback

×