When debugging or reviewing an unfamiliar codebase in a code comprehension round, trace from public entry points and mutable state outward to bounds, locks, and error paths so you isolate the root cause before making surgical, test-verified fixes.
TL;DR30-second simplified summary▾
The Core Idea (Intermediate): Audit unfamiliar code for unbounded collections (OOM), 32-bit int counter wraps, check-then-act races, and lock-held I/O.
Key Trade-Offs: Prefer computeIfAbsent or per-entity locks over global synchronized, inject Clock for deterministic tests, and bound every queue or cache.
Boundary Rule: Make the smallest backward-compatible fix that restores the broken invariant.
Audit Bounds, Atomicity, and Test Determinism.
Why this matters: Catch production-grade memory, race, and boundary bugs systematically.
Check every cache/queue for a max capacity or eviction policy, replace int sequence/byte offsets with long, replace containsKey + put with atomic map ops or per-entity locks, and inject Clock so regression tests never Thread.sleep().
Guessing Fixes vs. Invariant Repair
- Unbounded ArrayList / HashMap
- Global synchronized around I/O
- Thread.sleep() in unit tests
- Ring buffer / LRU capacity
- Per-entity lock outside I/O
- Clock injection & latch test
Pair every root-cause fix with a deterministic test that fails before the patch and passes after.
Practice: Make the trade-off yourself.
When dropped into a 600-line unfamiliar module during a code comprehension round, what is the fastest way to locate a subtle state-corruption bug?
How to Prepare for Code Comprehension
Read unfamiliar codebases from entry points, mutable state, and bounds outward—isolate the root cause on a concrete trace, then ship a minimal fix with a deterministic regression test.