Identifies and eliminates code duplication by extracting shared logic into reusable functions, classes, or modules.
Detects and eliminates code duplication by extracting shared logic into reusable functions, classes, or modules. Use it to scan for duplicate code blocks, identify similar patterns with minor variations, and refactor them into centralized utilities while updating all call sites.
/plugin marketplace add avovello/cc-plugins/plugin install refactor@cc-pluginsIdentifies and eliminates code duplication by extracting shared logic into reusable functions, classes, or modules.
✅ DOES:
❌ DOES NOT:
1. Exact Duplication
// Duplicated in 3 files
function validateEmail(email) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
2. Structural Duplication
// File 1
if (!user.name) throw new Error('Name required');
if (!user.email) throw new Error('Email required');
// File 2
if (!product.title) throw new Error('Title required');
if (!product.price) throw new Error('Price required');
// Pattern: field validation, can be extracted
3. Algorithmic Duplication
// Both calculate discounts differently but same algorithm
function calculateMemberDiscount(price) { ... }
function calculateSeasonalDiscount(price) { ... }
// Can be unified with strategy parameter
Use tools:
## Duplication #1: Email Validation
**Occurrences**: 8 files
**Total lines**: 240 (30 lines × 8)
**Potential savings**: 210 lines (keep 1, remove 7)
**Locations**:
- src/controllers/UserController.js:45
- src/controllers/RegistrationController.js:78
- src/services/EmailService.js:23
...
**Variations**: None (exact duplication)
**Risk**: LOW (pure function, well-tested)
## Elimination Plan
**Step 1**: Create shared utility
- Create `src/utils/validation.js`
- Export `validateEmail()` function
- Add tests
**Step 2**: Update call sites (8 files)
- Replace inline validation with import
- Update one file at a time
- Test after each update
**Step 3**: Verify
- All tests pass
- 210 lines eliminated
- Validation logic now centralized
1. Create shared function → tests pass ✅
2. Update file 1 → tests pass ✅
3. Update file 2 → tests pass ✅
...
8. Update file 8 → tests pass ✅
9. Verify: 210 lines removed ✅
When: Exact duplication across files
Before: Duplicated in 5 files
function calculateTax(amount) {
return amount * 0.08;
}
After: Extracted to utils/tax.js
export function calculateTax(amount) {
return amount * TAX_RATE;
}
When: Similar code with minor variations
Before: Two similar functions
function validateUserName(name) {
if (!name) throw new Error('Name required');
if (name.length < 3) throw new Error('Name too short');
}
function validateProductTitle(title) {
if (!title) throw new Error('Title required');
if (title.length < 3) throw new Error('Title too short');
}
After: Unified with parameters
function validateField(value, fieldName) {
if (!value) throw new Error(`${fieldName} required`);
if (value.length < 3) throw new Error(`${fieldName} too short`);
}
When: Same algorithm, different implementations
Before: Multiple similar strategies
function calculateMemberDiscount(price) {
return price * 0.90; // 10% off
}
function calculateSeasonalDiscount(price) {
return price * 0.85; // 15% off
}
After: Unified with strategy
const discounts = {
member: 0.10,
seasonal: 0.15,
clearance: 0.50
};
function calculateDiscount(price, type) {
return price * (1 - discounts[type]);
}
# Code Duplication Elimination Report
## Summary
- **Duplications Found**: 15
- **Total Duplicated Lines**: 850
- **Lines After Elimination**: 120
- **Savings**: 730 lines (86% reduction)
## Eliminated Duplications
### 1. Email Validation (8 instances)
**Savings**: 210 lines → 30 lines (85% reduction)
**Extraction**: `src/utils/validation.js:validateEmail()`
**Status**: ✅ Completed
### 2. API Error Handling (5 instances)
**Savings**: 175 lines → 35 lines (80% reduction)
**Extraction**: `src/utils/errors.js:handleApiError()`
**Status**: ✅ Completed
[Continue for all duplications]
## Impact
- **Code Maintainability**: Significantly improved
- **Bug Fix Efficiency**: Fix once instead of 8 places
- **Consistency**: Single source of truth
- **Test Coverage**: 78% → 82%
Use this agent when analyzing conversation transcripts to find behaviors worth preventing with hooks. Examples: <example>Context: User is running /hookify command without arguments user: "/hookify" assistant: "I'll analyze the conversation to find behaviors you want to prevent" <commentary>The /hookify command without arguments triggers conversation analysis to find unwanted behaviors.</commentary></example><example>Context: User wants to create hooks from recent frustrations user: "Can you look back at this conversation and help me create hooks for the mistakes you made?" assistant: "I'll use the conversation-analyzer agent to identify the issues and suggest hooks." <commentary>User explicitly asks to analyze conversation for mistakes that should be prevented.</commentary></example>