refactoring-patterns
关于
This Claude Skill applies safe refactoring patterns to improve code structure while preserving behavior, ideal for reducing technical debt and enhancing maintainability. It enforces a disciplined workflow where developers make small, test-verified changes and commit progress incrementally. Key features include a guaranteed safe refactoring cycle and integration with code review and quality analysis agents.
快速安装
Claude Code
推荐/plugin add https://github.com/proffesor-for-testing/agentic-qegit clone https://github.com/proffesor-for-testing/agentic-qe.git ~/.claude/skills/refactoring-patterns在 Claude Code 中复制并粘贴此命令以安装该技能
技能文档
Refactoring Patterns
<default_to_action> When refactoring:
- ENSURE tests pass (never refactor without tests)
- MAKE small change (one refactoring at a time)
- RUN tests (must stay green)
- COMMIT (save progress)
- REPEAT
Safe Refactoring Cycle:
npm test # Green ✅
# Make ONE small change
npm test # Still green ✅
git commit -m "refactor: extract calculateTotal"
# Repeat
Code Smells → Refactoring:
| Smell | Refactoring |
|---|---|
| Long method (>20 lines) | Extract Method |
| Large class | Extract Class |
| Long parameter list (>3) | Introduce Parameter Object |
| Duplicated code | Extract Method/Class |
| Complex conditional | Decompose Conditional |
| Magic numbers | Named Constants |
| Nested loops | Replace Loop with Pipeline |
NEVER REFACTOR:
- Without tests (write tests first)
- When deadline is tomorrow
- Code you don't understand
- Code that works and won't be touched </default_to_action>
Quick Reference Card
Common Refactorings
| Pattern | Before | After |
|---|---|---|
| Extract Method | 50-line function | 5 small functions |
| Extract Class | Class doing 5 things | 5 single-purpose classes |
| Parameter Object | fn(a,b,c,d,e,f) | fn(options) |
| Replace Conditional | if (type === 'a') {...} | Polymorphism |
| Pipeline | Nested loops | .filter().map().reduce() |
The Rule of Three
- First time → Just do it
- Second time → Wince and duplicate
- Third time → Refactor
Key Patterns
Extract Method
// Before: Long method
function processOrder(order) {
// 50 lines of validation, calculation, saving, emailing...
}
// After: Clear responsibilities
function processOrder(order) {
validateOrder(order);
const pricing = calculatePricing(order);
const saved = saveOrder(order, pricing);
sendConfirmationEmail(saved);
return saved;
}
Replace Loop with Pipeline
// Before
let results = [];
for (let item of items) {
if (item.inStock) {
results.push(item.name.toUpperCase());
}
}
// After
const results = items
.filter(item => item.inStock)
.map(item => item.name.toUpperCase());
Decompose Conditional
// Before
if (order.total > 1000 && customer.isPremium && allInStock(order)) {
return 'FREE_SHIPPING';
}
// After
function isEligibleForFreeShipping(order, customer) {
return isLargeOrder(order) &&
isPremiumCustomer(customer) &&
allInStock(order);
}
Refactoring Anti-Patterns
| ❌ Anti-Pattern | Problem | ✅ Better |
|---|---|---|
| Without tests | No safety net | Write tests first |
| Big bang | Rewrite everything | Small incremental steps |
| For perfection | Endless tweaking | Good enough, move on |
| Premature abstraction | Pattern not clear yet | Wait for Rule of Three |
| During feature work | Mixed changes | Separate commits |
Agent Integration
// Detect code smells
const smells = await Task("Detect Code Smells", {
source: 'src/services/',
patterns: ['long-method', 'large-class', 'duplicate-code']
}, "qe-quality-analyzer");
// Safe refactoring with test verification
await Task("Verify Refactoring", {
beforeCommit: 'abc123',
afterCommit: 'def456',
expectSameBehavior: true
}, "qe-test-executor");
Agent Coordination Hints
Memory Namespace
aqe/refactoring/
├── smells/* - Detected code smells
├── suggestions/* - Refactoring recommendations
├── verifications/* - Behavior preservation checks
└── history/* - Refactoring log
Fleet Coordination
const refactoringFleet = await FleetManager.coordinate({
strategy: 'refactoring',
agents: [
'qe-quality-analyzer', // Identify targets
'qe-test-generator', // Add safety tests
'qe-test-executor', // Verify behavior
'qe-test-refactorer' // TDD refactor phase
],
topology: 'sequential'
});
Related Skills
- tdd-london-chicago - TDD refactor phase
- code-review-quality - Review refactored code
- xp-practices - Collective ownership
Remember
Refactoring is NOT:
- Adding features
- Fixing bugs
- Performance optimization
- Rewriting from scratch
Refactoring IS:
- Improving structure
- Making code clearer
- Reducing complexity
- Removing duplication
- Without changing behavior
Always have tests. Always take small steps. Always keep tests green.
GitHub 仓库
相关推荐技能
code-review-quality
其他这是一个专注于代码质量、可测试性和可维护性的上下文驱动代码审查技能。它能在审查代码、提供反馈或建立审查实践时,自动从质量、安全、性能和测试覆盖度等多维度进行分析。该技能通过优先级分类、提问式反馈和提供上下文建议,帮助开发者进行高效、有建设性的代码评审。
tdd-london-chicago
其他该Skill帮助开发者在测试驱动开发中选择伦敦学派(基于模拟)或芝加哥学派(基于状态)的测试风格。它能根据代码类型(领域逻辑或外部依赖)推荐合适的测试方法,并指导完整的“红-绿-重构”TDD周期。适用于实践TDD或为特定上下文选择测试策略的开发场景。
content-collections
元Content Collections 是一个 TypeScript 优先的构建工具,可将本地 Markdown/MDX 文件转换为类型安全的数据集合。它专为构建博客、文档站和内容密集型 Vite+React 应用而设计,提供基于 Zod 的自动模式验证。该工具涵盖从 Vite 插件配置、MDX 编译到生产环境部署的完整工作流。
creating-opencode-plugins
元该Skill为开发者创建OpenCode插件提供指导,涵盖命令、文件、LSP等25+种事件类型。它详细说明了插件结构、事件API规范及JavaScript/TypeScript实现模式,帮助开发者构建事件驱动的模块。适用于需要拦截操作、扩展功能或自定义AI助手行为的插件开发场景。
