Debugging
Systematically finding why code doesn't do what you expect.
Also known as: debug, bug fixing, troubleshooting code
Debugging is finding out why your program doesn’t do what you expect, and then fixing the actual cause. It’s a skill, and it’s mostly method, not talent.
A reliable loop
- Reproduce it. If you can’t make it happen on demand, you can’t tell when you fixed it (see reproducing a bug).
- Read the error. The message and stack trace often point straight at it (reading error messages).
- Form a hypothesis: “I think
useris null here because the lookup failed.” - Test it: add a log, set a breakpoint, or write a small test. Check the actual value rather than assuming.
- Narrow it down. Cut the problem in half: is the data wrong before this function or after? (binary search debugging)
- Fix the cause, not the symptom. Wrapping a crash in
try/excepthides it. - Confirm and prevent. Check the original scenario now works, and add a test so it doesn’t come back.
Habits that save hours
- Change one thing at a time. Otherwise you won’t know which change mattered.
- Question assumptions. The bug is usually in something you were sure was fine.
- Check what changed recently:
git log,git diff, a deploy, a config edit. - Take a break or explain it to someone, even a rubber duck.
- Write down what you’ve tried, so you stop repeating it.
- Don’t guess-and-patch. Random edits until it works leave you with code you don’t understand.
Tools help (the debugger, logs, DevTools), but the thinking is the work: observe, hypothesize, test.