Skip to content
OpenSmartRoute
Skillv1.0.0

refactoring-patterns

Apply safe refactoring patterns to improve code structure without changing behavior. Use when cleaning up code, reducing technical debt, or improving maintainability.

by proffesor-for-testing(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from proffesor-for-testing/agentic-qe (.claude/skills/refactoring-patterns/SKILL.md). Install upstream with npx skills add proffesor-for-testing/agentic-qe --skill refactoring-patterns. Copyright stays with the author.

Refactoring Patterns

<default_to_action> When refactoring:

  1. ENSURE tests pass (never refactor without tests)
  2. MAKE small change (one refactoring at a time)
  3. RUN tests (must stay green)
  4. COMMIT (save progress)
  5. REPEAT

Safe Refactoring Cycle:

npm test               # Green ✅
# Make ONE small change
npm test               # Still green ✅
git commit -m "refactor: extract calculateTotal"
# Repeat

Code Smells → Refactoring:

Smell Refactoring
Long method (>20 lines) Extract Method
Large class Extract Class
Long parameter list (>3) Introduce Parameter Object
Duplicated code Extract Method/Class
Complex conditional Decompose Conditional
Magic numbers Named Constants
Nested loops Replace Loop with Pipeline

NEVER REFACTOR:

  • Without tests (write tests first)
  • When deadline is tomorrow
  • Code you don't understand
  • Code that works and won't be touched </default_to_action>

Quick Reference Card

Common Refactorings

Pattern Before After
Extract Method 50-line function 5 small functions
Extract Class Class doing 5 things 5 single-purpose classes
Parameter Object fn(a,b,c,d,e,f) fn(options)
Replace Conditional if (type === 'a') {...} Polymorphism
Pipeline Nested loops .filter().map().reduce()

The Rule of Three

  1. First time → Just do it
  2. Second time → Wince and duplicate
  3. Third time → Refactor

Key Patterns

Extract Method

// Before: Long method
function processOrder(order) {
  // 50 lines of validation, calculation, saving, emailing...
}

// After: Clear responsibilities
function processOrder(order) {
  validateOrder(order);
  const pricing = calculatePricing(order);
  const saved = saveOrder(order, pricing);
  sendConfirmationEmail(saved);
  return saved;
}

Replace Loop with Pipeline

// Before
let results = [];
for (let item of items) {
  if (item.inStock) {
    results.push(item.name.toUpperCase());
  }
}

// After
const results = items
  .filter(item => item.inStock)
  .map(item => item.name.toUpperCase());

Decompose Conditional

// Before
if (order.total > 1000 && customer.isPremium && allInStock(order)) {
  return 'FREE_SHIPPING';
}

// After
function isEligibleForFreeShipping(order, customer) {
  return isLargeOrder(order) &&
         isPremiumCustomer(customer) &&
         allInStock(order);
}

Refactoring Anti-Patterns

❌ Anti-Pattern Problem ✅ Better
Without tests No safety net Write tests first
Big bang Rewrite everything Small incremental steps
For perfection Endless tweaking Good enough, move on
Premature abstraction Pattern not clear yet Wait for Rule of Three
During feature work Mixed changes Separate commits

Agent Integration

// Detect code smells
const smells = await Task("Detect Code Smells", {
  source: 'src/services/',
  patterns: ['long-method', 'large-class', 'duplicate-code']
}, "qe-quality-analyzer");

// Safe refactoring with test verification
await Task("Verify Refactoring", {
  beforeCommit: 'abc123',
  afterCommit: 'def456',
  expectSameBehavior: true
}, "qe-test-executor");

Agent Coordination Hints

Memory Namespace

aqe/refactoring/
├── smells/*          - Detected code smells
├── suggestions/*     - Refactoring recommendations
├── verifications/*   - Behavior preservation checks
└── history/*         - Refactoring log

Fleet Coordination

const refactoringFleet = await FleetManager.coordinate({
  strategy: 'refactoring',
  agents: [
    'qe-quality-analyzer',   // Identify targets
    'qe-test-generator',     // Add safety tests
    'qe-test-executor',      // Verify behavior
    'qe-test-refactorer'     // TDD refactor phase
  ],
  topology: 'sequential'
});

Related Skills


Remember

Refactoring is NOT:

  • Adding features
  • Fixing bugs
  • Performance optimization
  • Rewriting from scratch

Refactoring IS:

  • Improving structure
  • Making code clearer
  • Reducing complexity
  • Removing duplication
  • Without changing behavior

Always have tests. Always take small steps. Always keep tests green.

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/proffesor-for-testing-agentic-qe-refactoring-patterns/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

proffesor-for-testing-agentic-qe-refactoring-patterns.ocm.jsonjson
{
  "ocm": "1",
  "id": "proffesor-for-testing-agentic-qe-refactoring-patterns",
  "kind": "skill",
  "name": "refactoring-patterns",
  "description": "Apply safe refactoring patterns to improve code structure without changing behavior. Use when cleaning up code, reducing technical debt, or improving maintainability.",
  "publisher": "proffesor-for-testing",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "coding"
    ],
    "tags": [
      "skill-md",
      "refactoring",
      "code-quality",
      "technical-debt",
      "maintainability",
      "clean-code",
      "skills-sh"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "Apply safe refactoring patterns to improve code structure without changing behavior. Use when cleaning up code, reducing technical debt, or improving maintainability."
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "skills.sh",
      "repository": "https://github.com/proffesor-for-testing/agentic-qe",
      "path": ".claude/skills/refactoring-patterns/SKILL.md",
      "ref": "HEAD",
      "url": "https://github.com/proffesor-for-testing/agentic-qe/blob/HEAD/.claude/skills/refactoring-patterns/SKILL.md",
      "key": "proffesor-for-testing/agentic-qe/.claude/skills/refactoring-patterns/SKILL.md"
    }
  },
  "instructions": "# Refactoring Patterns\n\n<default_to_action>\nWhen refactoring:\n1. ENSURE tests pass (never refactor without tests)\n2. MAKE small change (one refactoring at a time)\n3. RUN tests (must stay green)\n4. COMMIT (save progress)\n5. REPEAT\n\n**Safe Refactoring Cycle:**\n```bash\nnpm test               # Green ✅\n# Make ONE small change\nnpm test               # Still green ✅\ngit commit -m \"refactor: extract calculateTotal\"\n# Repeat\n```\n\n**Code Smells → Refactoring:**\n| Smell | Refactoring |\n|-------|-------------|\n| Long method (>20 lines) | Extract Method |\n| Large class | Extract Class |\n| Long parameter l",
  "cost": {
    "context_tokens": 1146
  }
}

Fetch it by URL: GET /api/v1/registry/proffesor-for-testing-agentic-qe-refactoring-patterns/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.