Spaces:
Runtime error
Runtime error
File size: 5,745 Bytes
a6b96c2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | # Common Bug Patterns
Checklist of frequent bug patterns to scan before forming hypotheses. Ordered by frequency. Check these FIRST β they cover ~80% of bugs across all technology stacks.
<patterns>
## Null / Undefined Access
- **Null property access** β accessing property on `null` or `undefined`, missing null check or optional chaining
- **Missing return value** β function returns `undefined` instead of expected value, missing `return` statement or wrong branch
- **Destructuring null** β array/object destructuring on `null`/`undefined`, API returned error shape instead of data
- **Undefaulted optional** β optional parameter used without default, caller omitted argument
## Off-by-One / Boundary
- **Wrong loop bound** β loop starts at 1 instead of 0, or ends at `length` instead of `length - 1`
- **Fence-post error** β "N items need N-1 separators" miscounted
- **Inclusive vs exclusive** β range boundary `<` vs `<=`, slice/substring end index
- **Empty collection** β `.length === 0` falls through to logic assuming items exist
## Async / Timing
- **Missing await** β async function called without `await`, gets Promise object instead of resolved value
- **Race condition** β two async operations read/write same state without coordination
- **Stale closure** β callback captures old variable value, not current one
- **Initialization order** β event handler fires before setup complete
- **Leaked timer** β timeout/interval not cleaned up, fires after component/context destroyed
## State Management
- **Shared mutation** β object/array modified in place affects other consumers
- **Stale render** β state updated but UI not re-rendered, missing reactive trigger or wrong reference
- **Stale handler state** β closure captures state at bind time, not current value
- **Dual source of truth** β same data stored in two places, one gets out of sync
- **Invalid transition** β state machine allows transition missing guard condition
## Import / Module
- **Circular dependency** β module A imports B, B imports A, one gets `undefined`
- **Export mismatch** β default vs named export, `import X` vs `import { X }`
- **Wrong extension** β `.js` vs `.cjs` vs `.mjs`, `.ts` vs `.tsx`
- **Path case sensitivity** β works on Windows/macOS, fails on Linux
- **Missing extension** β ESM requires explicit file extensions in imports
## Type / Coercion
- **String vs number compare** β `"5" > "10"` is `true` (lexicographic), `5 > 10` is `false`
- **Implicit coercion** β `==` instead of `===`, truthy/falsy surprises (`0`, `""`, `[]`)
- **Numeric precision** β `0.1 + 0.2 !== 0.3`, large integers lose precision
- **Falsy valid value** β value is `0` or `""` which is valid but falsy
## Environment / Config
- **Missing env var** β environment variable missing or wrong value in dev vs prod vs CI
- **Hardcoded path** β works on one machine, fails on another
- **Port conflict** β port already in use, previous process still running
- **Permission denied** β different user/group in deployment
- **Missing dependency** β not in package.json or not installed
## Data Shape / API Contract
- **Changed response shape** β backend updated, frontend expects old format
- **Wrong container type** β array where object expected or vice versa, `data` vs `data.results` vs `data[0]`
- **Missing required field** β required field omitted in payload, backend returns validation error
- **Date format mismatch** β ISO string vs timestamp vs locale string
- **Encoding mismatch** β UTF-8 vs Latin-1, URL encoding, HTML entities
## Regex / String
- **Sticky lastIndex** β regex `g` flag with `.test()` then `.exec()`, `lastIndex` not reset between calls
- **Missing escape** β `.` matches any char, `$` is special, backslash needs doubling
- **Greedy overmatch** β `.*` eats through delimiters, need `.*?`
- **Wrong quote type** β string interpolation needs backticks for template literals
## Error Handling
- **Swallowed error** β empty `catch {}` or logs but doesn't rethrow/handle
- **Wrong error type** β catches base `Error` when specific type needed
- **Error in handler** β cleanup code throws, masking original error
- **Unhandled rejection** β missing `.catch()` or try/catch around `await`
## Scope / Closure
- **Variable shadowing** β inner scope declares same name, hides outer variable
- **Loop variable capture** β all closures share same `var i`, use `let` or bind
- **Lost this binding** β callback loses context, need `.bind()` or arrow function
- **Scope confusion** β `var` hoisted to function, `let`/`const` block-scoped
</patterns>
<usage>
## How to Use This Checklist
1. **Before forming any hypothesis**, scan the relevant categories based on the symptom
2. **Match symptom to pattern** β if the bug involves "undefined is not an object", check Null/Undefined first
3. **Each checked pattern is a hypothesis candidate** β verify or eliminate with evidence
4. **If no pattern matches**, proceed to open-ended investigation
### Symptom-to-Category Quick Map
| Symptom | Check First |
|---------|------------|
| "Cannot read property of undefined/null" | Null/Undefined Access |
| "X is not a function" | Import/Module, Type/Coercion |
| Works sometimes, fails sometimes | Async/Timing, State Management |
| Works locally, fails in CI/prod | Environment/Config |
| Wrong data displayed | Data Shape, State Management |
| Off by one item / missing last item | Off-by-One/Boundary |
| "Unexpected token" / parse error | Data Shape, Type/Coercion |
| Memory leak / growing resource usage | Async/Timing (cleanup), Scope/Closure |
| Infinite loop / max call stack | State Management, Async/Timing |
</usage>
|