Extract function from repeated code
Extract repeated email validation and domain extraction logic into separate functions for improved maintainability and reusability
/refactor-code src/utils/processUsers.ts --extract-function/refactor [options] <file_or_selection>// Before
function processUsers(users) {
for (let user of users) {
if (user.email && user.email.includes('@')) {
user.isValid = true;
user.domain = user.email.split('@')[1];
} else {
user.isValid = false;
user.domain = null;
}
}
}
// After refactoring with --extract-function
function validateEmail(email) {
return email && email.includes('@');
}
function extractDomain(email) {
return email.split('@')[1];
}
function processUsers(users) {
for (let user of users) {
if (validateEmail(user.email)) {
user.isValid = true;
user.domain = extractDomain(user.email);
} else {
user.isValid = false;
user.domain = null;
}
}
}# Before
def calculate_discount(price, customer_type):
if customer_type == "premium":
return price * 0.2
elif customer_type == "regular":
return price * 0.1
else:
return 0
# After refactoring with --extract-constant
PREMIUM_DISCOUNT_RATE = 0.2
REGULAR_DISCOUNT_RATE = 0.1
PREMIUM_CUSTOMER_TYPE = "premium"
REGULAR_CUSTOMER_TYPE = "regular"
def calculate_discount(price, customer_type):
if customer_type == PREMIUM_CUSTOMER_TYPE:
return price * PREMIUM_DISCOUNT_RATE
elif customer_type == REGULAR_CUSTOMER_TYPE:
return price * REGULAR_DISCOUNT_RATE
else:
return 0// Before (ES5 style)
function getUserNames(users) {
var names = [];
for (var i = 0; i < users.length; i++) {
if (users[i].active) {
names.push(users[i].name);
}
}
return names;
}
// After refactoring with --modernize
function getUserNames(users) {
return users
.filter(user => user.active)
.map(user => user.name);
}{
"rules": {
"maxFunctionLength": 20,
"maxParameterCount": 4,
"enforceConstantExtraction": true,
"modernizeFeatures": true
},
"exclude": [
"node_modules/**",
"dist/**",
"*.test.js"
],
"backup": {
"enabled": true,
"directory": ".refactor-backups"
}
}Extract repeated email validation and domain extraction logic into separate functions for improved maintainability and reusability
/refactor-code src/utils/processUsers.ts --extract-functionReplace magic numbers and strings with named constants (PREMIUM_DISCOUNT_RATE, REGULAR_DISCOUNT_RATE) for better code clarity
/refactor-code src/services/discount.py --extract-constantUpdate legacy ES5 code to modern ES2024 features including arrow functions, array methods, and destructuring while maintaining functionality
/refactor-code src/utils/helpers.js --modernize --javascriptPreview refactoring changes without applying, run tests before and after, and create backup for safe code transformation
/refactor-code src/api/users.ts --extract-function --dry-run --test-firstRefactor with interactive prompts for each change, allowing selective approval of renaming and simplification operations
/refactor-code src/components/UserList.tsx --rename --simplify --interactiveApply performance optimizations including memoization, lazy loading, batch operations, and async conversion for improved efficiency
/refactor-code src/services/dataProcessor.ts --performanceApply Python-specific refactoring patterns with constant extraction, automatic backup creation, and Python best practices
/refactor-code src/utils/calculations.py --extract-constant --python --backupRefactoring breaks tests or changes functional behavior unexpectedly
Use --test-first to run tests before and after. Enable --dry-run to preview changes. Create git commit before refactoring: git commit -am 'pre-refactor'.
Modernize flag introduces breaking changes or incompatible syntax
Specify language version explicitly: --javascript --es2020. Check Node.js/browser target compatibility. Use --interactive to approve each modernization.
Refactored code fails linting or type checking after transformation
Run linter before refactoring: npm run lint. Enable --backup to preserve original. Fix type errors: tsc --noEmit before applying changes.
Command fails with parse errors on valid source code files
Verify language version matches codebase: check package.json engines. Update parser: npm update @babel/parser. Use --language flag explicitly for mixed-syntax files.
Large codebase refactoring times out or consumes excessive memory
Process files incrementally: /refactor src/module1 first. Increase Node.js memory: NODE_OPTIONS=--max-old-space-size=4096. Use --exclude for node_modules.
Refactoring introduces bugs or breaks existing functionality
Run test suite before and after refactoring. Use --dry-run to preview changes. Apply refactoring incrementally. Review changes carefully before committing.
Refactoring misses related code or creates incomplete changes
Use --include-related flag to refactor dependent code. Review impact analysis for missed files. Manually identify related code. Run refactoring in multiple passes.
Refactoring process is slow or consumes too many resources
Refactor specific files with --scope flag. Exclude large third-party libraries. Run refactoring incrementally. Check system resources are sufficient.
Loading reviews...
Intelligent code refactoring command that analyzes code structure and applies best practices for improved maintainability and performance
Deploy 100 specialized sub-agents for comprehensive enterprise-grade security, performance, and optimization audit of production codebase
Advanced performance optimization with bottleneck analysis, memory profiling, and automated improvements
Growing community of AI engineers actively building with Claude
Live in 5 minutes • Growing community