Systematic Debugging
Use a structured approach to find and fix bugs efficiently.
When to Use
Use this skill when:
- Debugging complex issues
- Investigating bugs with unclear causes
- Reproducing issues is difficult
- Need to track debugging progress
Workflow
Step 1: REPRODUCE - Can You Recreate It?
- Get exact steps to reproduce
- Confirm the bug exists
- Document reproduction steps
- Identify conditions (environment, data, state)
Checklist:
- Bug is reproducible
- Reproduction steps are documented
- Environment is noted
- Relevant data/state is identified
Step 2: ISOLATE - Find the Smallest Case
- Remove unrelated factors
- Simplify input data
- Isolate to single component/module
- Identify minimal reproduction case
Checklist:
- Minimal reproduction case found
- Isolated to specific code
- Unrelated factors removed
- Input data is simplified
Step 3: IDENTIFY - Find Root Cause
- Add logging at key points
- Use debugger to trace execution
- Check edge cases and assumptions
- Verify actual vs. expected behavior
Checklist:
- Execution path is understood
- Actual behavior is documented
- Expected behavior is documented
- Root cause is identified
Step 4: VERIFY - Fix and Confirm
- Implement fix
- Reproduce to verify bug is gone
- Test edge cases
- Check for regressions
Checklist:
- Bug no longer reproducible
- Edge cases are tested
- No regressions introduced
- Fix is minimal and targeted
Debugging Techniques
Logging
console.log('DEBUG: value =', value);
console.log('DEBUG: state =', JSON.stringify(state));
Debugger
// Add breakpoints
debugger;
// Use Chrome DevTools or VS Code debugger
Binary Search
If the bug is in a large codebase:
- Narrow down to subsystem
- Test half, narrow to quarter
- Repeat until isolated
Rubber Ducking
Explain the problem step-by-step to someone (or a rubber duck):
- Forces structured thinking
- Often reveals the issue
- No actual conversation needed
Anti-Patterns
- cancelRandomly changing code without understanding
- cancelAdding fixes without verifying they work
- cancelSkipping the isolation step
- cancelTesting only the happy path
- cancelNot checking for regressions
Example
// REPRODUCE
function calculateDiscount(price: number, quantity: number): number {
return price * 0.9; // Bug: doesn't use quantity
}
// ISOLATE
test('discount with quantity > 10', () => {
expect(calculateDiscount(100, 15)).toBe(850); // Fails
});
// IDENTIFY
// Root cause: quantity parameter is ignored
// VERIFY
function calculateDiscount(price: number, quantity: number): number {
const discount = quantity > 10 ? 0.85 : 0.9;
return price * discount;
}
Verification
npm run test # Bug test passes
npm run lint # No new lint errors
npm run typecheck # No type errors
Related Skills
- Error Handling - Add proper error handling
- Code Review - Review for similar bugs