Skip to content
mentoor

Topic 1 of 6 · 4 min read

Duplicated logic from generated code

Goal: Find logic in an AI-written diff that duplicates an existing helper and say which code should be reused instead.

An agent writes the code it can see, not the code it cannot, so it often builds a second copy of a helper that already lives elsewhere in your repository. Your job in review is to find the copy and point the change at the original.

Check yourself · 1 of 3

An agent adds a new function that formats prices. What is the best first review step?

Choose one answer

Why it matters

Duplicated logic is a bug waiting to split. Two functions that both format a price, validate an email or retry a request will drift apart. One gets a fix, the other does not. A year later a customer sees two different totals. AI tools make this easy to produce: asking for a feature in a fresh session means the model only knows the files it was shown, and it is cheaper for it to write ten new lines than to search a large codebase. GitClear's 2025 research on assistant-era repositories reports a rise in copy-pasted lines, which is the same habit at scale.

How to find it

  1. Name the job of each new function. "Parses a date", "builds a URL", "clamps a number". Then search the repo for that verb: grep -rn "formatPrice", or ask your editor for symbols with a similar name.
  2. Look for new files called utils, helpers or common. An agent that creates its own utility file did not find the existing one.
  3. Check for near twins. Same shape, different names: toCents next to an existing moneyToMinor.
  4. Look in the test files too. Copy-pasted fixtures and setup blocks count.

How to fix it

Tell the agent where the shared code is. Name the file and the function: "Use formatMoney from src/lib/money.ts; do not add a new formatter." Better, put that rule in AGENTS.md or CLAUDE.md so every session sees it. If the two versions really differ, make the difference a parameter on the shared one.

Before and after

// Added by the agent in src/orders/summary.ts
function money(cents: number) {
  return "$" + (cents / 100).toFixed(2);
}

The repo already has formatMoney(cents, currency) in src/lib/money.ts, which handles other currencies and rounding. The reviewed change imports it:

import { formatMoney } from "../lib/money";

What goes wrong

  • Treating similar as identical. Two functions may differ on purpose, such as rounding rules for tax versus display. Read both before merging them.
  • Over-merging. Forcing unrelated code into one helper creates a function full of flags. Three similar lines are sometimes cheaper than a wrong shared abstraction.
  • Only checking the new code. The copy may be in a file the diff does not show. Search the repo, not just the patch.

Examples

A second retry loop

// New in src/api/orders.ts
for (let i = 0; i < 3; i++) {
  try { return await fetch(url); }
  catch { await sleep(200 * i); }
}

The repo already has withRetry() in src/lib/http.ts, with backoff and a logged error. The inline loop has neither. Ask the agent to call withRetry and delete the loop.

The twin that is not a twin

A new slugify looks identical to src/lib/slug.ts. But the old one strips accents, and the new one keeps them, so URLs for French titles would change. Reuse the old one and ask whether the new behaviour was wanted at all.

Key points

  • Agents only know the files they were shown, so they often rebuild helpers that already exist.
  • Search the repository for the job of each new function, not for its name.
  • A new utils or helpers file in an agent diff is a signal that the existing shared code was missed.
  • Tell the agent the exact file and function to reuse, and record the rule in AGENTS.md or CLAUDE.md.
  • Read both versions before merging them, because a near twin may differ on purpose.

Common mistakes

  • Reviewing only the lines in the diff and never searching the repo for an existing helper.
  • Merging two near-identical functions without checking that their edge-case behaviour matches.
  • Telling the agent "avoid duplication" without naming the file it should reuse.
  • Accepting a new utils file because it is small and tidy.

Check yourself · 2 of 3

Why do AI agents often duplicate helpers that already exist in a repo?

Choose one answer

Check yourself · 3 of 3

Two functions look nearly identical in a diff. What should you check before merging them into one?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic