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.
Provides refactoring guidance for splitting large files and extracting methods. Triggers when users request refactoring help, mention splitting code, or discuss improving code structure.
/plugin marketplace add MaxBoiko21/claude-plugins-marketplace/plugin install code-splitter@own-plugins-marketplaceThis skill inherits all available tools. When active, it can use any tool Claude has access to.
references/extraction-patterns.mdThis 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.
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.
Each function, class, or module should have one reason to change. Identify when code violates SRP:
Question to ask: "If this code needs to change, how many different reasons could require that change?" More than one reason signals SRP violation.
Identify clear extraction boundaries by looking for:
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
Recognize complexity that signals refactoring need:
After extraction, ensure clear naming:
validateUserEmail() not check(), processPayment() not do()Example:
// 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);
}
Extract CRUD operations into Action classes:
CreateUserAction - Only handles creation logicUpdateUserAction - Only handles updatesDeleteUserAction - Only handles deletionGetUserAction - Only handles retrieval with related dataExtract validation into separate methods or Form Requests:
ValidateUserInput method or Form Request classExtract queries into scopes and methods:
scopeActive() → User::query()->active()scopeByEmail() → User::query()->byEmail($email)Extract relationships into dedicated classes:
Extract large components into smaller pieces:
Extract hooks for reusable logic:
useUserPermissions() → Encapsulates permission checkinguseFormValidation() → Handles form state and validationuseDataFetching() → Wraps React Query or SWRExtract context providers:
Extract service methods into focused services:
UserService → User-specific operationsEmailService → Email operationsPaymentService → Payment operationsExtract helper/utility functions:
validateEmail(), hashPassword(), formatResponse()Extract data access patterns:
Read the file and identify:
Plan the refactoring:
Problem: 50+ line controller method doing validation, business logic, and response formatting
Solution:
Problem: React component with 200+ lines doing data fetching, state management, and rendering
Solution:
Problem: Service class with 30+ public methods handling multiple unrelated operations
Solution:
UserCreation operationsUserNotification operationsUserPermissions operationsProblem: Method with 5+ nested if/else levels
Solution:
Characteristics:
handle() method with clear purposeWhen to use: Any complex business logic (user creation, payment processing, data transformation)
Characteristics:
When to use: Reusable component logic (form handling, data fetching, permissions)
Characteristics:
When to use: Domain-specific operations (UserService, EmailService, PaymentService)
Extract code when you see:
When extracting code with external dependencies:
When extracting code that manages state:
When extracting code with side effects (DB writes, API calls, file I/O):
For language and framework-specific action patterns, see:
references/extraction-patterns.md - Detailed extraction techniquesreferences/smell-indicators.md - Code smell checklistexamples/ - Working refactoring examplesAfter understanding refactoring principles, use the /scan-code command to identify candidates in your codebase, or /split-code <file> to refactor specific files following these patterns.
Master defensive Bash programming techniques for production-grade scripts. Use when writing robust shell scripts, CI/CD pipelines, or system utilities requiring fault tolerance and safety.