Code Simplification
Systematically refactor code for clarity and maintainability. The fundamental test: would a new team member understand this faster? Behavior must be preserved exactly.
Contract
Use this skill to improve the readability and structure of code that works but is harder to read, maintain, or extend than it should be.
Do not use this skill for:
- Bug fixes — use
quick instead.
- Feature work or behavior changes — any change that alters outputs, errors, or side effects is out of scope.
- Performance optimization — a separate concern with different tradeoffs.
- Unsolicited drive-by cleanup of files unrelated to the user's stated scope.
Stop and report when the code's intent is unclear and simplification would require guessing at behavior (Chesterton's Fence applies).
Input Contract
Required:
- The file(s) or function(s) to simplify.
- Confirmation that behavior must be preserved (assumed if not stated otherwise).
Optional:
- Specific patterns to target (nesting, naming, duplication, function length).
- Existing tests or commands to verify behavior before and after.
Output Contract
Return:
- Files changed and a summary of each simplification applied.
- The test or verification command run and its pass/fail result.
- Any patterns intentionally left unchanged and why.
Do not claim completion unless at least one verification step (existing tests, build, type-check, or manual diff review) confirms behavior is unchanged.
Foundational Principles
- Preserve behavior exactly — outputs, errors, side effects, and edge cases remain unchanged.
- Follow project conventions — match existing patterns; do not impose external style preferences.
- Prefer clarity over cleverness — explicit code beats compact code requiring mental parsing.
- Apply incrementally — one change, verify, then continue; do not batch unverified simplifications.
- Scope to what was asked — focus on the stated files; avoid unscoped opportunistic cleanups.
Workflow
- Understand first (Chesterton's Fence).
- Read the target code and any callers or tests before changing anything.
- Identify why the code is structured as it is. If the reason is unknown, note it before proceeding.
- Identify opportunities.
- Scan for signals from the Key Patterns list below.
- Rank by impact: structural complexity > naming > duplication.
- Apply incrementally.
- Make one logical change at a time.
- Run the verification command after each change, not at the end.
- Verify results.
- Confirm the simplified version is genuinely clearer, not just shorter.
- Run existing tests or build. If none exist, do a manual diff review and document the gap.
Key Patterns
| Signal |
Threshold |
Approach |
| Deep nesting |
3+ levels |
Extract early returns or named helpers |
| Long function |
50+ lines |
Split into focused, named sub-functions |
| Nested ternaries |
2+ levels |
Replace with if/else or named variables |
| Generic names |
data, temp, val, item |
Rename to domain-specific terms |
| Duplicated logic |
2+ copies |
Extract to a shared helper |
| Comment explaining what |
Any |
Rename the thing so the comment is unnecessary |
Critical Boundaries
Do not simplify:
- Code that is already clean — unnecessary churn creates noise in diffs.
- Code you do not fully understand — misread intent leads to silent behavior changes.
- Performance-critical sections without explicit profiling evidence that the change is safe.
- Multiple patterns in one commit without testing each — batch simplifications mask which change broke behavior.
Validation
# Run existing tests to confirm behavior is unchanged
# Replace with the actual test command for this project
npm test 2>&1 | tail -10 # Node projects
python -m pytest -x 2>&1 | tail -10 # Python projects
go test ./... 2>&1 | tail -10 # Go projects
1---2name: code-simplification3description: Use when the user asks to simplify, reduce complexity, or improve readability of existing code without changing behavior. Triggers on "simplify", "too complex", "hard to read", "reduce nesting", "clean up", "make clearer", or "refactor for clarity". Not for bug fixes, feature additions, or performance tuning.4---56# Code Simplification78Systematically refactor code for clarity and maintainability. The fundamental test: **would a new team member understand this faster?** Behavior must be preserved exactly.910## Contract1112Use this skill to improve the readability and structure of code that works but is harder to read, maintain, or extend than it should be.1314Do not use this skill for:15- Bug fixes — use `quick` instead.16- Feature work or behavior changes — any change that alters outputs, errors, or side effects is out of scope.17- Performance optimization — a separate concern with different tradeoffs.18- Unsolicited drive-by cleanup of files unrelated to the user's stated scope.1920Stop and report when the code's intent is unclear and simplification would require guessing at behavior (Chesterton's Fence applies).2122## Input Contract2324Required:25- The file(s) or function(s) to simplify.26- Confirmation that behavior must be preserved (assumed if not stated otherwise).2728Optional:29- Specific patterns to target (nesting, naming, duplication, function length).30- Existing tests or commands to verify behavior before and after.3132## Output Contract3334Return:35- Files changed and a summary of each simplification applied.36- The test or verification command run and its pass/fail result.37- Any patterns intentionally left unchanged and why.3839Do not claim completion unless at least one verification step (existing tests, build, type-check, or manual diff review) confirms behavior is unchanged.4041## Foundational Principles42431. **Preserve behavior exactly** — outputs, errors, side effects, and edge cases remain unchanged.442. **Follow project conventions** — match existing patterns; do not impose external style preferences.453. **Prefer clarity over cleverness** — explicit code beats compact code requiring mental parsing.464. **Apply incrementally** — one change, verify, then continue; do not batch unverified simplifications.475. **Scope to what was asked** — focus on the stated files; avoid unscoped opportunistic cleanups.4849## Workflow50511. **Understand first (Chesterton's Fence).**52 - Read the target code and any callers or tests before changing anything.53 - Identify why the code is structured as it is. If the reason is unknown, note it before proceeding.542. **Identify opportunities.**55 - Scan for signals from the Key Patterns list below.56 - Rank by impact: structural complexity > naming > duplication.573. **Apply incrementally.**58 - Make one logical change at a time.59 - Run the verification command after each change, not at the end.604. **Verify results.**61 - Confirm the simplified version is genuinely clearer, not just shorter.62 - Run existing tests or build. If none exist, do a manual diff review and document the gap.6364## Key Patterns6566| Signal | Threshold | Approach |67|---|---|---|68| Deep nesting | 3+ levels | Extract early returns or named helpers |69| Long function | 50+ lines | Split into focused, named sub-functions |70| Nested ternaries | 2+ levels | Replace with if/else or named variables |71| Generic names | `data`, `temp`, `val`, `item` | Rename to domain-specific terms |72| Duplicated logic | 2+ copies | Extract to a shared helper |73| Comment explaining what | Any | Rename the thing so the comment is unnecessary |7475## Critical Boundaries7677Do not simplify:78- Code that is already clean — unnecessary churn creates noise in diffs.79- Code you do not fully understand — misread intent leads to silent behavior changes.80- Performance-critical sections without explicit profiling evidence that the change is safe.81- Multiple patterns in one commit without testing each — batch simplifications mask which change broke behavior.8283## Validation8485```bash86# Run existing tests to confirm behavior is unchanged87# Replace with the actual test command for this project88npm test 2>&1 | tail -10 # Node projects89python -m pytest -x 2>&1 | tail -10 # Python projects90go test ./... 2>&1 | tail -10 # Go projects91```9293- [ ] At least one verification step ran and passed (tests, build, type-check, or lint).94- [ ] No behavior-altering changes are present in the diff.95- [ ] Each simplification is a distinct, reviewable unit (not one giant commit).96- [ ] Patterns intentionally left unchanged are noted with a reason.