Agent skill
refactoring
Safe code refactoring with test protection and incremental changes. Improves structure without changing behavior. Use when extracting methods, reducing duplication, or restructuring code.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/refactoring-1ambda-dataops-platform-4
SKILL.md
Refactoring
Behavior-preserving code transformation protected by tests.
Core Principle
Tests must pass before AND after every change.
When to Use
- Reducing duplication (DRY)
- Extracting methods/classes
- Improving naming and readability
- Simplifying complex conditionals
- Preparing code for new features
MCP Workflow
# 1. Understand current structure
serena.get_symbols_overview(relative_path="target/file")
# 2. Check all usages before change
serena.find_referencing_symbols(name_path="TargetClass/method")
# 3. Verify test coverage
serena.search_for_pattern("test.*TargetClass", relative_path="test/")
# 4. Get target code
serena.find_symbol(name_path="TargetClass/method", include_body=True)
# 5. Safe rename
serena.rename_symbol(name_path="oldName", relative_path="file", new_name="newName")
Workflow
1. Prepare
- Ensure tests exist and pass
- Identify refactoring scope
- Plan incremental steps
2. Refactor (small steps)
For each change:
1. Make one refactoring move
2. Run tests immediately
3. If PASS: commit
4. If FAIL: revert, try smaller step
3. Verify
- Run full test suite
- Review final structure
- Update docs if needed
Common Patterns
Extract Method
When: Function too long or has multiple responsibilities
BEFORE:
processOrder(order):
# validate (10 lines)
# calculate shipping (10 lines)
# save (5 lines)
AFTER:
processOrder(order):
validateOrder(order)
shipping = calculateShipping(order)
repository.save(order.copy(shipping=shipping))
Extract Class
When: Class has too many responsibilities
OrderService with 6 methods
-> OrderService (orchestration)
-> OrderCalculator (calculations)
-> OrderNotifier (notifications)
Replace Conditional with Polymorphism
When: Repeated switch/if-else on type
BEFORE:
switch customer.type:
case "REGULAR": return total * 0.05
case "PREMIUM": return total * 0.10
AFTER:
interface CustomerType { calculateDiscount(total) }
class RegularCustomer implements CustomerType { ... }
class PremiumCustomer implements CustomerType { ... }
Simplify Conditionals
BEFORE:
if a:
if b:
if c:
return true
return false
AFTER:
return a and b and c
Output Format
## Refactoring: [Target]
| Item | Value |
|------|-------|
| Target | ClassName.method |
| Type | Extract / Rename / Simplify |
| Usages | N references |
| Tests | X tests covering |
### Steps
1. [Description] - Tests: PASS
2. [Description] - Tests: PASS
### Result
Files: N | Changes: +X/-Y | Tests: PASS
Safety Checklist
- Tests exist and pass before starting
- Each step is independently committable
- No behavior changes
- All references updated
- Tests still pass after completion
When NOT to Refactor
- No test coverage (add tests first)
- Under time pressure (do minimal fix)
- Code is about to be deleted
- Not sure about desired outcome
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?