code-refactor-engine
Use when refactoring implementation code to improve design while maintaining test coverage, supporting P9 recursive self-optimization
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when refactoring implementation code to improve design while maintaining test coverage, supporting P9 recursive self-optimization
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
Use when auditing the overall architecture for completeness, consistency, principle compliance, derivation chain integrity, and proposing/validating adjustments. This skill unifies the meta-verification layer and replaces architecture-self-auditor, derivation-chain-validator, adjustment-proposer, and adjustment-validator.
Use when designing system architecture, recording decisions, selecting patterns and tech stack, analyzing data flow, designing interface contracts, and applying the Strangler pattern. This skill unifies D2 Architecture Derivation Domain and replaces architecture-decision-recorder, architecture-pattern-selector, tech-stack-selector, data-flow-analyzer, interface-contract-designer, and strangler-pattern-suite.
Use when generating implementation code from interface contracts for backend (Go) and frontend (Vue/React) in any language. This skill unifies D5 Implementation Derivation Domain (L4.5→L5) and replaces contract-driven-code-generator, backend-code-generator, frontend-code-generator, and generic-code-generator.
Use when validating code and specifications against all 15 Aether constitutional principles (P0-P14), detecting principle conflicts, resolving via dynamic weighting, and enforcing mechanized constraints. This skill unifies the constitution enforcement layer and replaces constitution-validator, principle-consistency-checker, and constraint-check-runner.
Use when orchestrating complex deployments (canary, blue-green, rolling), managing releases, handling rollbacks, and enforcing change management. This skill unifies deployment operations and replaces deployment-orchestrator, rollback-manager, release-manager, and change-management.
Use when evaluating task determinism, dispatching to traditional code generators vs AI generation, enforcing contract consistency gates, and routing for confidence-based human review. This skill unifies D4.5 Generation Dispatch Domain and replaces deterministic-evaluator, code-generator-dispatcher, contract-consistency-gate, and confidence-based-reviewer.
| name | code-refactor-engine |
| description | Use when refactoring implementation code to improve design while maintaining test coverage, supporting P9 recursive self-optimization |
Refactor implementation code to improve design while maintaining test coverage. Supports P9: Recursive Self-Optimization Principle by continuously improving code quality through automated refactoring patterns.
Implementation code exists? ─────────────┐
│
Need to improve design? ─────────────────┤
├─► Use code-refactor-engine
Tests are passing? ──────────────────────┤
│
Following P9 optimization principle? ────┘
Use when:
Don't use when:
Current Code
|
v
Identify Code Smells
|
v
Select Refactoring Pattern
|
v
Apply Refactoring
|
v
Run Tests (Must Pass)
|
v
Evaluate Improvement
|
v
Commit or Revert
| Pattern | When to Use | Example |
|---|---|---|
| Extract Method | Method is too long | Break 50-line method into 3 methods |
| Extract Class | Class has multiple responsibilities | Separate UserAuth from UserProfile |
| Move Method | Method uses more features of another class | Move validateEmail to EmailValidator |
| Replace Conditional with Polymorphism | Complex switch/if-else | Replace with Strategy pattern |
| Introduce Parameter Object | Method has too many parameters | Group params into UserRegistrationRequest |
| Replace Magic Numbers | Hard-coded values | Extract MAX_RETRY_COUNT constant |
| Remove Duplication | Repeated code blocks | Extract common logic into helper method |
class UserController {
async handleRequest(req: Request, res: Response) {
const { email, password, firstName, lastName, phone, address } = req.body;
// Validation
if (!email || !email.includes('@')) {
return res.status(400).json({ error: 'Invalid email' });
}
if (!password || password.length < 8) {
return res.status(400).json({ error: 'Invalid password' });
}
if (!firstName || !lastName) {
return res.status(400).json({ error: 'Name required' });
}
// Business logic
const existingUser = await db.users.findOne({ email });
if (existingUser) {
return res.status(409).json({ error: 'Email exists' });
}
const hashedPassword = await bcrypt.hash(password, 10);
const user = await db.users.create({
email,
password: hashedPassword,
firstName,
lastName,
phone,
address,
createdAt: new Date()
});
// Response
return res.status(201).json({
id: user.id,
email: user.email,
firstName: user.firstName,
lastName: user.lastName
});
}
}
// Extracted validation logic
class UserRegistrationValidator {
validate(data: UserRegistrationRequest): ValidationResult {
const errors: string[] = [];
if (!EmailValidator.isValid(data.email)) {
errors.push('Invalid email format');
}
if (!PasswordValidator.isValid(data.password)) {
errors.push('Password must be at least 8 characters');
}
if (!data.firstName || !data.lastName) {
errors.push('First and last name are required');
}
return errors.length > 0
? ValidationResult.failure(errors)
: ValidationResult.success();
}
}
// Extracted parameter object
class UserRegistrationRequest {
constructor(
public readonly email: string,
public readonly password: string,
public readonly firstName: string,
public readonly lastName: string,
public readonly phone?: string,
public readonly address?: string
) {}
}
// Refactored controller
class UserController {
constructor(
private userService: IUserService,
private validator: UserRegistrationValidator
) {}
async register(req: Request, res: Response) {
const request = new UserRegistrationRequest(
req.body.email,
req.body.password,
req.body.firstName,
req.body.lastName,
req.body.phone,
req.body.address
);
const validation = this.validator.validate(request);
if (!validation.isValid) {
return res.status(400).json({ errors: validation.errors });
}
try {
const user = await this.userService.register(request);
return res.status(201).json(user.toResponse());
} catch (error) {
if (error instanceof DuplicateUserError) {
return res.status(409).json({ error: 'Email already registered' });
}
throw error;
}
}
}
| Smell | Detection | Refactoring |
|---|---|---|
| Long Method | > 20 lines | Extract Method |
| Large Class | > 200 lines | Extract Class |
| Primitive Obsession | Raw types for domain concepts | Introduce Value Object |
| Feature Envy | Method uses more of another class | Move Method |
| Switch Statements | Complex conditionals | Replace with Polymorphism |
| Temporary Field | Fields only used in some methods | Extract Class |
| Refused Bequest | Subclass doesn't use parent methods | Replace Inheritance with Delegation |
| Divergent Change | Class changes for different reasons | Extract Class |
| Shotgun Surgery | Change requires many small edits | Move Method, Inline Class |
| Parallel Inheritance | Mirror class hierarchies | Bridge Pattern |
class CodeRefactorEngine:
def __init__(self, code, tests):
self.code = code
self.tests = tests
self.smells = []
def analyze(self):
"""Analyze code for smells."""
self.smells = [
self.detect_long_methods(),
self.detect_large_classes(),
self.detect_duplication(),
self.detect_primitive_obsession(),
self.detect_feature_envy()
]
return self.smells
def refactor(self, smell):
"""Apply refactoring for detected smell."""
# Ensure tests pass before refactoring
assert self.run_tests() == 'pass', "Tests must pass before refactoring"
# Apply refactoring
refactored_code = self.apply_refactoring(smell)
# Ensure tests still pass
assert self.run_tests() == 'pass', "Tests must pass after refactoring"
return refactored_code
def detect_long_methods(self):
"""Detect methods longer than 20 lines."""
long_methods = []
for method in self.code.methods:
if method.line_count > 20:
long_methods.append({
'smell': 'long_method',
'method': method.name,
'lines': method.line_count,
'suggestion': 'extract_method'
})
return long_methods
def apply_refactoring(self, smell):
"""Apply appropriate refactoring pattern."""
refactorings = {
'long_method': self.extract_method,
'large_class': self.extract_class,
'duplication': self.remove_duplication,
'primitive_obsession': self.introduce_value_object,
'feature_envy': self.move_method
}
return refactorings[smell['smell']](smell)
refactoring_report:
target_file: "user-controller.ts"
analysis_date: "2025-04-24T12:00:00Z"
smells_detected:
- type: "long_method"
location: "UserController.handleRequest"
severity: "high"
lines: 45
- type: "primitive_obsession"
location: "UserController.handleRequest parameters"
severity: "medium"
refactorings_applied:
- pattern: "extract_method"
location: "UserController.handleRequest"
description: "Extracted validation logic into UserRegistrationValidator"
tests_status: "passing"
- pattern: "introduce_parameter_object"
location: "UserController.handleRequest"
description: "Created UserRegistrationRequest class"
tests_status: "passing"
metrics_before:
average_method_length: 45
class_responsibilities: 3
code_duplication: 15%
metrics_after:
average_method_length: 12
class_responsibilities: 1
code_duplication: 5%
improvement:
maintainability: "+40%"
testability: "+30%"
readability: "+50%"