Software bugs are inevitable; some 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! The mental fatigue and wasted time can be avoided by using strategies like identifying the most-appropriate tool, taking a logical & objective approach, challenging assumptions, listening to variables, isolating the code path, 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 quickly and effectively.
1. Photo by Joseph B // cc by-nc-nd 2.0 // https://flic.kr/p/7GAMBe
Debugging Effectively
Colin O’Dell
2. Photo by Joseph B // cc by-nc-nd 2.0 // https://flic.kr/p/7GAMBe
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
• PHP 7 Upgrade Guide e-book
• @colinodell / www.colinodell.com
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
8. Photo by florianric // cc by 2.0 // https://flic.kr/p/c4QJzC
Planning
Testing &
Debugging
Coding
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. Process is the foundation of effective debugging
Process
Photo by Matthew Paulson // cc by-nc-nd 2.0 // https://flic.kr/p/di35xa
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. Photo by Matthew Paulson // cc by-nc-nd 2.0 // https://flic.kr/p/di35xa
Develop a “sixth sense”
“Sixth Sense”
Experience
Process
13. Junior Developers
• Try the “usual” steps
• Clear the cache
• Re-install dependencies
• 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. XY Problem
Photo by Yoel Ben-Avraham, with changes // cc by--nd 2.0 // https://flic.kr/p/6pmtQL
•I want to solve problem X
•How do I solve X?
15. XY Problem
Photo by Yoel Ben-Avraham, with changes // cc by--nd 2.0 // https://flic.kr/p/6pmtQL
•I want to solve problem X
•How do I solve X?
•Solution Y might work
•How can I do Y?
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. “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
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. Assume your
code is the
problem
Photo by Sergio Flores Rosales // cc by-nc-nd 2.0 // https://flic.kr/p/5UHkaW
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. 1. Gather information
• Expected behavior vs.
actual behavior
• Error messages
• Stack traces
Photo from youmustdesireit.wordpress.com
•Screenshots
•Browser & OS
•Date & time
•Log entries
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. 3. Identify the Culprit
Photo by U.S. Navy // cc 2.0 // https://flic.kr/p/n6Wgks
• Be methodical
• Make no assumptions
• Understand the bug
25. 4. Fix it & Re-test
• 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
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. 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. 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
35. 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
36. 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
37. 1. Trace backwards
•Use a debugger
•Identify source of error
•Establish context
•Work backwards
a()
b()
c()
d()
Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw
38. 1. Trace backwards
•Use a debugger
•Identify source of error
•Establish context
•Work backwards
a()
b()
c()
d()
Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw
39. 1. Trace backwards
•Use a debugger
•Identify source of error
•Establish context
•Work backwards
a()
b()
c()
d()
Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw
40. 1. Trace backwards
•Use a debugger
•Identify source of error
•Establish context
•Work backwards
a()
b()
c()
d()
Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw
41. 1. Trace backwards
•Use a debugger
•Identify source of error
•Establish context
•Work backwards
a()
b()
c()
d()
Photo by Shawn Harquail // cc by-nc 2.0 // https://flic.kr/p/npxFVw
42. 2. Divide & Conquer
• Identify different code sections
• Set breakpoints at the boundaries
• Isolate issue to one particular area
• Focus efforts on that area
56. New Relic
Photo by Alan Stark // cc by-sa 2.0 // https://flic.kr/p/dn3qjx
57. git bisect
v1.7 ? ? ? ? ? ? ? HEAD
Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm
git bisect start
git bisect bad
58. git bisect
v1.7 ? ? ? ? ? ? ? HEAD
Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm
git bisect good
59. git bisect
v1.7 ? ? ? ? ? ? ? HEAD
Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm
git bisect bad
60. git bisect
v1.7 ? ? ? BAD ? ? ? HEAD
Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm
git bisect bad
61. git bisect
v1.7 ? ? ? BAD BAD BAD BAD HEAD
Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm
git bisect good
62. git bisect
v1.7 ? GOOD ? BAD BAD BAD BAD HEAD
Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm
git bisect good
63. git bisect
v1.7 GOOD GOOD ? BAD BAD BAD BAD HEAD
Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm
64. git bisect
v1.7 GOOD GOOD
X BAD BAD BAD BAD HEAD
Photo by unbekannt270 // cc by 2.0 // https://flic.kr/p/oHReqm
abcd123 is the first bad commit
65. Photo by Joseph B // cc by-nc-nd 2.0 // https://flic.kr/p/7GAMBe
netcat
66. Photo by Joseph B // cc by-nc-nd 2.0 // https://flic.kr/p/7GAMBe
curl
67. 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
68. 5. Take a break
Photo by Kristina Alexadnerson // cc by-nc-nd 2.0 // https://flic.kr/p/cFEcDC
• Clear your mind; start fresh
• Forget invalid assumptions
• Recharge your batteries
• Let your subconscious work on it
69. 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
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
BE NICE / CONSTRUCTIVE
TODO ADD NOTES
When you give up on the proper approach
Apply workaround instead
Improper solutions will cause issues later
NEXT - EXAMPLE
First Magento client
Ability to generate PDFs
Base85-encoded
Don’t be clever
TRANSITION NEEDED!!
Magical thinking
Code is not a magic black box
Article
sneaky
Bugs are almost always based on faulty assumptions
If you build on faulty assumptions
95% in your code
If helping others, assume their code is problem
Challenge their assumptions
Take with grain of salt
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
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
Stack trace
Grep for error message
Set breakpoint
For example
Examine variables
How did I get here? – call stack
Debugger is critical
Not only tool
Tools are not a substitute for thinking
Phpunit
Phpspec
behat
Drop-in replacement for var_dump(), print_r() and debug_backtrace().
Symfony VarDumper component
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