home / skills / dasien / claudemultiagenttemplate / code-refactoring

This skill refactors Python code to improve structure and readability while preserving behavior, guiding incremental changes and ensuring tests exist.

npx playbooks add skill dasien/claudemultiagenttemplate --skill code-refactoring

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.6 KB
---
name: "Code Refactoring"
description: "Improve code structure, readability, and maintainability without changing external behavior through systematic refactoring"
category: "implementation"
required_tools: ["Read", "Write", "Edit", "MultiEdit", "Grep", "Glob"]
---

# Code Refactoring

## Purpose
Improve code structure, readability, and maintainability without changing its external behavior or functionality.

## When to Use
- Code is hard to understand or modify
- Duplicated code exists
- Functions are too long or complex
- Code smells are present
- Preparing for new features

## Key Capabilities
1. **Extract Method** - Break long functions into smaller pieces
2. **Rename** - Improve variable/function names for clarity
3. **Remove Duplication** - Consolidate repeated code

## Approach
1. Identify code that needs improvement
2. Ensure tests exist before refactoring
3. Make small, incremental changes
4. Run tests after each change
5. Commit working states frequently

## Example
**Before**:
````python
def process(data):
    result = []
    for item in data:
        if item > 0 and item < 100 and item % 2 == 0:
            result.append(item * 2)
    return result
````

**After**:
````python
def is_valid_even_number(n):
    return 0 < n < 100 and n % 2 == 0

def process(data):
    valid_numbers = filter(is_valid_even_number, data)
    return [n * 2 for n in valid_numbers]
````

## Best Practices
- ✅ Always have tests before refactoring
- ✅ Make small, incremental changes
- ✅ Run tests after each change
- ❌ Avoid: Refactoring and adding features simultaneously

Overview

This skill performs systematic code refactoring to improve structure, readability, and maintainability without changing external behavior. It focuses on identifying smells, extracting methods, renaming elements, and consolidating duplication. The goal is safer, incremental improvements backed by tests and frequent commits.

How this skill works

The skill inspects source files to locate long functions, duplicated blocks, unclear names, and other code smells. It suggests and applies patterns like Extract Method, Rename, and Remove Duplication while preserving program behavior. Each change is made in small steps, with tests run after each edit and commits created at working checkpoints.

When to use it

  • Code is hard to read, navigate, or modify
  • Repeated logic appears in multiple places
  • Functions or classes are excessively long or complex
  • Preparing the codebase for new features or performance changes
  • Before adding tests or improving existing tests

Best practices

  • Ensure comprehensive tests exist before refactoring
  • Make small, incremental changes and run tests after each step
  • Prefer clear, descriptive names over clever ones
  • Extract cohesive helper functions to reduce cognitive load
  • Avoid adding new features during a refactor to reduce risk

Example use cases

  • Break a monolithic data-processing function into focused helpers for readability
  • Rename ambiguous variables and methods to reflect intent across a module
  • Replace repeated conditional blocks with a shared utility function
  • Simplify complex loops with comprehensions or iterator helpers
  • Introduce small, targeted unit tests while refactoring to protect behavior

FAQ

Will refactoring change behavior?

Refactoring aims to preserve external behavior; tests verify that behavior remains unchanged.

How large should each change be?

Keep changes small and focused—one refactor per commit when possible—to make rollbacks and reviews easier.