duplicate-function-body
Functions that are copies of each other under new names.
Deterministic checkBlocks by default
The rule
Agents read this text as written when they write and review code.
Never write a function whose body is another function's with only the names or the literal values changed. Keep one, and pass the values that differed as arguments.
- This covers every function, method and callback, in the same file or another.
- A function that only calls another function with its own fixed values is not a copy: the shared code is already in one place.
Renaming a copy hides it from a search by name, so the two drift apart unseen.
Examples
The check flags the incorrect code and passes the correct code. Each example is a small repo, because this rule looks across files. It opens on the file that matters; the other files are the repo around it.
Incorrect
srcbillinginvoices
interface Params {id: number;totals: Record<string, number>;}export const buildChargeSummary = ({ id, totals }: Params): Record<string, number> => {const first = totals.a + totals.b + totals.c + totals.d;const second = first + totals.e + totals.f + totals.g + totals.h;const third = second + totals.i + totals.j + totals.k + totals.l;return { id, first, second, third };};
Correct
srcbillinginvoices
interface Params {id: number;totals: Record<string, number>;}export const buildChargeSummary = ({ id, totals }: Params): Record<string, number> => {const first = totals.a + totals.b + totals.c + totals.d;const second = first + totals.e + totals.f + totals.g + totals.h;const third = second + totals.i + totals.j + totals.k + totals.l;return { id, first, second, third };};
Configure
- BlockDefault
"blocking"Stops a run when a file the run changed breaks the rule. - Advise
"advisory"Reports it and hands it to the refactor agent. Never stops a run. - Off
"off"Not checked. Use it when your own linter already enforces the rule.
Add this to your lightsout.config.json, then change the value.
{"standards-rule-settings": {"duplicate-function-body": {"severity": "blocking","options": {"minBodyTokens": 40}}}}