Debugging Strategies That Work
Most developers debug by staring at code and hoping the bug reveals itself. Here is a more systematic approach.
Reproduce First, Fix Second
Before touching any code, reproduce the bug reliably. Write down the exact steps. If you cannot reproduce it, you cannot verify the fix. Intermittent bugs are the hardest, but they always have a trigger. Find it.
Read the Error Message
This sounds obvious, but many developers skip the error message and jump straight to Stack Overflow. Read the full error message, including the stack trace. It often tells you exactly what went wrong and where.
Binary Search Your Code
Comment out half the code. Does the bug persist? If yes, the problem is in the remaining half. Comment out half of that. Repeat until you isolate the problematic section. This is faster than reading every line.
Rubber Duck Debugging
Explain the problem out loud to an inanimate object, a rubber duck, a plant, a coffee mug. The act of articulating the problem forces you to think through your assumptions. You will be surprised how often the answer comes while explaining.
Check Your Assumptions
Print or log every variable you assume has a certain value. The bug is almost always in a wrong assumption. The variable you are sure is a number might be a string. The array you think has five items might be empty.
Git Bisect for Regressions
If something worked before and broke recently, use git bisect. It performs a binary search through your commit history to find the exact commit that introduced the bug. Mark commits as good or bad and Git does the rest.