| name | prevent-generated-code-duplication |
| description | Detect and refactor duplicate logic introduced by generated code, including overlaps between generated artifacts and existing project code. |
Goal
Prevent divergence and maintenance risk by consolidating duplicated logic created by code generation or by mixing generated and hand-written code.
When to Use
- New generated files duplicate logic already present in the project.
- Generated output duplicates existing generated modules.
- Generated code and hand-written code implement equivalent validation/parsing/mapping/error handling.
- Reviews identify repeated blocks after schema/code generation.
Rules
- Treat generated-vs-existing duplication as high-priority technical debt and resolve in the same change when feasible.
- Prefer extraction to shared runtime modules (helpers/utilities/services), not copy-paste edits across multiple generated files.
- Keep codegen outputs thin: delegate behavior to shared functions where possible.
- If generator templates are available, fix duplication at template/source level first.
- If template-level fix is not feasible, add a small shared abstraction and route both generated and existing code through it.
- Preserve behavior and public interfaces; avoid broad architecture changes unless required.
- Validate with lint, typecheck, and relevant tests after deduplication.
Workflow
- Identify duplicate blocks and classify:
- generated <-> generated
- generated <-> hand-written
- Pick dedup strategy:
- template fix (preferred)
- shared helper/module extraction
- Refactor call sites to shared implementation.
- Remove duplicated code paths.
- Run verification (lint + typecheck + tests).
- Document the dedup decision in change summary.
Output Pattern
- Duplicates found (files + intent).
- Chosen dedup strategy and why.
- Refactored shared location.
- Verification results.