Skip to content
mentoor

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?

Choose one answer

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

  1. Unused functions, imports and variables. Your linter and the compiler can find many of these. Run them first (tsc --noUnusedLocals, ESLint no-unused-vars, ruff, knip).
  2. Unreachable or always-true branches. A check for a value the type system already rules out, or a catch for an error nothing throws.
  3. Debug output. console.log, print, debugger, commented-out blocks, hard-coded test values.
  4. Stubs. TODO, pass, throw new Error("not implemented"), functions that return a constant.
  5. Old and new side by side. A rewrite that keeps the previous implementation "for safety" behind a flag nobody sets.
  6. 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?

Choose one answer

Check yourself · 3 of 3

An agent leaves "// TODO: handle errors" in merged code. What should you ask for?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic