Ask Claude to Find Duplicated Code and Consolidate It into Shared Functions
Copy-paste starts as a shortcut and ends as a maintenance nightmare. When the same logic lives in five places, a bug fix in one leaves four still broken. Claude Code can find the duplicates you've forgotten about and extract them into shared code.
Search the codebase for duplicated logic — functions that do the same thing
with slightly different names, repeated code blocks across files, and
near-identical patterns. Extract the shared logic into reusable functions.
Claude reads across files, identifies code that does the same thing (even when variable names differ), and extracts the common pattern:
# Found duplicates:
- UserController.formatDate() and OrderController.formatDate() are identical
- Three components have the same pagination logic copy-pasted
- src/api/users.ts and src/api/orders.ts have identical error handling blocks
Then it extracts:
// Before: same logic in 3 files
// users.ts: const page = Math.max(1, parseInt(params.page) || 1);
// orders.ts: const page = Math.max(1, parseInt(query.page) || 1);
// products.ts: const page = Math.max(1, parseInt(req.page) || 1);
// After: shared utility
// src/utils/pagination.ts
export function parsePage(value: unknown): number {
return Math.max(1, parseInt(String(value)) || 1);
}
Target specific duplication patterns:
# Find similar API clients
Compare all files in src/api/. Which ones have the same fetch wrapper,
error handling, or retry logic? Extract the common pattern into a base client.
# Find similar components
Search for React components that render the same structure with different data.
Can any be consolidated into a single parameterised component?
# Find similar validation
Check if multiple forms validate the same fields the same way.
Extract shared validation rules into a central schema.
# Find similar test setup
Look for test files that have identical beforeEach blocks or helper functions.
Extract the common setup into shared test utilities.
Duplication is the root of inconsistency — let Claude find the copies and consolidate them into a single source of truth.
via Claude Code
Log in to leave a comment.
Set up Claude Code as an automated reviewer in your CI pipeline — on every pull request, it reads the diff, checks for bugs, security issues, missing tests, and convention violations, then posts its findings as a PR comment. Your human reviewers get a head start because the obvious issues are already flagged before they look.
Before deploying, tell Claude to read your project — migrations, environment variables, queue workers, scheduled tasks, caching, third-party integrations — and generate a deployment checklist that's specific to your app. Not a generic "did you run migrations?" list, but one that knows YOUR infrastructure and catches the things YOUR deploy can break.
Instead of writing a README from memory or copying a template, tell Claude to read your project and generate one that's actually accurate — real setup instructions from your config, real architecture from your directory structure, real API examples from your routes, and real prerequisites from your dependency files.