Learning center

Should you extract the duplicated code a bot flagged?

Extract shared code when the copies must change together, so that a fix to one is always a fix to the other. Keep them apart, and say why, when they change for different reasons. If they should merge later, defer the change with a note that names the event that will make the merge worthwhile.

The question behind the comment#

"This logic is duplicated; consider extracting a helper" is one of the most common AI review comments, and the reviewer can see only half of the evidence. Text that matches is easy to detect. Whether the two copies stay identical over time depends on why each one exists, which the diff rarely shows.

The test that decides it is old: gather together what changes for the same reasons, and separate what changes for different reasons. Robert C. Martin uses it to explain the single responsibility principle. Applied to one review comment, it becomes a single question. If someone fixes a bug in one copy next month, must the other copy get the same fix?

Copies that are meant to diverge#

In October 2026, an OpenROAD pull request, part 9 of a 12-part stack for the resizer's PARALLEL_EVAL setup repair, added UnbufferMt, a parallel form of the existing buffer-removal move, beside the serial one. A review comment headed "Claude", posted from a maintainer's account, flagged the new file: "UnbufferMtGenerator.cc copies roughly 300 lines of UnbufferGenerator.cc almost verbatim". It recommended the usual fix and gave its reason: "Suggest moving the shared code into a helper both use, so the two cannot drift apart. They already differ in the conflict guard and in how slack_scene is set." Review comment

The pull request merged the same day, 18 October 2026, and the author answered in a follow-up pull request that collected responses to the review. Its table lists this finding with the response "Mt/serial duplication is intentional until serial moves retire; shared helper deferred." Follow-up PR description A finding about a roughly 450-line copy in a sibling pull request got "Intentional Mt/serial separation until serial moves retire. No change."

The reviewer's own evidence supports the author. The two generators already differ in the conflict guard and in how a value is set. A shared helper would have to take parameters or flags for those differences, and the serial copy is planned for removal. Extracting now would couple new code to code that is on its way out. The response is a deferral with a condition: the helper becomes worth writing only if both versions stay. The visible thread does not link an issue that tracks that condition, which is the part I would add.

Values that match by accident#

The opposite request came from CodeRabbit in a recoupable/api pull request in November 2026. The code used one constant for two jobs, a board label and a sandbox profile name, because both happened to be the string "claude". CodeRabbit objected: "The board profile name .as({ name: CLAUDE_BOARD_LABEL.value }) reuses a label constant as an identity." Its reason was the same test: "Label values and profile names change for different reasons." Review comment

The author agreed and split them in commit a2a3453: "CLAUDE_BOARD_PROFILE is now its own constant." Both the profile and the watcher ID now use it "instead of reusing the label value." Author's reply The pull request's diff now holds two constants with the same text, each in its own file under lib/board/:

export const CLAUDE_BOARD_LABEL = { key: "agent", value: "claude" } as const;
export const CLAUDE_BOARD_PROFILE = "claude";

That is deliberate duplication. Changing the label that marks board tasks should not silently rename a sandbox profile, and the reverse. The pull request was still a draft when I read it, so this records an accepted change, not a merged one.

Decide for this comment#

What you findPoints toAction
A bug fixed in one copy would have to be fixed in the other, and the copies encode one ruleSame reason to changeExtract the shared code in this pull request
The copies are identical today, but each follows a different owner, caller or specificationDifferent reasonsKeep them apart and say which reason each follows
The copies already differ in ways a helper would need flags forDifferent reasonsKeep them apart; a flag-driven helper couples them
One copy is planned for removal, or the shared shape is not yet clearPossibly the same reason, laterDefer with an issue that names the trigger, such as removing the serial path
The duplicated lines are a small fixture or setup in testsUsually different reasonsKeep them; each test should read on its own

Check the history, not only the text#

Three checks answer the question with evidence instead of instinct:

  1. Read the history of both copies with git log -L on the duplicated range, or git blame. Copies that were edited together in the same commits change for the same reason. Copies whose edits never coincide do not.
  2. Name the rule each copy implements. If you can write one sentence that covers both, such as "a session token expires after 24 hours", extract it. If the sentences differ, keep the copies.
  3. Count the parameters the helper would need. If it takes a mode flag to reproduce both copies, the extraction moves the difference into a conditional and keeps the coupling.

When the answer is "extract", do it while the context is fresh, and run the tests for both callers. When it is "keep", reply with the reason each copy follows; that sentence is what a later reviewer needs to avoid raising the same comment. When it is "defer", the follow-up issue is the part that makes the deferral real, as review finding follow-through describes for fixes that arrive later.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading