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.

Workflow

  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

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

Explore all resources