Patterns / Duplicated blocks
Duplicated blocks
A model writing a second endpoint will happily write the first one again with a different name. Studies of AI-assisted repositories have reported more copy-pasted code and less refactoring since coding assistants became common. Each copy is another place to fix the next bug.
What it looks like
Two handlers with the same 6-line try/query/log/map body The same validation block pasted into three forms A utility re-implemented in a new file instead of imported
Why it matters
The model's context window is the file in front of it, not your whole codebase, and writing fresh code is easier for it than finding the existing helper.
Before and after
Before
export async function getInvoices(id) {
const rows = await db.query(SQL, [id]);
return rows.map(toInvoice);
}
export async function getOverdue(id) {
const rows = await db.query(SQL, [id]);
return rows.map(toInvoice).filter(isOverdue);
}After
export async function getInvoices(id) {
return (await db.query(SQL, [id])).map(toInvoice);
}
export async function getOverdue(id) {
return (await getInvoices(id)).filter(isOverdue);
}How to fix it
- Flag repeated blocks of 4+ substantial lines inside the PR (SlopScore for Code ignores imports, braces and short lines).
- Extract the shared part, or call the first function from the second.
- Give agents the existing helper explicitly in the prompt or rules file.
When it's fine
Test cases that deliberately repeat setup, and generated code you don't edit by hand (put those paths in ignorePaths).
Catch this automatically. SlopScore for Code checks every pull request for this pattern (rule
duplicate-block) and 11 others. Score a public PR or add the free GitHub Action.