agentleFS
IsNoobgrammerIsNoobgrammer/skills-for-agents/skills/refactor/SKILL.md

refactor

Skill4 starsChanged 6 months agoMITView on GitHub

Use this skill to refactor messy, monolithic, or legacy codebases into clean, modular structures. Triggers on "refactor my project", "clean up my code", "split this file", or "restructure my files". Always use this before the `harden` skill if the code is not yet modular.

111 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use modern-go, to-design 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.
  • jidechaoSkill

    modern-go

    Modernize Go code by applying version-appropriate idioms and APIs (gofix-style transformations). Scans go.mod for the Go version, then transforms Go source files to use modern patterns—from Go 1.0 through 1.26+. Use when the user says "现代化","现代Go语言", "地道的", "idiomatic", "modernize", "modern-go", "update Go code", "gofix", or wants to upgrade Go idioms.

    Also in 3 projects that use this

  • jidechaoSkill

    to-design

    Generate a design document (design proposal) from a PRD, in the style of Go's official design proposals — Abstract / Background / Design / Rationale / Compatibility / Implementation, heavy on the 'why' and tradeoffs. Triggers on: to-design, prd-to-design, prd转设计文档, 生成设计文档, 写设计文档, design doc, design proposal, 设计提案, 技术设计文档.

    Also in 3 projects that use this

  • chidekinaSkill

    test-gen

    Generate test cases from type constraints and specs. Creates boundary tests, edge cases, and fixtures automatically. Pact-lang inspired. Triggers on: generate tests, test from spec, create fixtures, boundary tests.

    Also in 5 projects that use this

  • pvnarpSkill

    incident

    Guides incident response and post-mortem analysis. Covers triage, mitigation, root cause analysis, and prevention. Use when responding to active incidents, writing post-mortems, or improving incident processes.

    Also in 6 projects that use this

  • platformplatformSkill

    db-query

    Query the local Postgres database of the active Aspire worktree via psql.

    Also in 3 projects that use this

  • JetBrainsSkill

    fix-bug

    End-to-end bug fix workflow for the Exposed project. Accepts a GitHub issue (#NUMBER), YouTrack issue (EXPOSED-NUMBER), or YouTrack URL (https://youtrack.jetbrains.com/issue/EXPOSED-NUMBER). Fetches the issue, creates a failing reproducer test, commits on a new branch, implements the fix, validates, and opens a PR. Use this skill whenever the user wants to fix a bug from an issue tracker, mentions a EXPOSED issue number, or references a GitHub issue to fix.

    Also in 6 projects that use this

  • +2 more from jidechao

The file

Show its rules and text

We split this skill into the rules it gives an agent

13 rules. 0 are common, 12 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 94 edited versions we read keep these.

  • 12

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Single Responsibility: Every file and function must have one job.

    line 34
  2. Distinctive:

    Hard Limit: No file should exceed 300 lines.

    line 35
  3. Very specific:

    Use index files (index.js) for barrel exports.

    line 63
  4. Distinctive:

    Ensure all imports are correctly updated.

    line 64
  5. Distinctive:

    Remove unused imports and dead code.

    line 65
  6. Distinctive:

    All original routes and UI components are preserved.

    line 72
  7. Distinctive:

    No file exceeds the 300-line limit.

    line 73
  8. Distinctive:

    No business logic was lost or rewritten.

    line 74
  9. Distinctive:

    Does not rewrite business logic unless it's broken

    line 80
  10. Distinctive:

    Does not handle security hardening (defers to harden)

    line 81
  11. Distinctive:

    Does not add new features — restructures existing ones only

    line 82
  12. Distinctive:

    Does not handle deployment or CI/CD (defers to documenter)

    line 83
  13. Distinctive:

    Does not change public API contracts without explicit approval

    line 84

“Common” is measured: kept by at least 6 in 10 of the 94 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

Refactor

[!IMPORTANT] This skill has reference files in the references/ directory. You MUST read them at least once to understand the deep-dive content (Modular Architectures, Refactoring Patterns, Code Quality Standards) and call them whenever you need specific information from there.

You are a FAANG-level staff engineer reviewing a legacy codebase. Your mission is to refactor it into a clean, modular, and maintainable structure without breaking existing business logic.


When to Use

  • "My code is a mess" or "This file is too large (300+ lines)".
  • Moving from a monolithic single-file script to a proper project structure.
  • Extracting duplicated logic into reusable components or services.
  • Goal: Establish a foundation that can support 1M+ users and zero-downtime deploys.

Core Instructions

  1. Analyze First: Map the project, identify files > 300 lines, and detect missing separation of concerns (e.g., routes doing DB work) — because files past 300 lines have sharply decreased reviewability (cognitive load research shows review quality drops ~50% beyond this threshold).
  2. Modular Layout: Target a standard folder structure (/components, /pages, /services, /utils, /routes, etc.) adapted to the stack — because recognized patterns reduce onboarding friction and search time for every future contributor.
  3. Single Responsibility: Every file and function must have one job. Extract helpers if a function exceeds 80 lines — because 80 lines is roughly the limit of working memory for a single function; beyond that, reviewers lose track of control flow.
  4. Hard Limit: No file should exceed 300 lines. Split by sub-domain if necessary — because 300 lines is where diff reviews start missing bugs and merge conflicts spike.
  5. No Breaking Changes: Preserve all existing functionality. Do not rewrite business logic unless it is clearly broken — because silent behavior changes are the #1 source of post-refactor regressions.

Phase 1 — Project Health Report

Produce a report including:

  • Stack Detected: Frameworks and database.
  • Files Needing Split: List oversized files and their line counts.
  • Risks: Scale and maintenance risks in the current structure.

Confirm with the user before proceeding.


Phase 2 — Refactoring Plan

Produce a full plan including:

  • New Folder Structure: A visualization of the target layout.
  • File-by-File Mapping: Original file → New file(s).
  • Reasoning: Why these specific changes are being made.

Phase 3 — Code Execution

Output the refactored files in full.

  • Use index files (index.js) for barrel exports.
  • Ensure all imports are correctly updated.
  • Remove unused imports and dead code.

Phase 4 — Verification Checklist

Provide a final report confirming:

  • All original routes and UI components are preserved.
  • No file exceeds the 300-line limit.
  • No business logic was lost or rewritten.

Boundaries

  • Does not rewrite business logic unless it's broken
  • Does not handle security hardening (defers to harden)
  • Does not add new features — restructures existing ones only
  • Does not handle deployment or CI/CD (defers to documenter)
  • Does not change public API contracts without explicit approval

Composability — Working With Other Skills

See PROTOCOL.md (SIP) at skills root for full interop contract.

Domain Declaration

domain: process
composable: true
yields_to: []

Refactor owns process — the structural organization and architectural skeleton of the codebase.

When Refactor Leads

  • Structural overhauls, file splitting, or folder restructuring tasks.
  • Early-stage cleanup of legacy or "messy" projects.

When Refactor Defers

Other Skill's DomainWhat Refactor Does
VoiceRefactor provides the architectural plan; the voice skill handles the tone of the health reports.
Craft (e.g. harden)Refactor sets the stage. If harden is also active, Refactor focuses on where things go, while harden focuses on what goes inside them (pooling, caching, etc.).

Layered Composition Rules

  1. Process (Refactor) + Craft (Harden): Refactor is the primary skill. It defines the folder structure first. Harden follows and fills those folders with production-ready patterns.

Pipeline Behavior

  • Upstream: Often triggered after a requirements gathering phase or when a developer realizes a project has outgrown its current structure.
  • Downstream: Feeds into harden for production readiness or testing for verification.

[!IMPORTANT] Reminder: This skill has reference files in the references/ directory. If you need specific architectural templates, design pattern references, or modularity checklists, you MUST call and read the relevant reference files.

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.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

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.