agentleFS
AvinashPAvinashP/AgentsAtlas/skills/refactoring/SKILL.md

refactoring

Skill7 starsChanged 9 months agoMITView on GitHub

Refactors code safely with test verification. Use when asked to refactor, restructure, clean up, or improve code organization.

39 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use performance, ci-cd and 8 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.
  • pluginagentmarketplaceSkill

    performance

    Optimize application performance through caching strategies, load balancing, database scaling, and monitoring. Build systems handling thousands of concurrent users.

    Also in 7 projects that use this

  • JohnJBorenSkill

    ci-cd

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

    Also in 4 projects that use this

  • MicrosoftSkill

    error-handling

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

    Also in 6 projects that use this

  • CloudAI-XSkill

    database-design

    Designs database schemas, indexing strategies, query optimization, and migration patterns for SQL and NoSQL databases. Use when designing tables, optimizing queries, fixing N+1 problems, planning migrations, or when asked about database performance, normalization, ORMs, or data modeling.

    Also in 5 projects that use this

  • MicrosoftSkill

    api-design

    Guide for designing and documenting RESTful APIs. Use when asked to design an API, create endpoints, or document an API.

    Also in 14 projects that use this

  • AnthropicSkill

    documentation

    Write and maintain technical documentation. Trigger with "write docs for", "document this", "create a README", "write a runbook", "onboarding guide", or when the user needs help with any form of technical writing — API docs, architecture docs, or operational runbooks.

    Also in 8 projects that use this

The file

We split this skill into the rules it gives an agent

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

  • 0

    Common

    Most of the 34 edited versions we read keep these.

  • 6

    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:

    If no tests cover the code:

    line 23
  2. Distinctive:

    Write characterization tests first

    line 24
  3. Distinctive:

    Don't combine multiple refactorings

    line 74
  4. Distinctive:

    Don't "fix bugs" while refactoring

    line 75
  5. Distinctive:

    Don't add features while refactoring

    line 76
  6. Distinctive:

    Wait for duplication to appear 3 times

    line 232

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

Restructure code safely while preserving behavior.

Workflow

1. Verify Test Coverage

Before refactoring, ensure tests exist:

# Run existing tests
npm test
pytest
go test ./...

If no tests cover the code:

  1. Write characterization tests first
  2. These capture current behavior (even if buggy)
  3. Then refactor with confidence

2. Understand Current Structure

Map the code:

  • What are the dependencies?
  • Who calls this code?
  • What does it depend on?
  • What's the data flow?
# Find usages
grep -r "functionName" --include="*.ts"

# Find dependencies
grep -r "import.*from.*module" --include="*.ts"

3. Plan the Refactoring

Identify the target state:

  • What's the problem with current code?
  • What's the desired structure?
  • What's the smallest step toward that?

Choose your approach:

SituationApproach
Large functionExtract methods
Duplicated codeExtract shared function
Complex conditionalsReplace with polymorphism
Long parameter listIntroduce parameter object
Feature envyMove method to data's class
Data clumpExtract class

4. Execute in Small Steps

The safe refactoring cycle:

1. Make ONE small change
2. Run tests
3. Commit if green
4. Repeat

Never skip steps:

  • Don't combine multiple refactorings
  • Don't "fix bugs" while refactoring
  • Don't add features while refactoring

5. Verify Behavior Preserved

# Run full test suite
npm test

# Check for regressions
npm run test:e2e

# Manual smoke test if needed

Quick Reference

Safe Refactorings (Automated)

Most IDEs can do these automatically:

RefactoringShortcut (VS Code)
RenameF2
Extract functionCtrl+Shift+R
Extract variableCtrl+Shift+R
Inline variableCtrl+Shift+R
Move to fileDrag in explorer

Common Patterns

Extract Function

// Before
function processOrder(order) {
  // validate
  if (!order.items.length) throw new Error('Empty order');
  if (!order.customer) throw new Error('No customer');

  // calculate total
  let total = 0;
  for (const item of order.items) {
    total += item.price * item.quantity;
  }

  // apply discount
  if (order.coupon) {
    total *= (1 - order.coupon.discount);
  }

  return { ...order, total };
}

// After
function processOrder(order) {
  validateOrder(order);
  const total = calculateTotal(order);
  return { ...order, total };
}

function validateOrder(order) {
  if (!order.items.length) throw new Error('Empty order');
  if (!order.customer) throw new Error('No customer');
}

function calculateTotal(order) {
  const subtotal = order.items.reduce(
    (sum, item) => sum + item.price * item.quantity,
    0
  );
  return order.coupon
    ? subtotal * (1 - order.coupon.discount)
    : subtotal;
}

Replace Conditional with Polymorphism

// Before
function getSpeed(vehicle) {
  switch (vehicle.type) {
    case 'car': return vehicle.horsepower * 0.5;
    case 'bike': return vehicle.gears * 5;
    case 'boat': return vehicle.engineSize * 2;
  }
}

// After
class Car {
  getSpeed() { return this.horsepower * 0.5; }
}
class Bike {
  getSpeed() { return this.gears * 5; }
}
class Boat {
  getSpeed() { return this.engineSize * 2; }
}

Introduce Parameter Object

// Before
function createUser(name, email, age, country, role) {
  // ...
}

// After
function createUser({ name, email, age, country, role }) {
  // ...
}

// Or with type
interface CreateUserParams {
  name: string;
  email: string;
  age: number;
  country: string;
  role: string;
}

function createUser(params: CreateUserParams) {
  // ...
}

Code Smells to Address

SmellRefactoring
Long methodExtract method
Large classExtract class
Duplicate codeExtract function
Long parameter listParameter object
Switch statementsPolymorphism
Feature envyMove method
Data clumpExtract class
Primitive obsessionValue object
Comments explaining codeExtract well-named method

Anti-Patterns

Don't:

  1. Refactor and change behavior simultaneously
    • Refactoring = same behavior, different structure
    • Keep them separate
  2. Make big-bang changes
    • Small steps with tests passing between each
  3. Refactor without tests
    • Write characterization tests first
  4. Over-abstract too early
    • Wait for duplication to appear 3 times
  5. Refactor "just in case"
    • Refactor for a reason (readability, extensibility needed now)

See Also

  • patterns.md - Detailed refactoring patterns with examples

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.