Skip to content
/refactorStable

Structural refactoring with dependency analysis, safe rename/extract/move operations, and regression verification.

RefactoringArchitectureΒ· 2 min read

Quick import: Download the .md file and save it to .claude/commands/ (Claude Code), .cursorrules (Cursor), or paste as a system prompt in ChatGPT, Gemini, or any LLM API.

#What it does

The /refactor skill performs deep structural refactoring -- extracting modules, renaming across the codebase, moving responsibilities between layers, and restructuring file organization. Unlike /simplify which does a light cleanup pass, /refactor handles architectural changes that touch many files and require dependency-aware transformations.

#How to use

bash
/refactor Extract the auth logic from the API routes into a shared middleware
bash
/refactor Rename "User" to "Account" across the entire codebase
bash
/refactor Split the monolithic utils.ts into domain-specific utility modules

#Workflow

  1. Map -- Build a dependency graph of the target code. Identify every import, export, type reference, and test that touches the area being refactored
  2. Strategize -- Choose the refactoring pattern: Extract Module, Move Function, Rename Symbol, Inline Abstraction, Replace Inheritance with Composition, or Strangler Fig
  3. Transform -- Apply the refactoring in small, atomic steps. Each step preserves existing behavior -- the tests should pass after every individual change
  4. Update References -- Find and update every import path, type reference, config entry, and documentation reference affected by the change
  5. Verify -- Run the full test suite. Check that no dead imports, broken references, or orphaned files remain
  6. Report -- Summarize what changed: files created, modified, deleted, and renamed. Flag any manual follow-ups needed

#Supported patterns

  • Extract -- Pull a function, class, or module into its own file with proper exports
  • Rename -- Rename a symbol across every file, test, config, and documentation reference
  • Move -- Relocate code between directories or layers while updating all imports
  • Inline -- Collapse an unnecessary abstraction back into its callers
  • Split -- Break a large file into focused, single-responsibility modules
  • Consolidate -- Merge scattered related code into a cohesive module

#Example

bash
> /refactor Extract database queries from route handlers into repository classes
 
## Dependency Map
- 12 route handlers contain inline SQL queries
- 4 shared query patterns identified (CRUD for users, orders, products, sessions)
- 23 test files reference route handlers directly
 
## Plan
1. Create src/repositories/ directory
2. Extract UserRepository, OrderRepository, ProductRepository, SessionRepository
3. Define repository interfaces for each
4. Update route handlers to inject repositories
5. Update tests to mock repository interfaces
6. Verify: all 47 tests pass, no inline SQL remains in routes
Orel OhayonΒ·
View all skills