Debugging Habits That Outlast the Bug
Any single bug is forgettable. The habits you use to find it are not. A handful of steady routines make the difference between an hour of guessing and ten minutes of looking.
Make it fail on purpose first
Debugging begins with being able to make the problem happen whenever you want. A bug you can trigger with one command is nearly solved; a bug that appears "sometimes" can eat a whole day. Before changing any code, find the shortest sequence of steps that produces the failure, and write it down. If the failure needs a particular input, save that input to a file. This is your signal: when the same steps stop failing, you have fixed something, and until then you have not.
Read the whole message
When a program crashes, most languages print a stack trace: the chain of function calls that were in progress at the moment of failure. Beginners often look at the wall of text and read none of it. Read all of it, slowly, once. Find the line that names the type of error and the line that points into a file you wrote, rather than into a library. In most traces those two lines hold nearly everything you need, and the rest is the route the program took to get there.
State what you believe, then check it
A bug is a place where what you believe about the program and what the program does have separated. So write down, in one sentence, what you expect at a given line: "at this point the list has three items". Then check it, with a print statement or a debugger. If it is true, move further along. If it is false, move earlier. You are not looking for the bug directly; you are looking for the first place where your belief stops being true.
Explaining the code aloud, line by line, works for the same reason. The practice is known as rubber duck debugging, after the idea of explaining your code to a toy duck on the desk. Saying what each line is supposed to do forces you to notice the line that does something else.
Cut the problem in half
When you have no idea where the fault is, stop guessing and start halving. Put a check in the middle of the suspect code. If the data is still correct there, the fault is in the second half; if not, it is in the first. Repeat. The same idea works across time: if the program worked last week and fails today, test a version from halfway between, and keep halving until you find the change that broke it. Halving feels slow and is reliably fast, because each step removes half of what remains.
Change one thing at a time
The most common way to make a bug worse is to change three things, see a different result, and not know which change caused it. Make one change, run your failing steps again, and note what happened. If the change did not help, undo it before trying the next idea. Leaving failed attempts in the code means the program you are debugging is no longer the program that had the original bug.
Build the smallest version that still fails
If you are stuck, copy the failing code into a new file and delete everything that is not needed to produce the failure. Remove the database, the configuration, the user interface. Very often the bug reveals itself during the deleting, because you remove a piece and the failure disappears, which tells you where it lived. And if it does not reveal itself, you now have a short example you can show to someone else, which is the only kind of example other people have time to read.
- Reproduce before you repair.
- Read the full error, and find the line in your own file.
- Write down what you expect, then check it.
- Halve the search space instead of guessing.
- One change, one run, one note.
Keep a short log
For anything that takes longer than a few minutes, keep a running note of what you tried and what you saw. It stops you from trying the same thing twice, and it turns into a clear explanation when you ask for help. It also protects you from the tired-evening trap of believing you already checked something that you only meant to check.
After the fix
When the failing steps finally pass, do two more things. First, make sure you can say in one sentence why the bug happened. If you cannot, you may have hidden it rather than fixed it. Second, if the project has tests, add one that would have caught this. The bug you just fixed is the one most likely to come back.
Working rule: surprise is information. When the program does something you did not expect, do not explain it away. That moment is usually the nearest you will get to the cause.
Last revised 2026-11-20 · Wendell Arkwright