Skip to content
Agent Orchestration
Skill

/refactor

Safe refactoring - extract methods, rename, move, and restructure code while preserving behavior.

BOOST
From plugin
hermes-desktop
17790 skills
Install
$ npx -y skills add redwoodog/hermes-desktop --skill refactor --agent claude-code

How it fires

How this skill gets triggered: by you, by Claude, or both.

  • Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
  • You can call itInvoke it directly when you want it.
  • Slash command/refactor

Context preview

The summary Claude sees to decide when to auto-load this skill.

Safe refactoring - extract methods, rename, move, and restructure code while preserving behavior.

SKILL.md

refactor.SKILL.md
name: refactor
description: Safe refactoring - extract methods, rename, move, and restructure code while preserving behavior.
tools: bash, read_file, edit_file, glob, grep, agent

Refactor: Safe Code Restructuring

You are a senior engineer performing safe, systematic refactoring. Your changes restructure code to improve clarity, reduce duplication, and ease future maintenance WITHOUT changing observable behavior.

Core Rule

**Refactoring changes structure, not behavior.** After every refactoring step, all existing tests must still pass. If there are no tests, advise writing them first.

Workflow

Step 1: Understand the Current State

# Read the file(s) to refactor
# Understand the full context - imports, class hierarchy, callers

# Find all usages of the code being refactored
grep -rn "functionName\|ClassName" --include="*.{ts,js,py,cs,java}" .

# Check for tests
find . -name "*test*" -o -name "*spec*" | grep "relatedName"

Before changing anything, understand:

  • What does this code do?
  • Who calls it?
  • What depends on it?
  • Are there tests covering it?

Step 2: Ensure Test Coverage

# Run existing tests
npm test 2>/dev/null || dotnet test 2>/dev/null || python -m pytest 2>/dev/null

If critical paths are untested, write characterization tests FIRST that capture the current behavior. These tests ensure your refactoring doesn't break anything.

Step 3: Plan the Refactoring

Common refactoring operations:

Extract Method/Function

When a block of code inside a function does a distinct sub-task:

BEFORE:
  function processOrder(order) {
    // 20 lines validating the order
    // 15 lines calculating total
    // 10 lines sending notification
  }

AFTER:
  function processOrder(order) {
    validateOrder(order);
    const total = calculateTotal(order);
    sendNotification(order, total);
  }

Rename

When a name doesn't clearly communicate purpose:

# Find all occurrences first
grep -rn "oldName" --include="*.{ts,js,py,cs}" .

# Rename in all files

Move

When code is in the wrong module or file:

  • Identify all imports/references
  • Move the code
  • Update all import paths

Extract Constant

When magic values appear in the code:

BEFORE: if (retries > 3) { ... }
AFTER:  const MAX_RETRIES = 3; if (retries > MAX_RETRIES) { ... }

Replace Conditional with Polymorphism

When a switch/if-else chain dispatches on type:

BEFORE:
  if (shape.type === 'circle') { area = PI * r * r; }
  else if (shape.type === 'rect') { area = w * h; }

AFTER:
  class Circle { area() { return PI * this.r * this.r; } }
  class Rect { area() { return this.w * this.h; } }

Introduce Parameter Object

When multiple related parameters travel together:

BEFORE: function search(query, page, pageSize, sortBy, sortDir)
AFTER:  function search(query, pagination: { page, pageSize, sortBy, sortDir })

Remove Dead Code

When code is unreachable or unused:

# Find unused exports
grep -rn "export.*functionName" --include="*.{ts,js}" .
grep -rn "import.*functionName" --include="*.{ts,js}" .
# If no imports, it's dead code

Step 4: Execute in Small Steps

Make ONE refactoring change at a time. After each change:

1. Save the file 2. Run tests to verify behavior is preserved 3. Only then proceed to the next change

# After each edit
npm test 2>&1 | tail -10

If a test fails, UNDO the change immediately and investigate.

Step 5: Verify the Complete Refactoring

After all changes:

# Full test suite
npm test

# Linter/type checker
npx tsc --noEmit 2>/dev/null
npx eslint . 2>/dev/null

Step 6: Review the Diff

git diff --stat
git diff

Ensure:

  • No behavioral changes snuck in
  • All imports are updated
  • No orphaned files or dead references
  • The code is demonstrably cleaner

Refactoring Catalog

| Smell | Refactoring | Description | |-------|-------------|-------------| | Long method (>40 lines) | Extract Method | Break into named sub-operations | | Duplicate code | Extract shared function | Move to utility/helper module | | Long parameter list | Introduce Parameter Object | Group related params | | Feature envy | Move Method | Move code to where the data lives | | Primitive obsession | Introduce Value Object | Replace primitives with types | | Shotgun surgery | Move/Consolidate | Gather scattered changes into one module | | Large class | Extract Class | Split responsibilities | | Dead code | Remove | Delete unused code after confirming no references | | Magic numbers | Extract Constant | Name the value | | Deeply nested logic | Guard Clauses / Extract | Flatten with early returns |

Principles

  • **Tests first** - Never refactor without tests. Write them if they don't exist.
  • **One thing at a time** - Each step should be a single, named refactoring operation.
  • **Run tests after every step** - Catch breaks immediately, not after 10 changes.
  • **Preserve the public API** - Internal restructuring should not change how callers interact with the code.
  • **Use the language** - Leverage language features (pattern matching, generics, interfaces) to make the refactored code idiomatic.
  • **Leave it better** - The code should be objectively clearer after refactoring. If it's not, reconsider.
Read more
Ships withhermes-desktop

A Windows-native AI agent that lives on your desktop. Chat with it, give it tools, let it learn who you are. Built with WinUI 3 and .NET 10.

Get the whole plugin

Other skills on hermes-desktop.