refactoring-patterns

Applies systematic refactoring patterns for extracting, renaming, moving, and simplifying code with test verification.

Updated Jan 8, 2026
One-click install
npx skills add https://github.com/jayteealao/OtterStack --skill refactoring-patterns-jayteealao
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: refactoring-patterns
Source: https://github.com/jayteealao/OtterStack/tree/main/.claude/skills/refactoring-patterns
Command: npx skills add https://github.com/jayteealao/OtterStack --skill refactoring-patterns-jayteealao

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Refactoring code without a structured approach risks breaking behavior, losing test coverage, and creating unreviewable changes. This Skill provides proven patterns and safety checklists for restructuring code while preserving behavior. ## Core Features & Use Cases - Extract Patterns: Extract methods, classes, interfaces, modules, constants, and variables from bloated or duplicated code. - Rename Patterns: Safely rename variables, functions, classes, and modules with gradual deprecation strategies and dynamic-reference detection. - Move & Simplify Patterns: Relocate methods, fields, and classes between modules, and reduce complexity with guard clauses, polymorphism, and parameter objects. - Use Case: You inherit a 500-line utils.py with duplicated validation logic. Use this Skill to split it into cohesive modules, extract shared validation functions, and verify each step with tests. ## Quick Start Refactor the long process_order function in checkout.py by extracting validation, discount, and payment logic into separate methods while keeping all tests passing.

Frequently Asked Questions about refactoring-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I safely refactor code without breaking existing behavior?▼

Ensure tests exist and pass before refactoring, then make small incremental changes and run tests after each step. Commit working states frequently so any regression can be isolated and rolled back quickly.

How to extract a method from a long function in Python?▼

Identify a cohesive code block, check which local variables it uses, create a new method with those as parameters, and replace the original code with a call. Run tests immediately after the extraction to confirm behavior is unchanged.

What is the safest way to rename a function used across a codebase?▼

Use an IDE rename tool when possible, then search for dynamic references like getattr calls and string literals that tools miss. For public APIs, add the new name as an alias, deprecate the old one with warnings, and remove it after a grace period.

When should I replace conditionals with polymorphism?▼

Replace conditionals with polymorphism when switch statements or type-checking branches grow across multiple locations. Create a subclass per branch and override a shared method so new types are added without modifying existing conditional logic.

Why does refactoring fail even when tests pass?▼

Tests may miss dynamic references such as getattr calls, serialized data with old field names, environment variables, or database columns. Search for string references and check external contracts like APIs and configs before finalizing changes.

What code complexity thresholds indicate refactoring is needed?▼

Refactor when methods exceed 20 lines, cyclomatic complexity exceeds 10, functions take more than 4 parameters, nesting goes deeper than 3 levels, or classes have more than 20 methods. Tools like radon can measure these metrics in Python.