skills/improve-code-structure/SKILL.md
Analyzes and improves code structure — decomposes long functions and files, reduces complexity, extracts shared patterns, assesses test coverage on critical paths, and cleans up dead, unreachable, or orphaned code that accumulates as a side effect of refactoring. Use when the user wants to refactor for clarity, split large files, reduce nesting, DRY up code, improve testability, or sweep for dead code after restructuring. Not for feature changes, bug fixes, or performance optimization — this is structural refactoring only.
npx skillsauth add shhac/skills improve-code-structureInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Analyze code for structural improvements, then implement changes with user approval.
You are the lead orchestrating a structural analysis and refactoring workflow.
For three worked examples of how the lead should behave end-to-end (full happy path, Phase 3 skipped because no changes were approved, and a revert-on-fail in Phase 3d), see references/examples.md.
Copy this checklist into your first response and tick items off as you progress. It is your authoritative progress record across all four phases:
- [ ] Phase 1a — spawn one analyst subagent per lens, in parallel (every lens in references/lenses.md, no skipping)
- [ ] Phase 1b — synthesize findings into a prioritized plan
- [ ] Phase 1c — present the plan and wait for user approval
- [ ] Phase 2a — record baseline ref before the first change
- [ ] Phase 2b — implement approved changes sequentially, verifying each
- [ ] Phase 3 — dead code detection & cleanup (skip if Phase 2 made no changes)
- [ ] Phase 4 — final verification + cross-phase summary
Spawn one subagent per lens listed in references/lenses.md, in parallel. Every lens must run — count the lens sections in lenses.md and confirm you spawned that many subagents before continuing. Skipping a lens because it "seems unlikely to find anything" is not allowed; each lens covers a distinct failure mode and the value comes from running the full sweep. If a lens genuinely doesn't apply (e.g. the test-coverage lens against a project with zero tests), the analyst will report "no findings" — that's the correct outcome, not a reason to skip.
Each subagent receives:
Tell each analyst to focus on impact (the changes that genuinely improve clarity, testability, or maintainability — not every long function) and to note dependencies between findings (e.g. "splitting this function creates a new shared utility candidate"). Output uses the Finding Record schema, without the Verdict field.
Once all analysts report:
Present the prioritized plan and wait for approval — the user picks which recommendations to implement (all, some, or none). Do not proceed past this point without explicit approval. The pause mechanism depends on the harness (interactive prompt, return to caller, etc.) — what matters is that no Phase 2 changes happen until approval is in.
The lead implements approved changes directly, in dependency order.
Before the first change: record the baseline ref by running git rev-parse HEAD and remembering the SHA. Phase 3a's scan scope depends on diffing against this exact ref, so it must be captured before any modifications. If the working tree is dirty at the start of Phase 2, ask the user to commit, stash, or explicitly accept that the existing changes will be folded into the baseline.
package.json exports/main/bin, Cargo.toml [lib]/[[bin]], the equivalent for the ecosystem in use). If a refactor would change any interface, surface it before making the change.Sequentially, one logical change at a time — don't batch a file split, a function extraction, and a dedup into a single step; each should be independently verifiable. For each change:
After Phase 2 makes changes, dead code accumulates as a side effect — orphaned exports, unreachable branches, unused imports, files no one imports anymore.
Skip if Phase 2 made no changes. Otherwise, follow references/dead-code-cleanup.md. The phase is a five-step pipeline:
git diff --name-only <baseline-ref>...HEAD) plus their pre/post-refactor import neighbors.confirmed-dead / not-dead / uncertain.The full procedure — including the trap checklists, verdict dispatch table, named accumulators, and removal preconditions — is in references/dead-code-cleanup.md.
After all approved changes and dead-code removals are complete:
development
Audit a codebase's module boundaries — enumerate modules, map their seams (import edges between modules), produce a layered topology diagram, and classify each module as narrow, hub-by-design, or accidental hub (with separate flags for cycles, layer violations, and uncertain import graphs). Outputs a diagram plus a flagged-for-review list; does not change code. Use when assessing whether abstractions live at the right boundaries, before/after a refactor to verify the boundaries improved, or when an unfamiliar codebase needs an architectural map. Not for intra-module refactoring (see improve-code-structure), bug hunting, or feature work.
testing
Investigate and solve problems using a team of specialist agents. Use when facing complex, multi-faceted problems that benefit from parallel research and structured implementation.
tools
Sync a forked repository with its upstream. Fetches both remotes, shows divergence, resets shared branches to upstream, re-merges local-only branches, cleans up branches already merged upstream, and pushes. Use when upstream has accepted PRs or moved ahead and you need to bring your fork in line.
data-ai
Manage stacked branches — rebase cascades, detect landed PRs, show stack status. Use when branches are stacked (B on A on main), trunk has advanced, a mid-stack branch changed, or a PR has landed and descendants need rebasing. Lightweight alternative to Graphite that infers the stack from git history.