Find duplicated and redundant code, extract it to the shared layer that already owns it, and simplify the result without changing behavior. Use only when the user explicitly invokes $x-tidy-code.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of X Tidy Code?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/ttstack-x-tidy-code)
---
name: x-tidy-code
description: Find duplicated and redundant code, extract it to the shared layer that already owns it, and simplify the result without changing behavior. Use only when the user explicitly invokes $x-tidy-code.
---
# Tidy code
Before preflight, read `.agents/skill-rules/common.md` and then
`.agents/skill-rules/x-tidy-code.md` from the Git root if present.
Use the skill-specific rules for repository details such as checks or release
criteria. Apply them within the selected scope and the user's authorization.
Continue with the available rules when either file is absent.
Resolve all paths from the Git top level; Markdown links are relative to this
file. Read the invocation arguments from the user prompt.
Behavior-preserving refactor. It may move, merge, and delete code but must not
change observable behavior, public contracts, or persisted formats. It does not
commit, push, or bump a version; run `$x-commit` afterwards to commit the
result.
## Input
Optional paths or a named target (a crate, module, package, or symbol). With no
target, inventory the whole workspace and rank duplication by cost: lines
duplicated, number of copies, churn, and the blast radius of consolidating.
With a target, follow that code and the call sites that share its shape.
## Preflight
Run `git status --short --branch` and record the baseline. Build and run the
tests and linters for the affected surface **before** any edit and record the
result — a clean baseline is the precondition for a behavior-preserving
refactor. Freeze the paths this pass may change and preserve unrelated edits.
## Protocol
1. **Find candidates.** Search for near-duplicate logic: copied-and-patched
functions, parallel match/if chains, repeated literal tables, format strings
and magic numbers, duplicated configuration parsing, and copy-pasted tests.
Use structural search and `git log -S` for versions that diverged over time.
Record each cluster with its locations and its real differences — a copy
that already drifted is a finding, not just a candidate.
2. **Locate the owner.** Decide where the shared logic belongs by asking who
already owns that responsibility: an existing module, crate, package, or
utility layer. Prefer an existing seam over creating a new abstraction. Do
not invent a `common`/`utils` dump, and never introduce an abstraction whose
only caller set is the duplicates you are about to delete.
3. **Check the differences.** For each cluster, classify the differences as
accidental (safe to converge) or intentional (encode them as a parameter,
a trait/interface, or a documented variant). An intentional difference that
cannot be expressed cleanly is a reason to leave the copies alone and
report it, not a reason to force a merge.
4. **Refactor.** Extract the shared logic to the owner, keep names and
signatures stable where they are public, and update every call site in the
same change. Do one cluster per coherent unit; keep behavior identical,
including error messages, ordering, and edge cases that tests encode.
5. **Verify.** Run the tests and linters again and compare with the recorded
baseline. Confirm no public API, persisted format, or wire format changed;
if one would have to, stop and report instead. Remove any now-dead code and
update documentation and examples that referenced the old shape.
6. **Report.** State each cluster, its locations before and after, what moved
to which owner, the intentional differences preserved, the net line and
duplication change, and the validation compared against the baseline.
## Stop conditions
Stop for a cluster when the shared extraction would change observable
behavior, require a public or persisted-format change, or need a new
abstraction layer that the codebase does not already justify. Leave it in place
and report it rather than force a merge.