✂️ Refactoring Skill: Splitting Large Files
Context
Files exceeding 300 lines violate SRP and become hard to maintain. This skill systematically breaks them down.
Steps
- •
Analyze Responsibilities:
- •Identify distinct clusters of logic (e.g.,
UI ComponentsvsEvent HandlersvsHelper Functions). - •Use
view_file_outlineto visualize class/function groups.
- •Identify distinct clusters of logic (e.g.,
- •
Create Package Structure:
- •If
module.pyis the target:bashmkdir module_pkg mv module.py module_pkg/__init__.py
- •Or prefer creating specific files:
helpers.py,constants.py,types.py.
- •If
- •
Move & Isolate:
- •Move code chunks to new files.
- •CRITICAL: Ensure imports in new files are correct.
- •CRITICAL: Re-export moved classes in
__init__.pyto maintain backward compatibility if needed, OR update all consumers (preferred).
- •
Verify:
- •Run
uv run pytestimmediately. - •Check for circular imports (common risk).
- •Run
Example Command Sequence
powershell
# 1. Create sub-modules New-Item -Path "src/core/complex_module/types.py" -Force New-Item -Path "src/core/complex_module/logic.py" -Force # 2. Update __init__.py to export public API echo "from .types import *" > src/core/complex_module/__init__.py echo "from .logic import *" >> src/core/complex_module/__init__.py