refactoring
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRefactoring Skill
重构Skill
Atomic skill for code smell detection and safe refactoring
用于代码异味检测和安全重构的原子化Skill
Skill Definition
Skill定义
yaml
skill_id: refactoring
responsibility: Single - Code smell detection and refactoring guidance
atomic: true
idempotent: trueyaml
skill_id: refactoring
responsibility: Single - Code smell detection and refactoring guidance
atomic: true
idempotent: trueParameter Schema
参数Schema
typescript
interface SkillParams {
// Required
action: 'detect' | 'plan' | 'apply' | 'validate';
code: string;
language: string;
// Optional
smell_categories?: SmellCategory[];
risk_tolerance?: 'low' | 'medium' | 'high';
test_coverage?: 'none' | 'partial' | 'full';
}
type SmellCategory = 'bloaters' | 'oo_abusers' | 'change_preventers' | 'dispensables' | 'couplers';
interface SkillResult {
smells?: CodeSmell[];
plan?: RefactoringPlan;
refactored_code?: string;
validation?: ValidationResult;
}typescript
interface SkillParams {
// 必填
action: 'detect' | 'plan' | 'apply' | 'validate';
code: string;
language: string;
// 可选
smell_categories?: SmellCategory[];
risk_tolerance?: 'low' | 'medium' | 'high';
test_coverage?: 'none' | 'partial' | 'full';
}
type SmellCategory = 'bloaters' | 'oo_abusers' | 'change_preventers' | 'dispensables' | 'couplers';
interface SkillResult {
smells?: CodeSmell[];
plan?: RefactoringPlan;
refactored_code?: string;
validation?: ValidationResult;
}Validation Rules
验证规则
yaml
input_validation:
action:
required: true
enum: [detect, plan, apply, validate]
code:
required: true
min_length: 10
max_length: 100000
language:
required: true
allowed: [typescript, javascript, python, java, csharp, go]yaml
input_validation:
action:
required: true
enum: [detect, plan, apply, validate]
code:
required: true
min_length: 10
max_length: 100000
language:
required: true
allowed: [typescript, javascript, python, java, csharp, go]Retry Logic
重试逻辑
yaml
retry_config:
max_attempts: 3
backoff:
type: exponential
initial_delay_ms: 1000
max_delay_ms: 10000
multiplier: 2
retryable_errors:
- TIMEOUT
- RATE_LIMIT
non_retryable_errors:
- INVALID_CODE
- UNSAFE_REFACTORINGyaml
retry_config:
max_attempts: 3
backoff:
type: exponential
initial_delay_ms: 1000
max_delay_ms: 10000
multiplier: 2
retryable_errors:
- TIMEOUT
- RATE_LIMIT
non_retryable_errors:
- INVALID_CODE
- UNSAFE_REFACTORINGCode Smell Catalog
代码异味目录
Bloaters
臃肿类(Bloaters)
yaml
long_method:
threshold: 20 lines
severity_scale:
20-30: low
30-50: medium
50+: high
refactorings:
- Extract Method
- Replace Temp with Query
large_class:
threshold: 200 lines
refactorings:
- Extract Class
- Extract Subclass
long_parameter_list:
threshold: 3 params
refactorings:
- Introduce Parameter Object
- Preserve Whole Objectyaml
long_method:
threshold: 20 lines
severity_scale:
20-30: low
30-50: medium
50+: high
refactorings:
- Extract Method
- Replace Temp with Query
large_class:
threshold: 200 lines
refactorings:
- Extract Class
- Extract Subclass
long_parameter_list:
threshold: 3 params
refactorings:
- Introduce Parameter Object
- Preserve Whole ObjectCouplers
耦合类(Couplers)
yaml
feature_envy:
description: "Method uses another class's data more than its own"
refactorings:
- Move Method
- Extract Method
message_chains:
pattern: "a.b().c().d().e()"
threshold: 3 calls
refactorings:
- Hide Delegateyaml
feature_envy:
description: "方法使用其他类的数据多于自身类的数据"
refactorings:
- Move Method
- Extract Method
message_chains:
pattern: "a.b().c().d().e()"
threshold: 3 calls
refactorings:
- Hide DelegateRefactoring Techniques
重构技术
yaml
extract_method:
safety: high
steps:
1. Create new method with descriptive name
2. Copy extracted code
3. Replace original with call
4. Run tests
move_method:
safety: medium
steps:
1. Copy method to target
2. Adjust references
3. Update callers
4. Remove original
5. Run testsyaml
extract_method:
safety: high
steps:
1. 创建一个具有描述性名称的新方法
2. 复制待提取的代码
3. 用方法调用替换原代码
4. 运行测试
move_method:
safety: medium
steps:
1. 将方法复制到目标类
2. 调整引用关系
3. 更新调用方代码
4. 删除原方法
5. 运行测试Usage Examples
使用示例
Smell Detection
异味检测
typescript
// Input
{
action: "detect",
code: `
class OrderProcessor {
process(order, user, config, logger, db, cache, validator) {
// 150 lines of code
}
}
`,
language: "typescript"
}
// Output
{
smells: [
{
name: "Long Parameter List",
category: "bloaters",
severity: "high",
location: "OrderProcessor.process"
},
{
name: "Long Method",
category: "bloaters",
severity: "high"
}
]
}typescript
// 输入
{
action: "detect",
code: `
class OrderProcessor {
process(order, user, config, logger, db, cache, validator) {
// 150行代码
}
}
`,
language: "typescript"
}
// 输出
{
smells: [
{
name: "Long Parameter List",
category: "bloaters",
severity: "high",
location: "OrderProcessor.process"
},
{
name: "Long Method",
category: "bloaters",
severity: "high"
}
]
}Unit Test Template
单元测试模板
typescript
describe('RefactoringSkill', () => {
describe('detect', () => {
it('should detect Long Method smell', async () => {
const code = generateLongMethod(60);
const result = await skill.execute({
action: 'detect',
code,
language: 'typescript'
});
expect(result.smells).toContainEqual(
expect.objectContaining({ name: 'Long Method' })
);
});
});
});typescript
describe('RefactoringSkill', () => {
describe('detect', () => {
it('should detect Long Method smell', async () => {
const code = generateLongMethod(60);
const result = await skill.execute({
action: 'detect',
code,
language: 'typescript'
});
expect(result.smells).toContainEqual(
expect.objectContaining({ name: 'Long Method' })
);
});
});
});Error Handling
错误处理
yaml
errors:
INVALID_CODE:
code: 400
message: "Code cannot be parsed"
recovery: "Fix syntax errors"
UNSAFE_REFACTORING:
code: 400
message: "Refactoring too risky without tests"
recovery: "Add tests or increase risk_tolerance"yaml
errors:
INVALID_CODE:
code: 400
message: "代码无法解析"
recovery: "修复语法错误"
UNSAFE_REFACTORING:
code: 400
message: "无测试情况下重构风险过高"
recovery: "添加测试或提高风险容忍度"Integration
集成
yaml
requires:
- ast_parser
- smell_detector
emits:
- smells_detected
- refactoring_planned
- refactoring_applied
consumed_by:
- 04-refactoring (bonded agent)
- 01-design-principles (for principle violations)yaml
requires:
- ast_parser
- smell_detector
emits:
- smells_detected
- refactoring_planned
- refactoring_applied
consumed_by:
- 04-refactoring (bonded agent)
- 01-design-principles (for principle violations)