Back to Coding

How Do You Debug Code You Did Not Write?

How Do You Debug Code You Did Not Write?
AI Tutorials Team 22 May 2026

Debugging your own code starts with knowing what it should do. Debugging somebody else's starts with working that out, and skipping that step produces fixes that make the symptom disappear and break something you did not know was intentional.

What is the first question?

What was this supposed to do. Not what it does: what it was for. Without that you cannot distinguish a bug from a deliberate behaviour that looks odd, and unfamiliar code contains plenty of both.

Where do you find the intent?

  • Tests. Which describe expected behaviour directly.
  • Commit messages. Frequently the only record of why.
  • Issue references. Which explain what a change was for.
  • Comments. Particularly ones explaining an exception.
  • Whoever wrote it. If they are reachable.

What is the version control trick?

Finding when the line was last changed and reading that commit. It frequently reveals that an odd-looking construction was a deliberate fix for something, which is exactly what you need to know before removing it.

This is the most underused debugging tool available in an inherited codebase.

How do you use AI here?

Explain what this function is intended to do and what the odd-looking parts of it are handling. Where you are inferring intent rather than reading it from a comment or a test, say so.

The final clause matters, since a generated explanation of intent is a plausible story unless something in the code supports it.

What is the trap with inferred intent?

That it sounds authoritative. A model explaining why code does something unusual will produce a reason, and the reason may be entirely invented, which is more misleading than no explanation at all.

Requiring evidence from a comment, a test or a commit separates the two.

What should you not change?

Anything you cannot explain the purpose of. Unfamiliar code contains workarounds for problems you have not encountered, and removing one because it looks unnecessary reintroduces something that was fixed two years ago.

How do you make a change safely?

Write a test capturing current behaviour first, then make the change and see what else breaks. That converts an unknown blast radius into a visible one, and it is worth the extra twenty minutes.

What about generated code you wrote yourself?

The same category, which surprises people. Code produced from a description six weeks ago is unfamiliar code, and the reasoning behind its decisions may never have existed anywhere.

How do you build a map quickly?

By tracing one operation end to end rather than reading files individually. Following a single request from where it enters to where it returns teaches more about an unfamiliar system than any amount of browsing, and it is directly relevant to whatever you are debugging.

Asking for that walkthrough, one step at a time with the file and function named at each, is a good use of a tool with repository access. The map it produces is what makes the subsequent investigation targeted rather than exploratory.

What is the sequence?

Establish intent, trace the actual behaviour, find where they diverge, then change the smallest thing. Skipping the first step is what turns a bug fix into two bugs.