Debugging: The Science of Getting Unstuck
We have all been there.
You have been staring at the same function for 4 hours.
You have console.log everywhere.
It should work. logic says it works.
But it crashes.
Debugging is not a "Dark Art." It is a systematic process. The moment you start guessing ("Maybe if I change this variable name?"), you have lost.
Here is the Senior Engineer's guide to getting unstuck.
1. The Rubber Duck Method
This sounds stupid. It works. Grab a physical object (a rubber duck, a mug, a pen). Explain the code to the duck, line by line. Out loud.
"Okay Duck, so line 10 fetches the user. Line 11 checks if the user is null. Line 12... wait." "Wait." That is the moment. By forcing your brain to slow down and articulate the logic, you usually find the flaw yourself.
2. Divide and Conquer (Binary Search)
If you have a 1000-line file and it's crashing, don't read all 1000 lines. Find the middle.
- Put a log at Line 500.
- Does it reach Line 500?
- Yes: The bug is in the bottom half (500-1000).
- No: The bug is in the top half (0-500).
Repeat. 500 -> 250 -> 125 -> 60 -> 30. In 10 steps, you isolate the exact line.
3. Verify Your Assumptions
The worst bugs happen because you believe something that is false.
- "The API always returns JSON." (Does it? Did you check the 500 error page?)
- "The variable
useris never null." (Is it?) - "The loop runs 10 times." (Does it?)
Assert everything.
Don't assumes. Verify.
Log the typeof variable. Copy the API response into a JSON validator.
4. The 15-Minute Rule
If you are stuck for 15 minutes, you must switch tactics.
- Google it: Paste the exact error message.
- Read the Docs: Actually read the library documentation, don't just guess methods.
- Take a Walk: Your brain solves problems in the background. Go make coffee. Access the "Diffused Mode" of thinking.
5. Creating a Reproduction (Minimal Repro)
If you can't reproduce it, you can't fix it. Create a minimal separate file.
- Remove the framework.
- Remove the database.
- Just run the failing function with hardcoded inputs.
If it works in isolation, the bug is in the environment/integration. If it fails in isolation, you have a small, testable case to fix.
Summary
Debugging is removing the impossible until only the truth remains.
- Talk to the Duck.
- Bisect the Code.
- Verify Assumptions.
- Isolate the Problem.
And please, remove your console.log before you push.
