Code Refactoring Patterns
This skill should be used when the user asks to "refactor large files", "split code into smaller pieces", "extract methods", "how should I refactor", "improve code structure", "break down large functions", or discusses strategies for dividing monolithic code into smaller, maintainable components.
What this skill does
# Code Refactoring Patterns
## Purpose
This skill provides guidance on refactoring large, complex code files into smaller, focused, and maintainable components. It covers fundamental refactoring principles, extraction strategies, and patterns that apply across programming languages and frameworks.
## When to Use
Use this skill when analyzing code structure, planning refactoring strategies, or deciding how to split large files. It provides language-agnostic principles that work whether refactoring Laravel controllers, React components, Node.js services, or any codebase.
## Core Refactoring Principles
### 1. Single Responsibility Principle (SRP)
Each function, class, or module should have one reason to change. Identify when code violates SRP:
- **Controllers handling validation, business logic, and response formatting** → Extract validation and business logic to separate classes
- **Components managing state, data fetching, and rendering** → Extract hooks and services
- **Service classes with 15+ public methods** → Break into focused services
- **Functions doing multiple operations** → Extract each operation to its own function
**Question to ask:** "If this code needs to change, how many different reasons could require that change?" More than one reason signals SRP violation.
### 2. Extractable Boundaries
Identify clear extraction boundaries by looking for:
- **Distinct workflows**: Methods performing completely different operations (e.g., user creation separate from email sending)
- **Reusable logic**: Code that appears in multiple places or could be used elsewhere
- **High complexity**: Methods exceeding 15-20 lines or with nested conditionals
- **Clear input/output**: Methods with well-defined inputs and outputs (easier to extract)
- **Testability**: Logic that would benefit from independent testing
**Extraction pattern:**
```
1. Identify the boundary (start/end of related operations)
2. Extract to new method/function
3. Verify no side effects or tight coupling
4. Test independently
5. Update references
```
### 3. Complexity Metrics
Recognize complexity that signals refactoring need:
- **Cyclomatic Complexity > 10**: Multiple branches, nested conditions → Extract conditional logic to separate methods
- **Method length > 20 lines**: Likely handling multiple concerns → Break into smaller methods
- **Nesting depth > 3 levels**: Hard to follow logic → Extract inner logic to separate methods
- **Parameter count > 4**: Hard to understand and test → Group parameters into objects
- **Method with many local variables**: Suggests multiple concerns → Extract to separate methods
### 4. Naming and Intent
After extraction, ensure clear naming:
- **Method names should describe intent**: `validateUserEmail()` not `check()`, `processPayment()` not `do()`
- **Extract Guard Clauses**: Early returns for edge cases make the happy path clear
- **Extract Error Handling**: Separate validation from business logic
- **Extract Loops**: Extract loop bodies to named methods that describe the operation
**Example**:
```php
// Before: Hard to understand at a glance
public function createUser($data) {
if (!$data['email']) return false;
if (User::where('email', $data['email'])->exists()) return false;
// ... 30 more lines
}
// After: Clear intent with extracted boundaries
public function createUser($data) {
if (!$this->validateUserData($data)) return false;
if ($this->userExists($data['email'])) return false;
return $this->storeUser($data);
}
```
## Extraction Patterns by Language
### PHP / Laravel
**Extract CRUD operations into Action classes:**
- `CreateUserAction` - Only handles creation logic
- `UpdateUserAction` - Only handles updates
- `DeleteUserAction` - Only handles deletion
- `GetUserAction` - Only handles retrieval with related data
**Extract validation into separate methods or Form Requests:**
- `ValidateUserInput` method or Form Request class
- Keep business logic separate from validation
**Extract queries into scopes and methods:**
- `scopeActive()` → `User::query()->active()`
- `scopeByEmail()` → `User::query()->byEmail($email)`
**Extract relationships into dedicated classes:**
- Models with complex relationships → Separate trait files
- Resource formatting → Dedicated Resource class
### React / Vue
**Extract large components into smaller pieces:**
- Container components (data management) separate from presentational components (rendering)
- Extract hooks for reusable logic
- Extract smaller, focused sub-components
**Extract hooks for reusable logic:**
- `useUserPermissions()` → Encapsulates permission checking
- `useFormValidation()` → Handles form state and validation
- `useDataFetching()` → Wraps React Query or SWR
**Extract context providers:**
- Theme context separate from Auth context
- Each context for one concern
### Node.js / TypeScript
**Extract service methods into focused services:**
- `UserService` → User-specific operations
- `EmailService` → Email operations
- `PaymentService` → Payment operations
**Extract helper/utility functions:**
- `validateEmail()`, `hashPassword()`, `formatResponse()`
- Keep pure functions in separate files
**Extract data access patterns:**
- Database queries → Repository pattern or separate data access file
- API calls → Separate API client file
## Refactoring Workflow
### Step 1: Analyze Current Code
Read the file and identify:
- Current responsibilities (what does this code do?)
- Violation areas (where are multiple concerns mixed?)
- Extraction candidates (which parts could be separate?)
- Dependencies (what does extracted code depend on?)
### Step 2: Design Target Structure
Plan the refactoring:
- How many files/classes should be created?
- What will each contain?
- How will they interact?
- What's the folder structure?
### Step 3: Extract with Care
- Extract one concern at a time
- Update references to extracted code
- Verify functionality still works
- Run tests (if available)
### Step 4: Validate
- Does each extracted component have single responsibility?
- Are dependencies clear and minimal?
- Is the code easier to understand and test?
- Can extracted code be reused elsewhere?
## Common Extraction Scenarios
### Scenario 1: Large Controller Class
**Problem:** 50+ line controller method doing validation, business logic, and response formatting
**Solution:**
1. Extract validation to Form Request or validator method
2. Extract business logic to Action class
3. Keep controller thin (3-5 lines)
### Scenario 2: Complex Component
**Problem:** React component with 200+ lines doing data fetching, state management, and rendering
**Solution:**
1. Extract data fetching to custom hook
2. Extract child components for different sections
3. Extract form logic to separate hook
4. Component becomes 30-40 lines focused on layout
### Scenario 3: God Service Class
**Problem:** Service class with 30+ public methods handling multiple unrelated operations
**Solution:**
1. Group related methods into separate service classes
2. Service A: `UserCreation` operations
3. Service B: `UserNotification` operations
4. Service C: `UserPermissions` operations
5. Update references to use appropriate service
### Scenario 4: Conditional Logic
**Problem:** Method with 5+ nested if/else levels
**Solution:**
1. Extract guard clauses to top (early returns)
2. Extract condition checking to named methods
3. Extract branch logic to separate methods
4. Result: Clear happy path with early exits
## Patterns to Follow
### Action Pattern (Preferred for Business Logic)
**Characteristics:**
- Single `handle()` method with clear purpose
- Dependencies injected via constructor
- Reusable from commands, controllers, jobs, API requests
- Easy to test
**When to use:** Any complex business logic (user creation, payment processing, data transformation)
### Hook Pattern (React/Vue)
**Characteristics:**
- Reusable logic encapsulated
- Composed into components
- Separate concerns (state, effectsRelated in Code Review
gstack
IncludedFast headless browser for QA testing and site dogfooding. Navigate pages, interact with elements, verify state, diff before/after, take annotated screenshots, test responsive layouts, forms, uploads, dialogs, and capture bug evidence. Use when asked to open or test a site, verify a deployment, dogfood a user flow, or file a bug with screenshots. (gstack)
startup-due-diligence
IncludedLegal due diligence review for seed-stage and Series A startups (US, Delaware C-Corp focus). Supports both investor and founder perspectives. Capabilities include: (1) Interactive document review and issue spotting; (2) Document request list generation; (3) Cap table and SAFE/convertible note analysis; (4) Red flag identification with severity ratings; (5) Diligence report generation. TRIGGERS: due diligence, DD, startup investment, cap table review, Series A, seed round, investor diligence, legal review startup, SAFE analysis, convertible note, 409A, founder vesting.
interview-master
IncludedThis skill should be used when the user asks to "generate interview questions", "prepare for interview", "optimize resume", "conduct mock interview", "analyze git commits for resume", "generate resume from code", "review my resume", or mentions interview preparation, career assistance, or extracting project experience from git history. Provides comprehensive interview and career development guidance for both job seekers and interviewers.
fix-issue
IncludedFixes GitHub issues using parallel analysis agents for root cause investigation, code exploration, and regression detection. Reads issue context from gh CLI, searches codebase and memory for related patterns, generates a fix with tests, and links the resolution back to the issue via PR. Includes prevention analysis to avoid recurrence. Use when debugging errors, resolving regressions, fixing bugs, or triaging issues.
sf-apex
IncludedGenerates and reviews Salesforce Apex code with 150-point scoring. TRIGGER when: user writes, reviews, or fixes Apex classes, triggers, test classes, batch/queueable/schedulable jobs, or touches .cls/.trigger files. DO NOT TRIGGER when: LWC JavaScript (use sf-lwc), Flow XML (use sf-flow), SOQL-only queries (use sf-soql), or non-Salesforce code.
swift-development
IncludedComprehensive Swift development for building, testing, and deploying iOS/macOS applications. Use when Claude needs to: (1) Build Swift packages or Xcode projects from command line, (2) Run tests with XCTest or Swift Testing framework, (3) Manage iOS simulators with simctl, (4) Handle code signing, provisioning profiles, and app distribution, (5) Format or lint Swift code with SwiftFormat/SwiftLint, (6) Work with Swift Package Manager (SPM), (7) Implement Swift 6 concurrency patterns (async/await, actors, Sendable), (8) Create SwiftUI views with MVVM architecture, (9) Set up Core Data or SwiftData persistence, or any other Swift/iOS/macOS development tasks.