Start with an observable failure
Write down the smallest input, action, actual result and expected result that demonstrate the problem. Capture the complete error message and stack trace before changing code. Confirm the environment, dependency version and data shape. If the failure cannot be repeated reliably, record how often it occurs and which evidence is missing instead of guessing. A useful report says that target 3 in [1, 3, 3, 7] returned the second duplicate when the contract requires the first; “search is broken” is not specific enough.
Reduce the failing surface
Remove unrelated UI, network and database steps until the failure remains in a small function or request. Change one factor at a time and keep a known-good comparison. Use a minimal fixture that still contains the important boundary, such as duplicate values. Do not delete validation, catch every exception or replace real inputs with an unrelated toy merely to make the symptom disappear. The purpose is to locate the first incorrect state, not to hide the final error.
Read the stack and inspect state
Begin at the first frame in code you own, then inspect the inputs that reached it. A debugger statement or IDE breakpoint can pause execution when debugging tools are active; remove temporary pauses before committing production code. Prefer focused observations such as low, high and middle over logging whole user records. Never put passwords, tokens or personal data in logs. Check the last correct value and the first incorrect value—the transition often reveals the violated assumption.
State an invariant before editing
For the binary search below, the candidate answer is always in the half-open range [low, high). Values before low are known to be below the target. Each loop must shrink the range. Setting high to middle keeps a matching value eligible, which is necessary to find the first duplicate. Writing these rules makes an off-by-one change reviewable. If you cannot explain why each branch preserves the invariant, more random edits are unlikely to produce a dependable fix.
Turn the reproducer into a test
Add the smallest failing example before applying the correction, confirm that the old implementation fails it, then run the wider suite after the change. Include empty input, one value, duplicates, values before and after the range and invalid input. A passing test only proves the assertions that actually ran, so inspect the expected values and make the test fail deliberately once. Keep regression tests deterministic: avoid live APIs, current time and shared mutable state unless the test controls them.
Separate cause, fix and cleanup
Make the smallest change that repairs the stated contract. Avoid combining it with renaming, formatting, dependency upgrades or performance work, because a wide diff makes the cause hard to verify. After tests pass, remove temporary logs and debugger statements, review error handling, and document any input assumptions. Explain why the change prevents recurrence rather than saying only that it works now.
Know when local debugging is insufficient
Browser and unit tests cannot reveal every production problem. Intermittent failures may require request IDs, timestamps, sanitized structured logs, deployment version and metrics. Database concurrency issues need transaction evidence; network failures need status, timeout and retry context. Do not copy production secrets or personal records into a local fixture. If evidence is incomplete, preserve it and escalate with a narrow hypothesis instead of asserting a root cause.
Run the boundary exercise
Save the module below and the companion debugging-boundary.test.mjs in the same directory, then run node debugging-boundary.test.mjs. The function returns the index of the first value greater than or equal to the target, or -1 when none exists. The test covers seven boundary cases, rejects unsorted/non-array input, verifies that the array is unchanged and confirms that the displayed example matches the tested file. Set a breakpoint inside the loop and watch how [low, high) shrinks for target 3.
Corrected boundary search with explicit input checks
export function firstAtLeast(sorted, target) {
if (!Array.isArray(sorted)) throw new TypeError("Expected an array");
for (let index = 1; index < sorted.length; index++) {
if (sorted[index] < sorted[index - 1]) {
throw new RangeError("Input must be sorted in ascending order");
}
}
let low = 0;
let high = sorted.length;
while (low < high) {
const middle = low + Math.floor((high - low) / 2);
if (sorted[middle] < target) {
low = middle + 1;
} else {
high = middle;
}
}
return low < sorted.length ? low : -1;
}
Expected result
Target 3 in [1, 3, 3, 7] -> index 1 Target 4 in [1, 3, 3, 7] -> index 3 Target 8 -> -1 Unsorted input -> RangeError Test output: PASS: 7 boundary cases, sortedness/type failures, immutability and displayed-code consistency
Further reading
Check your progress
Keep your working files, test cases and notes together. Explain one design decision, demonstrate a failure case and list what you would improve next. These concrete outputs make it easier to identify the next skill to practice.