Agent Skills: Code Refactoring Skill

Proactively monitors file sizes and suggests refactoring BEFORE editing. Auto-invoked when files exceed size thresholds (150/200/300 lines) or complexity patterns detected. Works across JavaScript/TypeScript, Python, and general codebases. Prevents accidental complexity growth.

code-refactoringcomplexity-assessmentjavascriptpythontypescript
code-intelligenceID: WomenDefiningAI/claude-code-skills/code-refactoring

Skill Files

Browse the full folder contents for code-refactoring.

Download Skill

Loading file tree…

skills/code-refactoring/SKILL.md

Skill Metadata

Name
code-refactoring
Description
Proactively monitors file sizes and suggests refactoring BEFORE editing. Auto-invoked when files exceed size thresholds (150/200/300 lines) or complexity patterns detected. Works across JavaScript/TypeScript, Python, and general codebases. Prevents accidental complexity growth.
<!-- Created by: Madina Gbotoe (https://madinagbotoe.com/) Version: 2.0.0 Created: October 30, 2025 Last Updated: October 31, 2025 License: Creative Commons Attribution 4.0 International (CC BY 4.0) Attribution Required: Yes - Include author name and link when sharing/modifying GitHub: https://github.com/mgbotoe/claude-code-share Find latest version: https://github.com/mgbotoe/claude-code-share/tree/main/claude-code-skills/code-refactoring Purpose: Universal code refactoring skill - Works across multiple languages (JavaScript/TypeScript/React, Python, and general patterns). Proactively prevents code complexity by catching size and pattern issues before they become problems. NEW in v2.0: Path-based thresholds! The watcher now intelligently adjusts thresholds based on file location (pages can be larger than utilities, data files get different limits than logic files). Language Support: - JavaScript/TypeScript/React: Component splitting, hook extraction, data files - Python: Class/module splitting, function extraction, configuration management - General: Universal patterns that work across all languages -->

Code Refactoring Skill

🎯 Purpose: Prevent Complexity Before It Becomes a Problem

Problem this solves: "I rarely know when to refactor, typically get caught by accident"

Solution: AI proactively monitors file sizes and complexity patterns, alerting you BEFORE making changes that would worsen the problem.

Works with: JavaScript/TypeScript (React, Node), Python, and general codebases.

πŸ”” CRITICAL FIRST STEP: Check for File Watcher Alerts

BEFORE doing anything else when this skill is invoked, ALWAYS check for unread alerts from the background file watcher:

node ~/.claude/plugins/marketplaces/custom-skills/code-refactoring/scripts/check-alerts.js

Why this is critical:

  • The file watcher runs in the background monitoring code file sizes
  • It writes alerts to watcher-alerts.json when files are edited and exceed thresholds
  • These alerts tell you which files the user just edited that need refactoring attention
  • You must check these alerts FIRST to provide real-time feedback

If alerts exist:

  1. Display the alerts using the check-alerts.js script output
  2. Ask the user if they want help refactoring the alerted files NOW or LATER
  3. Then proceed with the rest of the skill workflow

If no alerts:

  • Continue with normal skill workflow (checking file sizes manually, etc.)

User can also manually check alerts with: /check-refactor-alerts


πŸ€– AUTO-INVOKE CONDITIONS (AI Should Check These)

1. Before Editing Files (PREVENTIVE)

When AI is about to edit a file, check size first:

# Check file size before editing
wc -l [target-file]

# If result:
# 150-200 lines β†’ ⚠️ WARN: Getting large, plan extraction
# 200-300 lines β†’ 🚨 ALERT: Should split before adding more
# 300+ lines   β†’ πŸ›‘ STOP: Must refactor before editing

Example workflow:

User: "Add new feature to UserProfile.tsx"
AI: *Before editing, checks file size*
AI: *Sees 280 lines*
AI: *Auto-invokes code-refactoring skill*
Skill: "🚨 UserProfile.tsx is 280 lines. Before adding features:
       - Extract data to separate file
       - Split modal into separate component
       - Then add new feature safely"

2. During File Creation (PROACTIVE)

While creating new code file, track size:

  • At 100 lines β†’ βœ… Good, continue
  • At 150 lines β†’ ⚠️ Pause, suggest structure
  • At 200 lines β†’ 🚨 Stop, extract now

Example workflow:

AI: *Creating new DashboardService.py*
AI: *Reaches 150 lines*
AI: *Auto-invokes code-refactoring skill*
Skill: "⚠️ File reached 150 lines. Before continuing:
       1. Extract data/config to constants file
       2. Move helper functions to utils module
       3. Continue with cleaner structure"

3. After Reading Files (REACTIVE)

When AI reads a file and discovers size issues:

# After reading, if file >300 lines, suggest refactor
wc -l filename

# If >300 lines, proactively suggest refactor BEFORE user asks for changes

Example workflow:

User: "Take a look at the analytics module"
AI: *Reads analytics.py*
AI: *Sees 450 lines*
AI: *Auto-invokes code-refactoring skill*
Skill: "πŸ›‘ analytics.py is 450 lines (150 lines over limit).

       Refactor plan:
       1. Extract constants to analytics_config.py (50 lines)
       2. Create AnalyticsChart class (100 lines)
       3. Create AnalyticsProcessor class (120 lines)
       4. Keep Analytics as orchestrator (180 lines)

       Should I proceed with refactoring before making other changes?"

4. Pattern Detection (AUTOMATIC)

AI should automatically detect these patterns and invoke skill:

Universal Patterns (All Languages)

  • βœ… Large data structures (>20 lines) β†’ Suggest separate config/data file
  • βœ… Complex conditionals (5+ branches) β†’ Suggest strategy pattern or dispatch table
  • βœ… Deeply nested logic (>3 levels) β†’ Suggest extraction
  • βœ… Repeated code blocks β†’ Suggest function/method extraction
  • βœ… Long parameter lists (>5 params) β†’ Suggest config object or builder pattern

JavaScript/TypeScript/React Patterns

  • βœ… 5+ data items hardcoded β†’ Suggest data file extraction
  • βœ… 4+ useState/useEffect hooks β†’ Suggest custom hook extraction
  • βœ… Modal/Dialog code β†’ Suggest separate component
  • βœ… Complex form (10+ fields) β†’ Suggest separate form component
  • βœ… Large switch statements β†’ Suggest lookup table or component map

Python Patterns

  • βœ… Class >300 lines β†’ Suggest splitting into multiple classes
  • βœ… Function >50 lines β†’ Suggest breaking into smaller functions
  • βœ… Module >500 lines β†’ Suggest splitting into package
  • βœ… Too many methods (>15) β†’ Suggest composition or mixins
  • βœ… Complex init (>20 lines) β†’ Suggest factory method or builder

πŸ“ File Size Guidelines (Language-Specific)

JavaScript/TypeScript/React Components

  • 100-200 lines: βœ… Perfect for most components
  • 200-300 lines: ⚠️ Good for complex components, watch closely
  • 300+ lines: πŸ›‘ MUST split into logical sub-components

Python Modules

  • 150-250 lines: βœ… Good module size
  • 250-400 lines: ⚠️ Consider splitting by responsibility
  • 400+ lines: πŸ›‘ Split into multiple modules or package

Python Classes

  • 100-200 lines: βœ… Well-focused class
  • 200-300 lines: ⚠️ Check single responsibility principle
  • 300+ lines: πŸ›‘ Split into multiple classes or use composition

General Files (Any Language)

  • 150-250 lines: βœ… Maintainable size
  • 250-400 lines: ⚠️ Getting complex
  • 400+ lines: πŸ›‘ Refactor needed

πŸ” Refactoring Analysis Template

When skill is invoked, provide this analysis:

## Code Refactoring Analysis: [Filename]

**Current State:** [X] lines - [βœ… Good / ⚠️ Warning / πŸ›‘ Critical]

**Identified Issues:**
- File size issues (>200 or >300 lines)
- Complex nested logic / too many responsibilities
- Language-specific patterns (hooks, data arrays, classes, etc.)

**Refactoring Recommendations:**
1. Extract [what] to [new-file] (saves [X] lines, [X] mins)
2. Extract [what] to [new-file] (saves [X] lines, [X] mins)

**Result:** Main file β†’ [target] lines | Risk: [Low/Med/High] | Time: [X] mins

**Proceed?** [Yes/No/Later/Review]

For complete template with all fields, see FORMS.md


🚨 Automatic Splitting Triggers

When ANY of these conditions occur, AUTO-INVOKE this skill:

Universal Triggers (All Languages)

  1. βœ… File reaches 150 lines β†’ Suggest extraction points
  2. βœ… File reaches 200 lines β†’ Alert and plan refactor
  3. βœ… File reaches 300 lines β†’ Stop and refactor first
  4. βœ… Complex nested logic detected
  5. βœ… Repeated code patterns found

JavaScript/TypeScript Triggers

  1. βœ… Component reaches 150 lines β†’ Extract next section
  2. βœ… 3+ useState hooks β†’ Extract to custom hook
  3. βœ… Large data array β†’ Move to data file immediately
  4. βœ… Modal/Dialog code β†’ Separate component from start
  5. βœ… Complex form β†’ Separate form component immediately

Python Triggers

  1. βœ… Class reaches 250 lines β†’ Plan class split
  2. βœ… Function reaches 40 lines β†’ Suggest breaking down
  3. βœ… Module reaches 350 lines β†’ Plan module split
  4. βœ… 10+ methods in class β†’ Consider composition
  5. βœ… Complex init β†’ Suggest factory pattern

πŸ“‹ Quick Decision Matrix

When to extract what (Quick Reference):

JavaScript/TypeScript/React:

  • Data file: 5+ items, arrays >20 lines, config objects
  • Sub-component: Modal/dialog, complex list items, distinct UI sections
  • Custom hook: 4+ useState, complex logic, reusable state

Python:

  • Config file: 5+ variables, large data (>20 lines), magic numbers
  • New class: Class >300 lines, multiple responsibilities, 10+ methods
  • Utility function: Repeated logic, pure functions, generic operations
  • New module: File >400 lines, distinct responsibilities, testable independently

For detailed extraction patterns and examples, see REFERENCE.md


πŸ’‘ Proactive Suggestions

AI should auto-suggest at these milestones:

  • New file: Monitor size, alert at 150 lines
  • 100 lines: Looking good, will alert at 150
  • 150 lines: ⚠️ Check if should extract before continuing
  • 200 lines: 🚨 Recommend refactoring now
  • Reading large file: Offer refactoring plan before changes

πŸ”§ EXECUTION PHASE (After User Approval)

IMPORTANT: This skill can EXECUTE refactoring, not just suggest it!

Step 1: Present Plan and Get User Approval

After analyzing file size and creating refactoring plan, always ask for approval:

## Refactoring Plan Ready

**File:** [filename] ([X] lines)
**Target:** Reduce to <[target] lines

### Proposed Changes:
1. Extract [data/component/class] to [new-file] (saves [X] lines)
2. Extract [component/function] to [new-file] (saves [X] lines)
3. Extract [hook/utility] to [new-file] (saves [X] lines)

**Result:** Main file reduced to ~[X] lines
**Estimated time:** [X] minutes
**Risk level:** Low (all changes are git-committed incrementally)

### Options:

**βœ… Yes** - Execute refactoring now (recommended)
  - I'll split the file step-by-step
  - Each step will be tested and committed
  - You can rollback if needed
  - Then I'll continue with your original request

**❌ No** - Skip refactoring
  - I'll continue with your edit
  - Warning: File will grow larger
  - May become harder to maintain

**⏰ Later** - Save plan for later
  - I'll save the plan to `refactoring-plan-[filename].md`
  - You can execute it manually later
  - I'll continue with your edit now

**πŸ“‹ Review** - Show detailed step-by-step execution plan
  - I'll explain exactly what will happen
  - Then you can choose Yes/No/Later

**Your choice?**

Step 2: Execute Refactoring (Only if User Says "Yes")

When user approves, execute refactoring incrementally:

  1. Create backup commit (safety first)
  2. Verify starting state (lint, type-check, test must pass)
  3. Execute step-by-step (extract β†’ verify β†’ commit for each change)
  4. Final verification (full test suite)
  5. Rollback automatically if any step fails

Key execution principles:

  • βœ… Each step is atomic and committed separately
  • βœ… Each step is verified before proceeding
  • βœ… Automatic rollback on any failure
  • βœ… User can cancel at any time

For detailed execution procedures, safety measures, rollback procedures, and examples, see REFERENCE.md β†’ "EXECUTION PHASE - Detailed Procedures"


Step 3: Continue With Original Request

After execution (success or skip):

Refactoring: βœ… Complete (or ⏭️ Skipped)
Original request: "[user's request]"

Now proceeding with your request...

πŸ”„ Integration with Other Skills

Works with:

  • ui-ux-audit - Check file sizes during UI audits
  • technical-writing - Document refactored structure
  • qa-testing - Ensure tests cover refactored code

Triggers:

  • Before major edits to large files
  • During file creation
  • After reading files >200 lines
  • When detecting size/complexity patterns

πŸ” CODEBASE AUDIT MODE (For Existing Technical Debt)

Use this mode when user asks to:

  • "Audit the codebase" or "find large files"
  • Check for "technical debt" or "existing problems"
  • "Scan for refactoring opportunities"
  • Review legacy/inherited codebase
  • Plan major refactoring initiative

Audit Workflow (Brief):

  1. Scan for oversized files by language/type
  2. Categorize by severity (Critical >300, High 200-300, Medium 150-200, Good <150)
  3. Prioritize using formula: (Size/100) + (Change Frequency Γ— 2) + (Business Impact Γ— 3)
  4. Create refactoring roadmap with time estimates
  5. Execute incrementally (one file per sprint)

Quick scan commands:

# Find oversized files by type
find src -name "*.tsx" -exec wc -l {} \; | awk '$1 > 200' | sort -rn

# Use helper script
bash scripts/check-size.sh src/ '*.tsx'

For detailed audit procedures, report templates, prioritization matrices, and batch analysis commands, see REFERENCE.md β†’ "CODEBASE AUDIT MODE - Detailed Procedures"


πŸ“š Language-Specific Details

For detailed refactoring patterns specific to each language, see:

  • REFERENCE.md - Comprehensive language-specific guides
  • FORMS.md - Refactoring templates and checklists

To check file size quickly:

# Use the helper script
bash ~/.claude/plugins/marketplaces/custom-skills/code-refactoring/scripts/check-size.sh [filename]

βœ… Success Criteria

Refactoring is successful when:

  1. βœ… Main file within target size for language
  2. βœ… All extracted files well-organized
  3. βœ… No broken imports or references
  4. βœ… All tests pass
  5. βœ… Functionality unchanged
  6. βœ… Code more maintainable and readable
  7. βœ… Clear separation of concerns
  8. βœ… Documentation updated

Remember: Prevention > Cure

This skill prevents "caught by accident" scenarios by:

  • Checking BEFORE editing (preventive)
  • Alerting DURING creation (proactive)
  • Suggesting AFTER reading (reactive)
  • Enforcing patterns automatically

The goal: Never again discover an oversized, complex file by accident.

Multi-language support: Works across JavaScript/TypeScript, Python, and general codebases with language-specific patterns.