Topic 2 of 6 · 4 min read
Dead code and leftover scaffolding
Goal: Identify dead or leftover code in an agent's change and decide what to delete before merge.
Agents leave a trail of scaffolding: code that was needed during an attempt, is no longer called, and still ships unless a reviewer deletes it. Every dead line is something the next person must read, understand and be afraid to remove.
Check yourself · 1 of 3
In a diff you find a helper function that nothing calls. What is the usual right call?
Why it matters
An agent often works by trial. It writes a version, runs the test, fails, rewrites. The first attempt's helper may survive the rewrite. A debug console.log added to find a bug stays behind. A fallback branch added "just in case" can never run, yet it looks important. Reading cost is real: a reviewer cannot tell whether an unused function is a mistake or a hook for something that is not in the diff.
What to look for
- Unused functions, imports and variables. Your linter and the compiler can find many of these. Run them first (
tsc --noUnusedLocals, ESLintno-unused-vars,ruff,knip). - Unreachable or always-true branches. A check for a value the type system already rules out, or a
catchfor an error nothing throws. - Debug output.
console.log,print,debugger, commented-out blocks, hard-coded test values. - Stubs.
TODO,pass,throw new Error("not implemented"), functions that return a constant. - Old and new side by side. A rewrite that keeps the previous implementation "for safety" behind a flag nobody sets.
- Orphan files. Scratch scripts, generated fixtures, a second config the build does not read.
How to decide
For each item ask one question: what calls this? Search for references. If nothing calls it, delete it. Version control keeps the history; a commented-out copy adds nothing. If something should call it but does not, you have found a real bug, not dead code.
Before and after
export function total(items: Item[]) {
console.log("items", items); // debug
const sum = items.reduce((a, i) => a + i.price, 0);
if (sum < 0) return 0; // prices are validated as >= 0 upstream
return sum;
}
function legacyTotal(items: Item[]) { /* old version */ } // no callers
The reviewed version keeps only the reduce. The log, the impossible branch and the unused function go.
What goes wrong
- Deleting something reached by reflection. Routes, plugins, framework hooks and serialized names are called by string. Check before you remove.
- Trusting the agent's "cleanup" summary. Ask for a grep, or run the unused-code tool yourself.
- Leaving stubs because they are marked TODO. A TODO in merged code is a promise nobody owns. Either do it now or file a ticket.
Examples
The debug log that shipped
def charge(order):
print("DEBUG order", order.__dict__)
return gateway.charge(order.total)
The agent added the print to find a failing test. It now writes customer data to the logs in production. Delete it, and add a lint rule that bans print in src/.
The fallback nobody sets
A rewrite adds if (process.env.USE_NEW_PRICING) { newPricing() } else { oldPricing() }. Nothing sets the variable, so the new code never runs. Decide: ship the new path and delete the old, or drop the new one.
Key points
- Agents work by trial and leave behind helpers, logs and branches from earlier attempts.
- For every suspicious item ask what calls it; if nothing does, delete it, since history keeps a copy.
- Run the compiler and a dead-code tool before reading by eye.
- A stub or fallback that never runs can hide a real bug where something should call it but does not.
- Code reached by name, such as routes and plugins, may look unused to a simple search.
Common mistakes
- Leaving debug logging in because the tests pass.
- Approving a TODO stub with no owner or ticket.
- Removing a function that is called by string name, such as a route or plugin hook.
- Keeping commented-out old code "just in case" when version control already holds it.
Check yourself · 2 of 3
Why does an agent so often leave debug logs and unused helpers behind?
Check yourself · 3 of 3
An agent leaves "// TODO: handle errors" in merged code. What should you ask for?
Practice items, not counted towards your mastery.
Start here
- AI Copilot Code Quality: 2025 research (GitClear) (opens in a new tab)ArticleFree
An analysis of how code churn, copy-pasted lines and moved code changed as AI assistants spread.
- Refactoring (Martin Fowler) (opens in a new tab)BookFree
The catalogue of small behaviour-preserving steps and the reasoning behind doing them one at a time.
- Is High Quality Software Worth the Cost? (martinfowler.com) (opens in a new tab)ArticleFree
Why internal quality pays back through faster feature delivery, the case for caring about maintainability.
More for this topic
- Code Smell (martinfowler.com) (opens in a new tab)ArticleFree
What a smell is: a surface hint that something deeper in the design may be wrong.
- What to look for in a code review (Google Engineering Practices) (opens in a new tab)ArticleFree
Its complexity and consistency checks apply directly to agent-written changes.