Skill v1.0.1
currentAutomated scan100/100+1 new
name: simplify description: "Reduce complexity in recently changed code — guard clauses, extract helpers, deduplicate, name constants and conditions. Apply after code review or before shipping. Never changes behavior; reverts any change that breaks tests." metadata: version: "1.0.1"
Simplify
Read the workflow contract first. Select profile and topology independently; default STANDARD + SOLO. Apply mandatory project policy in every profile. This procedure does not authorize another actor or work beyond the requested stage.
Reduce unnecessary complexity in recently changed code without changing behavior.
Golden Rule: Preserve behavior and scope. Undo only your own failed simplification, preserving earlier user work; never reset or restore a whole file/worktree to roll back one edit.
When to Activate
- After
/revieworcode-revieweragent identifies complexity issues (deep nesting, long functions, magic values, duplicated logic) - Before
/shipwhen code passes tests but has structural noise - After implementing a feature — as a final cleanup pass before the quality gate
- When you spot nesting depth ≥ 4, functions > 50 lines, or repeated 3-line blocks while writing code
Do not activate:
- On code outside the current change scope
- When all complexity findings are intentional (performance-critical tight loops, generated code, migrations)
- When tests are failing — fix tests first
Phase 1 — SCOPE
If a specific file or directory was requested, use that as the scope.
Otherwise, use review scope selection: current unstaged, staged and task-relevant untracked files first, then the actual base comparison when clean. A recent commit is a target only when intended. Include docs/config when relevant; do not silently select an unrelated last commit.
QUICK removes obvious noise only; STANDARD evaluates clearer structure; DEEP records alternatives and maintenance tradeoffs. If nothing needs simplification, record a no-op assessment. Never expand scope to satisfy this skill.
Detect test command from project config:
| Marker | Command | |
|---|---|---|
package.json | npm test (or pnpm/yarn/bun test) | |
pytest.ini or pyproject.toml | pytest | |
go.mod | go test ./... | |
Cargo.toml | cargo test | |
Makefile with test target | make test | |
| Repository unittest suite | Documented python3 -B -m unittest discover -s tests |
For documentation/configuration changes, a relevant installed linter or manual check may be the meaningful verification; do not invent code tests or fetch tools.
Phase 2 — SIMPLIFICATION PASSES
Run applicable passes sequentially. After each owned edit, rerun affected checks and mandatory project gates. If it regresses behavior, undo only that edit and verify recovery; stop if recovery fails. Report unavailable required checks as NOT RUN, never fabricate a green suite.
Pass 1 — Guard Clauses
Target: Functions with nesting depth ≥ 4.
Invert conditions and return early to flatten nesting:
// BEFOREfunction validate(user) {if (user) {if (user.email) {if (isValidEmail(user.email)) {return processUser(user);}}}}// AFTERfunction validate(user) {if (!user) return null;if (!user.email) return null;if (!isValidEmail(user.email)) return null;return processUser(user);}
Per function: identify deepest block → invert condition → add early return → unindent → run tests → revert on failure.
Pass 2 — Extract Helpers
Target: Functions longer than ~50 lines.
Extract cohesive sub-tasks to named helpers (name describes WHAT, not HOW):
// BEFORE: processOrder() — 50+ lines// AFTERfunction processOrder(order) {const validated = validateOrderItems(order);const priced = calculatePricing(validated);saveToDatabase(priced);sendConfirmation(priced);}
Per function: find logical groupings → extract to named helper → replace with call → run tests → revert on failure.
Pass 3 — Deduplicate
Target: Repeated code blocks (3+ similar lines, 2+ occurrences) within the change scope.
Extract to a shared utility function. Do not reach outside the scope to deduplicate with unrelated code.
Per duplication: identify pattern → extract utility → replace both occurrences → run tests → revert on failure.
Pass 4 — Name Magic Values
Target: Hardcoded literals used in logic conditions (if (status === "PENDING"), if (count > 100)).
// BEFOREif (items.length > 50) { ... }// AFTERconst MAX_BATCH_SIZE = 50;if (items.length > MAX_BATCH_SIZE) { ... }
Per literal: identify usage context → choose descriptive name → define constant at file/module top → replace → run tests → revert on failure.
Pass 5 — Name Complex Conditions
Target: Boolean expressions with 3+ operators.
// BEFOREif (user.isActive && user.hasSubscription && !user.isBlocked || user.isAdmin) { ... }// AFTERconst canAccess = (user.isActive && user.hasSubscription && !user.isBlocked) || user.isAdmin;if (canAccess) { ... }
Name describes intent, not logic. Per condition: identify intent → extract to named variable or helper → replace → run tests → revert on failure.
Phase 3 — SUMMARY
## Simplification Summary### Changes- [file:line] — guard clauses: inverted N conditions in functionName- [file:line] — extracted helper: helperName- [file:line] — named constant: CONSTANT_NAME = value- ...### Skipped- [file] — reason (e.g., "No deep nesting", "All changes reverted — tests failed")### TestsAll N tests passing.
If nothing was simplified: Code is already clean — no simplifications applied.
Lifecycle Position
Follow the workflow contract. Simplify follows Review and precedes Ship readiness; rerun affected checks after edits. No-op is valid and neither simplification nor readiness authorizes delivery.