Prompt Engineering Refactoring Duplication

Remove Duplicate Code — Consolidate Without Coupling

Three near-identical functions, one future bug fixed in only two of them. This prompt consolidates duplication into shared logic — and knows when near-duplicates should stay separate.

Overview

Duplicate code removal sounds mechanical until the duplicates turn out to differ on purpose. This prompt uses the maintainability goal — extract the shared logic so a future fix lands in one place — with the warning that saves refactors: near-duplicate code is not always true duplication, and merging two blocks that evolve for different reasons creates coupling, not reuse. The loaded setup carries a real-world constraint of exactly that kind: one of the three export functions uses a subtly different date format, and the contract demands confirmation of whether that is intentional before any merge.

How to use this resource

  1. Map the copies

    Identical, near-identical, and intentionally-different blocks get separated before anything merges.

  2. Confirm the differences

    A subtle divergence — a date format, a rounding rule — is a question for a human, not a casualty of the merge.

  3. Extract once, verify each

    The shared logic lands in one place; every original call path is validated against its old behavior.

Why This Works

  • Future fixes landing in one place is the entire economic case for the refactor
  • The coupling warning prevents the merge that makes two features hostage to each other
  • Per-call-path validation catches the copy that was different for a reason

Best for

  • Codebases where the same fix keeps being applied in several places
  • Copy-paste-driven modules that drifted slightly apart
  • Cleanups after fast feature work

Not for

  • Detecting duplicated phrasing in a prompt — that's the Prompt Cleaner
  • Reporting duplication as a finding without changing it — that's the Code Review Prompt Generator

Use cases

  • Consolidating copy-pasted export, report, or handler functions
  • Extracting shared validation scattered across endpoints
  • Deciding whether two similar blocks are one concept or two

FAQ

How does this refactor prompt handle export functions that differ in one small way, like a date format?

It's told to ask before merging. The stated constraint is that the CSV export uses a subtly different date format — "confirm whether that is intentional before merging." The TRANSFORMATION GUIDANCE warns that near-duplicate blocks merged when they evolve for different reasons "creates coupling, not reuse." The refactor-prompt-builder outputs this prompt; the divergence becomes a question for you, not a silent casualty.

Will this prompt change what my code outputs when it removes the duplication?

It's constrained not to. BEHAVIOR PRESERVATION REQUIREMENTS keep formats, ordering, precision, side effects, and public contracts identical, and if goal and behavior conflict, "behavior wins: stop and explain the conflict." Anything skipped for safety lands under "Suggested but not applied." You still validate each original call path — the prompt produces the refactor, not a proof it's behavior-neutral.

More resources from Refactor Prompt Builder

Resources that pair well

Related tools

Tip: Save time by exploring related resources and tools that integrate with this resource.