Duplicating code instead of reusing it
Spot a new helper, validator or client that duplicates one already in the codebase that the agent never opened. Explain the later cost when the copies diverge, and when merging the copies would instead create the wrong abstraction.
Builds on: What the agent cannot see
A tiny new email check
You asked the agent to add email validation to the signup endpoint. It came back fast, tests green. The diff adds `isValidEmail` inside `signup/handler.ts`. But `src/lib/validation.ts` already has `validateEmail`, used by login, invites and password reset. The transcript shows the agent never opened that file.
Is one small extra helper harmless?
It can only reuse what it has seen
The agent can reuse only what is in its context. A helper in a file it never opened does not exist for it, and nothing in the task made it search. Writing a fresh helper is quick and turns the tests green.
The later cost comes when the copies diverge. A fix or rule change lands in one copy, not the other. The same input is then accepted in one place and rejected in another.
Tells of a duplicate, and the check
**Tells** in the diff or transcript:
- a new helper, validator, parser or client for a common job the codebase very likely handles already (emails, money, dates, slugs, HTTP calls)
- regexes, base URLs, timeouts or limits declared again as local constants
- no search of the repo before the code was written
**Check:** run `$ grep -rn` for the concept and its synonyms. Then compare what each copy really does.
The duplicate in the diff
**Tell:** a module-local regex and helper for email, which the app already handles elsewhere. `$ grep -rn Email src/lib` finds `validateEmail`, which also trims and lowercases the address and returns the normalised form.
**Later cost:** signup stores `Bob@X.com` as typed. Login looks up `bob@x.com` and never finds the account. Rules added to `validateEmail` later also skip signup.
**Fix:** call `validateEmail` and store what it returns.
// TypeScript
--- a/src/signup/handler.ts
+++ b/src/signup/handler.ts
+const EMAIL_RE = /^\S+@\S+\.\S+$/;
+
+function isValidEmail(s: string) {
+ return EMAIL_RE.test(s);
+}
+
export async function signup(req: Req) {
- // TODO: validate email
+ if (!isValidEmail(req.body.email)) {
+ return badRequest("invalid email");
+ }
await createUser(req.body);
When merging is the wrong fix
Not every lookalike is a duplicate. Take two `format_price` functions. Invoices need exactly two decimals plus the currency code. The shop page uses the locale's symbol, sometimes with no decimals. Merge them into `format_price(x, for_invoice=True)` and you get the wrong abstraction: one function serving two rules that change for different reasons. Every page tweak now puts invoices at risk, and the flags keep piling up.
Ask: *if one changes, must the other change too?* Yes: keep one shared copy. No: keep both and name each by its purpose.
Remember
When the agent adds a helper, search for one that already exists. It reuses only what it opened. Copies of the *same* rule drift apart and give inconsistent behaviour later. Code that only *looks* alike but follows *different* rules should stay separate.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.