| name | refactor |
| description | Safely refactor code while preserving behavior |
Safe Refactoring Skill
When to Use
Use this skill when you need to refactor existing code, especially when extracting helpers, reorganizing functions, or improving structure without changing behavior.
Before Making Changes
- Identify existing tests for the code being modified
- If no tests exist, write tests first to establish baseline behavior
- Run tests to confirm they pass before changes
- Understand the current behavior completely
During Refactoring
- Make small, incremental changes
- Preserve all existing functionality
- Extract helpers or reorganize without changing behavior
- Add clear docstrings to new functions
After Changes
- Run tests again to verify behavior is preserved
- Review the diff to confirm only intended changes
- Suggest any additional tests if coverage gaps exist
Example: Extracting a Helper Function
When extracting logic into a helper:
def process_data(items):
prepared = [transform(i) for i in items if valid(i)]
return calculate(prepared)
def prepare_items(items):
"""Prepare items for processing by filtering and transforming."""
return [transform(i) for i in items if valid(i)]
def process_data(items):
prepared = prepare_items(items)
return calculate(prepared)
Always verify tests pass after extraction.