Systematic Debugging
Diagnose and fix errors using scientific method with disciplined root cause analysis.
The Iron Law
NO FIXES WITHOUT ROOT CAUSE INVESTIGATION FIRST
If you haven't completed Phase 1, you cannot propose fixes. Symptom fixes are failure.
When to Use
Use for ANY technical issue:
- Test failures
- Bugs in production
- Unexpected behavior
- "It doesn't work" problems
- Build/compilation failures
- Performance problems
Use ESPECIALLY when:
- Under time pressure (emergencies make guessing tempting)
- "Just one quick fix" seems obvious
- You've already tried multiple fixes
- Previous fix didn't work
Don't skip when:
- Issue seems simple (simple bugs have root causes too)
- You're in a hurry (systematic is faster than thrashing)
The Four Phases
Complete each phase before proceeding to the next.
Phase 1: Root Cause Investigation
BEFORE attempting ANY fix:
1. Read Error Messages Carefully
- Don't skip past errors or warnings
- They often contain the exact solution
- Read stack traces completely
- Note line numbers, file paths, error codes
2. Reproduce Consistently
- Can you trigger it reliably?
- What are the exact steps?
- Does it happen every time?
- If not reproducible → gather more data, don't guess
3. Check Recent Changes
- What changed that could cause this?
git diff, recent commits
- New dependencies, config changes
- Environmental differences
4. Trace Data Flow
When error is deep in call stack:
- Where does the bad value originate?
- What called this with the bad value?
- Keep tracing up until you find the source
- Fix at source, not at symptom
See root-cause-tracing.md for the complete backward tracing technique.
5. Gather Evidence in Multi-Component Systems
When system has multiple components (API → service → database):
For EACH component boundary:
- Log what data enters component
- Log what data exits component
- Verify environment/config propagation
Run once to gather evidence showing WHERE it breaks
THEN analyze evidence to identify failing component
THEN investigate that specific component
Phase 2: Pattern Analysis
Find the pattern before fixing:
- Find Working Examples - Locate similar working code in same codebase
- Compare Against References - Read reference implementation COMPLETELY, don't skim
- Identify Differences - List every difference between working and broken
- Understand Dependencies - What settings, config, environment does it need?
Phase 3: Hypothesis and Testing
Scientific method:
- Form Single Hypothesis - State clearly: "I think X is the root cause because Y"
- Test Minimally - Make the SMALLEST possible change to test hypothesis
- One Variable at a Time - Don't fix multiple things at once
- Verify Before Continuing
- Did it work? → Phase 4
- Didn't work? → Form NEW hypothesis, don't add more fixes
Phase 4: Implementation
Fix the root cause, not the symptom:
- Create Failing Test Case - Simplest possible reproduction, automated if possible
- Implement Single Fix - ONE change at a time, no "while I'm here" improvements
- Verify Fix - Test passes? No other tests broken? Issue actually resolved?
- If Fix Doesn't Work - STOP. If ≥3 fixes failed, question the architecture
Red Flags - STOP and Return to Phase 1
If you catch yourself thinking:
- "Quick fix for now, investigate later"
- "Just try changing X and see if it works"
- "Add multiple changes, run tests"
- "It's probably X, let me fix that"
- "I don't fully understand but this might work"
- Proposing solutions before tracing data flow
- "One more fix attempt" (when already tried 2+)
ALL of these mean: STOP. Return to Phase 1.
Common Rationalizations
| Excuse |
Reality |
| "Issue is simple, don't need process" |
Simple issues have root causes too. Process is fast for simple bugs. |
| "Emergency, no time for process" |
Systematic debugging is FASTER than guess-and-check thrashing. |
| "Just try this first, then investigate" |
First fix sets the pattern. Do it right from the start. |
| "Multiple fixes at once saves time" |
Can't isolate what worked. Causes new bugs. |
| "I see the problem, let me fix it" |
Seeing symptoms ≠ understanding root cause. |
Quick Reference
| Phase |
Key Activities |
Success Criteria |
| 1. Root Cause |
Read errors, reproduce, check changes, trace data |
Understand WHAT and WHY |
| 2. Pattern |
Find working examples, compare |
Identify differences |
| 3. Hypothesis |
Form theory, test minimally |
Confirmed or new hypothesis |
| 4. Implementation |
Create test, fix, verify |
Bug resolved, tests pass |
Debugging Commands
# Git bisect for regressions
git bisect start
git bisect bad HEAD
git bisect good <last-known-good-commit>
# Find recent changes to a file
git log --oneline -10 path/to/file
# Search for error message
grep -r "error message" --include="*.ts"
Common Fixes by Error Type
| Error Pattern |
Likely Root Cause |
Investigation |
Cannot read property 'x' of undefined |
Missing data, async timing |
Trace where null originates |
Module not found |
Path issues, missing export |
Check exact path, case sensitivity |
CORS error |
Backend config |
Check Network tab, test with curl |
Timeout |
Slow operation, connection issue |
Profile, check network |
| Works locally, fails in CI |
Environment diff |
Compare env vars, versions, permissions |
| Intermittent failures |
Race conditions, timing |
Look for shared state, async issues |
Real-World Impact
From debugging sessions:
- Systematic approach: 15-30 minutes to fix
- Random fixes approach: 2-3 hours of thrashing
- First-time fix rate: 95% vs 40%
See Also
1---2name: debugging3description: Use when encountering any bug, test failure, error, or unexpected behavior - before proposing fixes. Requires root cause investigation first.4---56# Systematic Debugging78Diagnose and fix errors using scientific method with disciplined root cause analysis.910## The Iron Law1112```13NO FIXES WITHOUT ROOT CAUSE INVESTIGATION FIRST14```1516If you haven't completed Phase 1, you cannot propose fixes. Symptom fixes are failure.1718## When to Use1920**Use for ANY technical issue:**21- Test failures22- Bugs in production23- Unexpected behavior24- "It doesn't work" problems25- Build/compilation failures26- Performance problems2728**Use ESPECIALLY when:**29- Under time pressure (emergencies make guessing tempting)30- "Just one quick fix" seems obvious31- You've already tried multiple fixes32- Previous fix didn't work3334**Don't skip when:**35- Issue seems simple (simple bugs have root causes too)36- You're in a hurry (systematic is faster than thrashing)3738## The Four Phases3940Complete each phase before proceeding to the next.4142### Phase 1: Root Cause Investigation4344**BEFORE attempting ANY fix:**4546#### 1. Read Error Messages Carefully47- Don't skip past errors or warnings48- They often contain the exact solution49- Read stack traces completely50- Note line numbers, file paths, error codes5152#### 2. Reproduce Consistently53- Can you trigger it reliably?54- What are the exact steps?55- Does it happen every time?56- If not reproducible → gather more data, don't guess5758#### 3. Check Recent Changes59- What changed that could cause this?60- `git diff`, recent commits61- New dependencies, config changes62- Environmental differences6364#### 4. Trace Data Flow65When error is deep in call stack:66- Where does the bad value originate?67- What called this with the bad value?68- Keep tracing up until you find the source69- **Fix at source, not at symptom**7071See [root-cause-tracing.md](./root-cause-tracing.md) for the complete backward tracing technique.7273#### 5. Gather Evidence in Multi-Component Systems74When system has multiple components (API → service → database):7576```77For EACH component boundary:78 - Log what data enters component79 - Log what data exits component80 - Verify environment/config propagation8182Run once to gather evidence showing WHERE it breaks83THEN analyze evidence to identify failing component84THEN investigate that specific component85```8687### Phase 2: Pattern Analysis8889**Find the pattern before fixing:**90911. **Find Working Examples** - Locate similar working code in same codebase922. **Compare Against References** - Read reference implementation COMPLETELY, don't skim933. **Identify Differences** - List every difference between working and broken944. **Understand Dependencies** - What settings, config, environment does it need?9596### Phase 3: Hypothesis and Testing9798**Scientific method:**991001. **Form Single Hypothesis** - State clearly: "I think X is the root cause because Y"1012. **Test Minimally** - Make the SMALLEST possible change to test hypothesis1023. **One Variable at a Time** - Don't fix multiple things at once1034. **Verify Before Continuing**104 - Did it work? → Phase 4105 - Didn't work? → Form NEW hypothesis, don't add more fixes106107### Phase 4: Implementation108109**Fix the root cause, not the symptom:**1101111. **Create Failing Test Case** - Simplest possible reproduction, automated if possible1122. **Implement Single Fix** - ONE change at a time, no "while I'm here" improvements1133. **Verify Fix** - Test passes? No other tests broken? Issue actually resolved?1144. **If Fix Doesn't Work** - STOP. If ≥3 fixes failed, question the architecture115116## Red Flags - STOP and Return to Phase 1117118If you catch yourself thinking:119- "Quick fix for now, investigate later"120- "Just try changing X and see if it works"121- "Add multiple changes, run tests"122- "It's probably X, let me fix that"123- "I don't fully understand but this might work"124- Proposing solutions before tracing data flow125- "One more fix attempt" (when already tried 2+)126127**ALL of these mean: STOP. Return to Phase 1.**128129## Common Rationalizations130131| Excuse | Reality |132|--------|---------|133| "Issue is simple, don't need process" | Simple issues have root causes too. Process is fast for simple bugs. |134| "Emergency, no time for process" | Systematic debugging is FASTER than guess-and-check thrashing. |135| "Just try this first, then investigate" | First fix sets the pattern. Do it right from the start. |136| "Multiple fixes at once saves time" | Can't isolate what worked. Causes new bugs. |137| "I see the problem, let me fix it" | Seeing symptoms ≠ understanding root cause. |138139## Quick Reference140141| Phase | Key Activities | Success Criteria |142|-------|---------------|------------------|143| **1. Root Cause** | Read errors, reproduce, check changes, trace data | Understand WHAT and WHY |144| **2. Pattern** | Find working examples, compare | Identify differences |145| **3. Hypothesis** | Form theory, test minimally | Confirmed or new hypothesis |146| **4. Implementation** | Create test, fix, verify | Bug resolved, tests pass |147148## Debugging Commands149150```bash151# Git bisect for regressions152git bisect start153git bisect bad HEAD154git bisect good <last-known-good-commit>155156# Find recent changes to a file157git log --oneline -10 path/to/file158159# Search for error message160grep -r "error message" --include="*.ts"161```162163## Common Fixes by Error Type164165| Error Pattern | Likely Root Cause | Investigation |166|---------------|------------------|---------------|167| `Cannot read property 'x' of undefined` | Missing data, async timing | Trace where null originates |168| `Module not found` | Path issues, missing export | Check exact path, case sensitivity |169| `CORS error` | Backend config | Check Network tab, test with curl |170| `Timeout` | Slow operation, connection issue | Profile, check network |171| Works locally, fails in CI | Environment diff | Compare env vars, versions, permissions |172| Intermittent failures | Race conditions, timing | Look for shared state, async issues |173174## Real-World Impact175176From debugging sessions:177- Systematic approach: 15-30 minutes to fix178- Random fixes approach: 2-3 hours of thrashing179- First-time fix rate: 95% vs 40%180181## See Also182183- [root-cause-tracing.md](./root-cause-tracing.md) - Trace bugs backward to original trigger184- [strategies.md](./strategies.md) - Detailed strategies by error type185- [examples.md](./examples.md) - Real debugging case studies