Skip to content

commandCode quality

Simplify Code

Cuts the length and nesting of one function without changing what it does, and proves it with before and after tests.

npx workspacetemplates add simplify-code --tool claude-code

Writes 1 file to .claude/commands/

.claude/commands/simplify-code.md14 lines · 1.2 KBFile
---
description: Cuts the length and nesting of one function without changing what it does, and proves it with before and after tests.
argument-hint: "[function name or file:line]"
---

Simplify this function without changing behavior: $ARGUMENTS

1. Read the function and its callers. Record the starting numbers: lines, deepest nesting level, count of branches.
2. Make sure tests cover it. If a path is untested, add a test for it first, using real inputs from the code. Run them.
3. Apply, in this order, and stop when the code reads cleanly: guard clauses instead of nested else; remove branches that cannot happen; merge duplicated branches; replace flag variables with direct returns; use the language's built-ins (map, filter, any, get with default) over hand loops; delete unused variables and parameters that no caller passes.
4. Keep the same inputs, outputs, exceptions and side effects, including their order. If a change might alter behavior in an edge case, do not make it; list it instead.
5. Run the tests after the edit and compare with the starting numbers.

Show the old and new function side by side with the numbers. Do not touch other functions or ask to commit before showing the diff.

What it does

Deeply nested code with flags and repeated branches is hard to read and easy to break. This command targets a single function and removes the accidental complexity: early returns, dead branches, needless variables.

It measures the result in plain numbers, such as line count and maximum nesting, and keeps the function signature and side effects exactly the same.

Example

you › /simplify-code parseDiscountRules in src/cart/rules.js

agent › Before: 74 lines, nesting 5, 11 branches. After: 38 lines, nesting 2, 6 branches. Used guard clauses and removed 2 unreachable cases.
9 existing tests plus 2 new ones pass.
Not changed: the silent catch on line 52, it may be relied on.

Questions

How is this different from the Refactor command?
It works on one function only and aims at fewer lines and less nesting, while Refactor can restructure across modules in several steps.
Could it change behavior by accident?
It runs tests before and after, adds missing ones, and lists risky edge-case changes instead of applying them.
  • Add Error Handling

    CommandCode quality

    Free
  • Add Logging

    CommandCode quality

    Free
  • Add Types

    CommandCode quality

    Free
  • Extract Function

    CommandCode quality

    Free