| name | Codebase Organizer |
| description | Monitor and refactor large files into smaller, AI-friendly modules. Use when user asks to check file sizes, split large files, or organize the codebase. Ensures tests pass before and after refactoring. |
Codebase Organizer
Maintain optimal file sizes for AI-assisted development by splitting large files into focused modules.
Configuration
| Variable | Default | Description |
|---|
SOURCE_DIR | . | Root source directory to scan |
FILE_EXT | * | File extension filter (e.g., *.go, *.ts, *.py) |
MAX_LINES | 800 | Maximum lines before file must be split |
TEST_CMD | make test | Command to run tests |
LINT_CMD | make lint | Command to run linter |
Quick Start
find ${SOURCE_DIR:-.} -name "${FILE_EXT:-*}" -exec wc -l {} \; | awk '$1 > 500 {print}' | sort -rn
When to Use This Skill
Invoke when user says:
- "Check file sizes" / "report file sizes"
- "Split this file" / "refactor large file"
- "Organize the codebase"
- "Make files AI-friendly"
File Size Targets
| Size | Status | Action |
|---|
| 200-500 lines | Sweet spot | None needed |
| 500-800 lines | Acceptable | Consider splitting |
| >800 lines | CRITICAL | Must split |
Workflow
Step 1: Status Check
find ${SOURCE_DIR:-.} -name "${FILE_EXT:-*}" -type f -exec wc -l {} \; | awk '$1 > 500 {print}' | sort -rn
Output format:
CRITICAL (>800 lines):
src/parser/parser.ts: 2518 lines
WARNING (500-800 lines):
src/eval/evaluator.py: 765 lines
Step 2: Plan the Split
Before ANY refactoring:
-
Run baseline tests: ${TEST_CMD}
-
Identify natural boundaries:
- Different responsibilities or concerns
- Public API vs internal helpers
- Different data types or domains
- Logical groupings of related functions
-
Plan file names (match to primary functions):
expressions.ts → expression handling
statements.ts → statement handling
helpers.ts → utility functions
-
Check for circular dependency risks
Step 3: Execute Split
Keep together:
- Tightly coupled functions
- Helper functions used by one main function
- Functions that share complex state
Step 4: Validate
${TEST_CMD}
${LINT_CMD}
If tests fail:
- DO NOT COMMIT
- Analyze failure (missing import? broken reference?)
- Fix issue
- Re-run tests
- Only commit when tests pass
Step 5: Document & Commit
Commit with descriptive message:
git add src/path/*.ext
git commit -m "Split path/file.ext into N files (AI-friendly)
- primary.ext: Main types (200 lines)
- expressions.ext: Expression handling (450 lines)
..."
Error Handling
Circular Dependency
If split creates circular dependency:
- Identify cycle: File A imports B, File B imports A
- Solutions:
- Extract shared code to new file (e.g.,
types.ext)
- Use interfaces to break dependency
- Restructure to have one-way dependency
- Merge files if truly inseparable (<800 lines combined)
Test Failures
Common issues after split:
- Missing import in new file
- Function moved but still referenced in old location
- Test file not updated to match new structure
Success Metrics
After refactoring:
- 0 files over 800 lines
- <5 files between 500-800 lines
- Average file size: 300-400 lines
- 100% test pass rate maintained
Important Reminders
- ALWAYS run tests before and after refactoring
- NEVER commit if tests fail
- One refactoring at a time (easier to debug)
- Show plan before executing (get user approval)
- Keep related functions together (maintain cohesion)