agentleFS
larchankalarchanka/skills/refactoring/SKILL.md

refactoring

Skill50 starsChanged 7 days agoMITView on GitHub

Executes the 'Then Make it Right' discipline: pins behavior, eliminates duplication, enforces CRAP < 10, runs mutation gates, and improves architecture without altering observable behavior.

58 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use ci-cd, dependency-management and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • JohnJBorenSkill

    ci-cd

    CI/CD pipeline design, GitHub Actions, testing automation, and deployment strategies

    Also in 5 projects that use this

  • dkprogSkill

    dependency-management

    Apply when wiring dependencies, integrating external services, or designing interfaces between modules. Ensures loose coupling through dependency injection, interface segregation, and proper abstraction boundaries.

    Also in 3 projects that use this

  • javalenciacaiSkill

    multi-agent-orchestration

    Design and coordinate multi-agent systems where specialized agents work together to solve complex problems. Covers agent communication, task delegation, workflow orchestration, and result aggregation. Use when building coordinated agent teams, complex workflows, or systems requiring specialized expertise across domains.

    Also in 3 projects that use this

  • harrissohail6Skill

    database-design

    Database schema design and SQL best practices. Use when designing tables, writing queries, or optimizing database performance.

    Also in 5 projects that use this

  • MicrosoftSkill

    error-handling

    统一错误处理系统。在添加 API 端点、修改错误处理、添加前端 API 调用、编写错误相关测试时使用。

    Also in 7 projects that use this

  • klauswgSkill

    performance

    Optimize web performance for faster loading and better user experience. Use when asked to "speed up my site", "optimize performance", "reduce load time", "fix slow loading", "improve page speed", or "performance audit".

    Also in 7 projects that use this

The file

Show its rules and text

We split this skill into the rules it gives an agent

16 rules. 0 are common, 16 are distinctive, and 0 are very specific to this project.

  • 0

    Common

    Most of the 53 edited versions we read keep these.

  • 16

    Distinctive

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Pin what the code does: Ensure existing tests (or characterization tests) cover the current behavior.

    line 14
  2. Distinctive:

    Prove the behavior did not change: All tests must stay green.

    line 16
  3. Distinctive:

    If Cyclomatic Complexity ($\text{CC}$) is $\ge 10$, the function will fail even with 100% test coverage—it *must* be decomposed.

    line 27
  4. Distinctive:

    Killed mutant: Test suite fails (Good—test verified behavior).

    line 36
  5. Distinctive:

    Surviving mutant: Test suite passes (Bad—weak assertion or dead code).

    line 37
  6. Distinctive:

    Put the domain in a structure that makes illegal states impossible to represent.

    line 61
  7. Distinctive:

    If existing code is already clear and local, leave it alone.

    line 63
  8. Distinctive:

    Check data where it enters the system boundaries.

    line 66
  9. Distinctive:

    Keep business logic in pure, deterministic functions without side-effects.

    line 68
  10. Distinctive:

    Do not silence compiler, type-checker, or linter warnings with suppressions.

    line 73
  11. Distinctive:

    Ensure running an operation twice, or running it after a crash halfway through, results in the exact same state.

    line 76
  12. Distinctive:

    If two components do not need the same mutable state, give each its own.

    line 79
  13. Distinctive:

    If they do need shared state, allow only one writer, and enforce that single writer strictly in the structure.

    line 80
  14. Distinctive:

    Run the project's test suite.

    line 87
  15. Distinctive:

    Verify that the refactored code and tests leave zero surviving mutants.

    line 95
  16. Distinctive:

    Run the full test suite and confirm 100% pass rate.

    line 97

“Common” is measured: kept by at least 6 in 10 of the 53 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Refactoring ("Then Make it Right")

Refactoring is the disciplined process of changing internal code structure without changing its external observable behavior. It follows the Red $\to$ Green minimal implementation ("Make it Work") to ensure code health, architectural integrity, and long-term maintainability across any programming language.

The Core Rule

  1. Pin what the code does. Ensure existing tests (or characterization tests) cover the current behavior.
  2. Change the structure. Refactor code toward cleaner abstractions, lower complexity, and single responsibility.
  3. Prove the behavior did not change. All tests must stay green. A failing test during refactoring is a regression, not progress.

The Three Automated Quality Gates

To prevent subjective or superficial refactoring, enforce the three automated quality gates using the native tools of the target project's ecosystem:

1. risk-gate (Complexity & CRAP Score)

  • Rule: Reduce each function under CRAP 10 by splitting it, by testing it, or both. Consider whether case statements are worth reducing.
  • The Formula: $\text{CRAP} = \text{CC}^2 \times (1 - \text{coverage})^3 + \text{CC}$.
    • If Cyclomatic Complexity ($\text{CC}$) is $\ge 10$, the function will fail even with 100% test coverage—it must be decomposed.

2. clone-check (Structural Duplication)

  • Rule: Eliminate structural duplication that ranks 0.8 or above (or $\ge 80%$).
  • Extract repeated logic, algorithmic patterns, or mirrored workflows into clean, reusable functions or shared classes.

3. mutant-gate (Mutation Testing)

  • Rule: Cover the uncovered. Kill the survivors. Delete dead code.
  • Injects programmatic mutations (inverting comparison operators, arithmetic, or booleans) to verify that tests actually assert behavior.
    • Killed mutant: Test suite fails (Good—test verified behavior).
    • Surviving mutant: Test suite passes (Bad—weak assertion or dead code).

Native Tooling Matrix by Language

Execute the corresponding native CLI tool for the project being refactored:

Ecosystemrisk-gate ($\text{CRAP} / \text{CC} < 10$)clone-check (Duplication $< 0.8$)mutant-gate (Mutation Kill)
Universal (150+ langs)Check linter complexity rulesnpx jscpd --threshold 80 <path>—
TypeScript / JavaScriptnpx eslint --rule 'complexity: ["error", 10]'npx jscpd --threshold 80 src/npx stryker run
Gogocyclo -over 10 . or golangci-lint runnpx jscpd --threshold 80 .go-mutesting ./...
Rustcargo clippy -- -D clippy::cognitive_complexitynpx jscpd --threshold 80 src/cargo mutants
Pythonradon cc -s -n B . or flake8 --max-complexity 10npx jscpd --threshold 80 .mutmut run
Java / JVMCheckstyle CyclomaticComplexity or Sonarnpx jscpd --threshold 80 src/mvn org.pitest:pitest-maven:mutationCoverage

Architectural Disciplines

Refactoring must drive the code toward the following architectural standards:

Model the Domain

  • Put the domain in a structure that makes illegal states impossible to represent.
  • Maintain a glossary of domain terms in documentation or domain models.
  • If existing code is already clear and local, leave it alone.

Boundary Discipline

  • Check data where it enters the system boundaries.
  • Inside system boundaries, trust it.
  • Keep business logic in pure, deterministic functions without side-effects.

Type System Discipline

  • Make the illegal value impossible to write.
  • Parse outside data once, where it comes in (parse, don't validate).
  • Do not silence compiler, type-checker, or linter warnings with suppressions.

Make Operations Idempotent

  • Ensure running an operation twice, or running it after a crash halfway through, results in the exact same state.

Separate Before Serializing Shared State

  • If two components do not need the same mutable state, give each its own.
  • If they do need shared state, allow only one writer, and enforce that single writer strictly in the structure.

Step-by-Step Refactoring Workflow

  1. Verify Baseline Tests: Run the project's test suite. Ensure all tests pass. If tests do not exist for the code being refactored, write characterization tests to lock in current behavior before touching code.
  2. Run Quality Gates: Execute the ecosystem's risk-gate and clone-check commands to identify high-risk functions ($\text{CRAP} \ge 10$) and structural duplicates ($\ge 0.8$).
  3. Execute Surgical Refactorings:
    • Split complex methods into cohesive pure helper functions.
    • Eliminate duplication by extracting shared logic.
    • Apply domain and type system discipline.
  4. Run mutant-gate: Verify that the refactored code and tests leave zero surviving mutants.
  5. Prove Invariance: Run the full test suite and confirm 100% pass rate.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

Reviews can't be read right now.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.