How to Accidentally Fix a Bug
A completely serious guide to doing something without meaning to. Results not guaranteed. Accidents never are.
Short answer
Explain the bug out loud to a colleague, a rubber duck or your cat. Halfway through the sentence you will hear yourself say the answer. This is called rubber-duck debugging and it works embarrassingly often.
The step-by-step accident
- Restart it. You knew this was step one. Everyone knows. Nobody admits it.
- Add a print statement to look at the problem. The bug will vanish the moment you look at it. Leave the print statement there forever, out of respect.
- Go for a walk. Your brain fixes bugs in the background while you stare at a tree. The tree takes no credit.
- Delete the code you did not understand. Sometimes the fix is less code. Sometimes it is a new, more exciting bug.
People also ask
Why do bugs disappear when you add logging?
Timing changes. Logging slows code down just enough to hide race conditions — so the bug is still there, just shy.