Skip to content

How to debug code you didn't write

A calm method for debugging unfamiliar code: reproduce, locate, form hypotheses, test them one at a time, and fix with the smallest change that explains the bug.

October 13, 2026 · 2 min read · RepoGrasp team

Debugging your own code is hard enough. Debugging someone else's adds a second problem: you don't know how it's supposed to work. The trick is to keep those two problems separate, learning just enough of the design to explain the bug.

1. Reproduce it reliably

Before reading any code, get the bug to happen on demand: exact inputs, user state, environment and steps. If it's intermittent, note what's different when it happens (time, load, data, order of actions). A bug you can't reproduce is one you can't confirm you've fixed.

2. Find where it surfaces

Start from the symptom and work backward:

  • An error message: search for it, or read the stack trace (see tracing a bug from a stack trace).
  • Wrong data on screen: find the component, then where its data comes from.
  • Wrong data in the database: find every writer of that field.

3. Trace the path the bad data takes

Follow the value from where it's wrong back to where it was last right. At each step, ask: is the value correct here? Logging or breakpoints at three or four points along the path quickly narrow it down. This is binary search on the code path.

4. Form hypotheses, then test them one at a time

Write down possible causes: "the discount is applied twice", "the cache returns stale prices", "the timezone shifts the date". Rank them by likelihood and cost to test. Test the cheapest likely one first. Change one thing at a time; changing three things at once tells you nothing.

5. Understand the intended behaviour before fixing

Before you change code, find out what it was meant to do: tests, comments, the commit that introduced it (git log -L on the function), the original PR. The "obvious" fix sometimes breaks a case the original author handled deliberately. See code archaeology with git.

6. Make the smallest fix that explains the bug

The best fix accounts for every symptom you saw. If your fix stops the error but you can't explain why the bug happened, you may have hidden it rather than fixed it.

7. Add a test that would have caught it

Write a failing test that reproduces the bug, then make it pass. This proves the fix and prevents regressions.

8. Check for siblings

If one function had this bug, similar code nearby might too. Search for the same pattern.

Common causes in unfamiliar JavaScript code

  • Missing await, so a promise is treated as a value
  • Mutating shared objects, often arrays or objects passed into a function
  • Time zones and date parsing
  • Floating-point arithmetic with money
  • Caches serving stale values
  • Two writers updating the same record without coordination

Using AI while debugging

The sample demo in RepoGrasp includes a debugging scenario: from a reported symptom, it shows code paths worth investigating, labelled as hypotheses with evidence, not as conclusions. On a real repository you can ask "the order total is sometimes 1 cent off; which code paths compute or round totals?" and get cited candidates. Treat them as hypotheses to test in step 4, not as the answer.

Try the debugging scenario.