Topic 2 of 5 · 4 min read
Coupling, cohesion and duplicated logic
Goal: Spot duplicated or tightly coupled code an agent introduced and choose the refactor that removes it.
Good code keeps related things together (cohesion) and unrelated things apart (low coupling), and agent-written code tends to break both because it adds instead of reorganises.
Check yourself · 1 of 3
An agent adds a new function formatMoney, but the repo already has one in lib/money.ts. What is the best review response?
Why it matters
An agent starts each task with a limited view of the repository. It often does not find the helper you wrote last year, so it writes a new one. It also takes the easiest place to put new code: the file that is already open. Over many tasks you get three copies of date formatting, and one helpers.ts with 2,000 lines that everything imports. A change to one thing now risks all the others.
What to look for
- A new helper that sounds familiar. Before accepting
formatMoney, search the repo formoney,currencyandformat. If one exists, the new one is a duplicate. Ask the agent to use the old one. - Near-copies with small differences. Two functions that differ by one argument usually want to be one function with that argument. But do not merge unrelated code that merely looks alike. Merge when the two must change together.
- Shared mutable state. A module-level array, a cache object or a singleton that several modules write to means any of them can break the others, in an order nobody can predict.
- Files that do everything. If a file's name is
utils,helpers,commonormanager, ask what it is for. A file with one reason to change is cohesive. A file with five is not. - Long parameter lists and reaching into objects. Code like
order.customer.address.country.codeknows the whole shape of three other types. A change in any of them breaks this line.
Before and after
// agent's change: a second copy, with a subtle difference
export function toUsd(cents: number) {
return "$" + (cents / 100).toFixed(2);
}
The repo already has formatMoney(cents, currency) in lib/money.ts, which handles thousands separators and negative values. The copy does neither, so a refund of -1250 prints as "$-12.50" on one screen and "-$12.50" on another. The fix is to delete the copy and import the original.
What goes wrong
- Merging too early. Two pieces of code look the same today but belong to different features. Joining them couples those features.
- Fixing the bug in one copy. The next bug report names the copy you did not fix.
- Accepting the god file. Each agent change adds fifty lines to the same module, and no single diff looks big.
Give the agent a map of where shared code lives, in AGENTS.md or CLAUDE.md, and ask it to search before it writes.
Examples
Two money formatters
// lib/money.ts (existing)
export const formatMoney = (cents: number, cur = "USD") =>
new Intl.NumberFormat("en-US", { style: "currency", currency: cur }).format(cents / 100);
// agent added in reports/summary.ts
const money = (c: number) => `$${(c / 100).toFixed(2)}`;
The new one has no thousands separator, so 1,234,500 cents prints as $12345.00 in reports and $12,345.00 elsewhere. Remove the copy and import formatMoney.
The shared cache nobody owns
An agent adds export const cache = {} to utils.ts so two services can skip a repeated lookup. Both write to it with different key formats.
A better design gives the cache one owner module with get and set functions, and each service passes through them. The state now has one writer, one key format and one place to test.
Key points
- Cohesion means a module has one reason to change; coupling means a change in one place forces changes elsewhere.
- Agents often write a new helper because they did not find the existing one, so search the repo before approving any new utility.
- Merge duplicated code only when the copies must change together, not just because they look alike.
- Module-level mutable state shared by several files makes failures depend on order and are hard to reproduce.
- Files named utils, helpers or common are warning signs: ask what single job each one has.
Common mistakes
- Accepting a new helper without searching for an existing one.
- Merging two similar functions that belong to different features.
- Letting a utils file grow by fifty lines in every change.
- Ignoring module-level mutable variables because the tests pass.
Check yourself · 2 of 3
Which sign most suggests a file has low cohesion?
Check yourself · 3 of 3
Two functions in different features look almost identical today. When should you merge them into one?
Practice items, not counted towards your mastery.
Start here
- Architectural Decision Records (adr.github.io) (opens in a new tab)DocsFree
Templates and examples for recording context, decision and consequences.
- Software Architecture Guide (martinfowler.com) (opens in a new tab)ArticleFree
Clear essays on layers, coupling and keeping a design healthy over time.
- Architectural fitness function (Thoughtworks Technology Radar) (opens in a new tab)ArticleFree
The idea behind automated checks that guard an architecture.