/code-simplification:code-simplificationfrom code-simplification
Reduces code complexity without changing behavior. Targets deep nesting, long functions, high cyclomatic complexity, duplication, dead code, unclear names, and uninformative comments. Language-agnostic.
Ships in code-simplification. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Simplify code by reducing complexity while preserving exact behavior. The goal is not fewer lines — it's code that is easier to read, understand, modify, and debug. Every simplification must pass one test: "Would a new team member understand this faster than the original?"
This skill is language-agnostic: it works on patterns (nesting, naming, duplication, dead code), not on any one language's idioms. Apply it using the idioms of whatever language and codebase you're in.
This skill ships in two layers. SKILL.md carries the rules and a short summary of each step; four files beside it carry the full procedures:
evidence-gathering.md — model-tier routing, handoff packets, verifying findingscyclomatic-complexity.md — counting, tools, the split procedureequivalent-functions.md — diff → callers → propose → mergecomments.md — the comment digestThe first time you use this skill in a session, read all four before doing anything else — before scoping, before reading the target code, before answering a question about it. The summaries in this file are reminders for a reader who has already seen the full text; they are not a substitute for it. Re-read the relevant file again at the step that names it. If the files are missing, say so — do not proceed as if the summaries were the whole skill.
When NOT to use:
Default to recently modified code: the current session's edits, the working diff (git diff HEAD), or the current branch's changes. Only widen the scope when the user explicitly names a file, module, or the whole codebase. Unscoped simplification creates noisy diffs and risks regressions in code nobody asked you to touch.
Don't change what the code does — only how it expresses it. All inputs, outputs, side effects, error behavior, ordering, and edge cases must remain identical. If you're not sure a simplification preserves behavior, don't make it.
ASK BEFORE EVERY CHANGE:
→ Does this produce the same output for every input?
→ Does this maintain the same error behavior?
→ Does this preserve the same side effects and ordering?
→ Do all existing tests still pass without modification?
→ Is there a test, written BEFORE the change, that would fail if the answer to any of these were "no"?
"Semantically equivalent" is a claim, and a claim is proven by a test that existed before the edit and still passes after it — see Prove Equivalence With a Test That Predates the Change. Reasoning about equivalence, however careful, is not proof.
When the scope is a branch or a diff, find its base: the starting commit the user or a plan names, else git merge-base HEAD <default branch>. Code and tests that are not on the base are the change's own draft. Nobody outside it has ever relied on them. "Behavior" is what the callers in the code can reach, and "existing tests" are the tests on the base.
When the diff adds a path that no call site in the code can reach — a guard for a state no caller passes, a fallback for a value no caller produces, a branch no input reaches — and a test the diff also adds is the only thing that reaches it, that test is not a caller. It is part of the dead path. Remove the path and that test in one change, run the remaining tests, and name the deleted test in the report. This is the Dead code row below, not bending a test, and it is not a question for the user: no user, caller, or test on the base can see it go. Confirm three things first:
git log -S'<a line of the path>' <base>..HEAD shows the diff added it.Two cases stay as they are. A test on the base is a pin and passes unmodified, always. A branch-new test of behavior a caller can reach is a pin too: never edit its expected value to make a reshape pass.
Simplification means making code more consistent with this codebase, not imposing external preferences. Before simplifying, read the project's instructions file (CLAUDE.md, AGENTS.md, CONTRIBUTING, style guides) and study how neighboring code handles similar patterns — imports, declaration style, naming, error handling, type/annotation depth. Match them, even where you'd personally write it differently.
Simplification that breaks project consistency is not simplification — it's churn.
Explicit code beats compact code whenever the compact version requires a mental pause to parse. A five-line conditional a reader scans in two seconds is simpler than a one-line nested ternary they have to decode. Dense chained one-liners that build a structure in a single expression are usually clearer as a named intermediate step. When in doubt, write the boring version.
Simplification has a failure mode: over-simplification. Watch for these traps:
Simplify the code in scope; leave the rest alone. No drive-by refactors, no "improving" adjacent code, no reformatting files you pass through. Every changed line should trace back to a simplification you can name.
Before changing or removing anything, understand why it exists. If you see a fence across a road and don't know why it's there, don't tear it down — first learn the reason, then decide whether the reason still applies.
BEFORE SIMPLIFYING, ANSWER:
- What is this code's responsibility?
- What calls it? What does it call?
- What are the edge cases and error paths?
- Are there tests that define the expected behavior?
- Why might it have been written this way? (Performance? Platform constraint? Historical reason?)
- What does git blame / history say about its original context?
If you can't answer these, you're not ready to simplify. Read more context first.
Scan for these patterns — each is a concrete signal, not a vague smell:
Structural complexity:
| Pattern | Signal | Simplification |
|---|---|---|
| Deep nesting (3+ levels) | Hard to follow control flow | Invert conditions into guard clauses / early returns, or extract helpers |
| Long functions (50+ lines) | Multiple responsibilities | Split into focused functions with descriptive names |
| High cyclomatic complexity (>10 decision points) | Too many paths to test one by one | Split along decision clusters — see Cyclomatic Complexity |
| Two functions with the same shape | Same branches and calls, different names or literals | Propose a merge — see Roughly Equivalent Functions |
| Nested conditionals-in-expressions | Requires a mental stack to parse | Replace with explicit branching or a lookup table |
| Boolean parameter flags | doThing(true, false, true) | Replace with named options or separate functions |
| Repeated conditionals | Same check in multiple places | Extract to a well-named predicate |
Naming and readability:
| Pattern | Signal | Simplification |
|---|---|---|
| Generic names | data, result, temp, val, item | Rename to describe the content: userProfile, validationErrors |
| Abbreviated names | usr, cfg, btn, evt | Use full words unless the abbreviation is universal (id, url, api) |
| Misleading names | A "get" that also mutates state | Rename to reflect actual behavior |
| Comments explaining "what" | // increment counter above an increment | Delete the comment — the code says it |
| Comments explaining "why" | // Retry because the API is flaky under load | Keep these — they carry intent the code can't express |
| Comments narrating history or a session | "used to", "pass 2", F7, "per review" | Rewrite as a present-tense constraint or delete — see Comments |
| Comments counting things that live elsewhere | "the 7 tests", "both fields", "the three callers" | Name the set (file, tag, pattern, invariant) so the comment grows with it — see Comments |
Redundancy:
| Pattern | Signal | Simplification |
|---|---|---|
| Duplicated logic | Same 5+ lines in multiple places | Extract to a shared function |
| Dead code | Unreachable branches, unused variables, commented-out blocks, a guard no call site can trip | Remove (after confirming it's truly dead); a branch-new test that only reaches it goes with it |
| Unnecessary wrappers | A layer that adds no value over what it wraps | Inline it; call the underlying thing directly |
| Over-engineered patterns | Factory-for-a-factory, strategy-with-one-strategy, config nobody sets | Replace with the simple direct approach |
| Manual re-implementation | Hand-rolled loop/branching for something the language or stdlib does idiomatically | Use the idiom the codebase already uses elsewhere |
Make one simplification at a time and run the tests after each change. Never batch several simplifications into a single untested edit — when something breaks, you need to know which change caused it. "Extracted four helpers" is four changes, not one.
FOR EACH SIMPLIFICATION:
1. Confirm a test pins the behavior you are about to reshape (write one first if not — see below)
2. Run it: it must pass on the ORIGINAL code
3. Make the change
4. Run the test suite
5. Tests pass → keep it, move to the next
6. Tests fail → revert and reconsider (do NOT modify the tests to make them pass)
Keep refactoring separate from feature work. A change that refactors and adds a feature is two changes — split them into separate commits or PRs.
The Rule of 500: if a refactoring would touch more than ~500 lines, invest in automation (codemods, structured search-and-replace, AST transforms) instead of hand-editing. Manual edits at that scale are error-prone and exhausting to review.
After the pass, step back and evaluate the whole:
COMPARE BEFORE AND AFTER:
- Is the simplified version genuinely easier to understand?
- Did you introduce any pattern inconsistent with the codebase?
- Is the diff clean and reviewable, with nothing unrelated mixed in?
- Would a teammate approve this change as a net improvement?
If the "simplified" version is harder to understand or review, revert it. Not every simplification attempt succeeds, and reverting is a valid outcome.
Finding (which functions are complex, which pairs share a shape, who calls what) is cheap work; comprehending (why, and whether a change is safe) is expensive work. Route them differently:
grep/rg and the project's complexity tool instead of reading.Read evidence-gathering.md before spawning any subagent — it has the routing table, the handoff-packet template, and the stop conditions.
Every change this skill makes — a renamed variable, a flattened conditional, a split function, a merged pair — must be semantically equivalent to what it replaced. The only accepted proof is TDD applied to refactoring:
1. Before touching the code, write (or locate) tests that pin its CURRENT behavior:
the normal cases, every branch you can reach, the error paths, and the edge cases
(empty input, zero, missing keys, malformed data). Write each expected value
down as a literal; assert only what a caller can observe.
2. Run them against the ORIGINAL code. They must pass. A test that fails here is
describing behavior the code does not have — fix the test, not the code.
3. Make the simplification.
4. Run the same tests, unmodified. They must still pass.
A test that passes before and after is the evidence. A test written after the change proves only that the new code does what the new code does.
If the repository has no tests — or none cover the code in scope — stop and ask the user before creating any. Adding a test file, a test framework, or a test dependency is a decision about their project, not yours. Ask once, with the specifics: which functions you want to pin, what framework you would use, and what the tests would live under. The simplification waits for the answer — do not make the change first and ask afterwards, and do not treat silence as a decline. Deadline pressure, "keep it moving", and "it's a small change" do not waive this — a missing test is exactly when a refactor silently changes behavior.
If the user declines tests, say plainly that the change is unproven, use a throwaway differential check (old function vs. new function over the same inputs) as the best remaining evidence, report what it covered, and keep the change mechanical. Do not present a deleted script as proof: it guards nothing once it is gone, and the next person who edits the function inherits none of it.
What the pinning tests are not: they are not a rewrite of the test suite, not a coverage project, and not a place to fix behavior you dislike. A characterization test records what the code does today — including the odd edge case — because today's behavior is what "equivalent" means.
A pin that cannot fail is not evidence, and neither is one that fails for the wrong reason. Two shapes of test defeat the proof above, and both look thorough:
Flag existing tests that have either fault. When you locate the tests that cover the code in scope, check each one against the two shapes above before relying on it. A tautological test (one that works out its own answer) or a change detector (one that pins how, including a count of internal work) is not a pin, so the function counts as uncovered until a test that can fail sits beside it. Report every such test: file, test name, which fault, and the fix (the literal its expected value should be, or the observable fact it should assert instead). Do not rewrite or delete it unasked: it is outside the simplification, and a test edited in the same diff as a refactor muddies the proof. Check only the tests that cover code in scope; this is not a sweep of the suite.
Count the independent paths: 1, plus 1 for each if, elif/else if, case, loop, catch, ternary, and &&/|| inside a condition. The number is roughly how many tests the function needs, which is why high values go untested.
| Complexity | Meaning | Action |
|---|---|---|
| 1–10 | Straightforward | Leave it unless another pattern applies |
| 11–20 | Hard to test fully | Look for a split along decision clusters |
| 21+ | Effectively untestable as one unit | Split — this is not optional |
Report the number before and after. Split along decision clusters into pure, individually testable functions, and never into once-called helpers that just relocate lines. Read cyclomatic-complexity.md before splitting a function — it has the tools per ecosystem, the split procedure, and the fragmentation trap.
Two functions with the same control-flow skeleton and the same calls, differing only in names or literals, were written twice. A merge is a proposal to the user, never a reflex: diff them line by line, count the callers of both, then propose the unified signature and wait. Behavioral differences — error handling, validation, mutation — end the review; they are not equivalent. Read equivalent-functions.md before proposing a merge.
Comments on touched lines are in scope. The rule: a comment earns its place by carrying information the code does not, about the code as it is now, in as few lines as that takes. Cover it with your hand — if the code is no poorer, delete it. Present tense, not history; two lines above a declaration, and a prose comment over four lines is a question for the user, not a keep; never name a field the body reads or a helper it calls; no session-scoped identifiers (finding numbers, pass labels, task IDs, phase names); no counts of things that live elsewhere ("the 7 tests", "both fields") — name the set so the comment grows with it; verify anything a comment names. A comment telling a future editor what to do ("never", "always", "keep in step") enforces nothing: it becomes an assertion in the existing test that runs the code, with one line of fact left behind. Read comments.md before writing or rewriting a comment. The standalone appropriate-comments-code skill is the full treatment.
The single most portable simplification — flattening nested conditionals into guard clauses. The shape is identical in every language:
# Before: the happy path is buried three levels deep
def process(data):
if data is not None:
if data.is_valid():
if data.has_permission():
return do_work(data)
else:
raise PermissionError("No permission")
else:
raise ValueError("Invalid data")
else:
raise TypeError("Data is None")
# After: reject early, then the happy path reads top-to-bottom
def process(data):
if data is None:
raise TypeError("Data is None")
if not data.is_valid():
raise ValueError("Invalid data")
if not data.has_permission():
raise PermissionError("No permission")
return do_work(data)
Same errors, same behavior, same edge cases — only the shape changed.
| Rationalization | Reality |
|---|---|
| "It's working, no need to touch it" | Working code that's hard to read will be hard to fix when it breaks. Simplifying now saves time on every future change. |
| "Fewer lines is always simpler" | A one-line nested conditional is not simpler than a five-line explicit branch. Simplicity is comprehension speed, not line count. |
| "I'll just quickly simplify this unrelated code too" | Unscoped simplification creates noisy diffs and risks regressions in code you didn't intend to change. Stay focused. |
| "The types make it self-documenting" | Types document structure, not intent. A well-named function explains why better than a signature explains what. |
| "This abstraction might be useful later" | Speculative abstraction is complexity without value. Remove it; re-add it when a second use actually appears. |
| "The original author must have had a reason" | Maybe — check the history (Chesterton's Fence). But complexity is often just the residue of iteration under pressure. |
| "I'll refactor while adding this feature" | Mixed changes are harder to review, revert, and understand in history. Separate them. |
| "The tests need a small tweak to pass" | Tests failing means you changed behavior. Revert the simplification, don't bend the tests. |
| "Removing the guard deletes a test, so the user has to approve it" | Not when the guard and the test are both new in the change and no call site reaches the guard. That test is part of the dead path, not a pin. Remove both, run the rest, and report it. |
| "It changes what the function does when called directly" | Only the test calls it that way. Behavior is what the code's own callers reach. |
| "I can't prove the state is impossible, data comes from elsewhere" | Read what the call sites pass and how the code stores it. "Someday a record might arrive malformed" is not a caller. |
| "I don't delete tests without asking" | That covers tests on the base. A test this change wrote for a path this change wrote is its own draft. |
| "There are no tests and we ship tomorrow, so I'll just be careful" | Careful is not proof. Ask the user for permission to add pinning tests; if they decline, say the change is unproven. Pressure is when refactors break things. |
| "I built the expected value with the project's own helper, so the test stays consistent" | Then the test and the code share one source and cannot disagree. Freeze a literal. |
| "A test already covers this function, so it's pinned" | Only if it can fail. Check it for both faults first; a tautological test or change detector is flagged in the report and leaves the function uncovered. |
| "The test is called 'caches preferences', so calling the loader once is the contract" | A test name is not a promise. Pin the cache by its result: change what the source returns between two calls and assert the second call still gets the first value. |
| "The query count is an effect on the database, so it is observable" | No caller reads it. It goes red on a harmless extra query and gets bumped. Pin the rows; if growth is the worry, assert the count is the same for one row and for many. |
| "Asserting the full output is the most thorough pin" | It pins wording and layout no caller relies on, and goes red on the next harmless rewrite. Pin the facts a caller reads. |
| "I verified it with a throwaway script, then cleaned up" | A deleted check guards nothing. The next edit to that function has no test. Either the tests stay, or the report says the change is unpinned. |
| "I split it into helpers, so it's simpler" | Only if each helper decides one nameable thing and has its own tests. Relocating lines into once-called helpers with vague names is fragmentation. |
| "Those two functions are basically the same, I'll merge them while I'm here" | Merging crosses scope and changes an API surface. Diff them, count callers, propose it. A near-duplicate is sometimes two things that change for different reasons. |
| "Complexity is subjective" | It is a count. Count the decision points, report the number before and after. |
| "The subagent already checked it" | A finder's report is a lead. Open the location, recount, re-diff, re-run — then act. |
| "I'll spin up a dozen agents, it's faster" | Faster for you, paid by the user. Ask how much parallelism they want, and use cheap tiers for finding. |
| "The summary in SKILL.md is enough, I'll skip the companion file" | The summary is a reminder of text you are supposed to have read. Read the four files on first use, and the named one again at its step. |
| "Reading the whole module is more thorough than grepping" | For finding, grep is thorough and cheap. Read whole files for meaning, not for locating. |
| "The comment explains the history of this fix" | The reader sees today's code. State the constraint in the present tense; the story goes in the commit message. |
| "The count is accurate, I just checked" | Accurate today; nothing re-checks it when the next one lands. Name the set instead. |
After completing a simplification pass:
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub patrickdappollonio/claude-plugins --plugin code-simplification