Skip to content
Back to skills

Collection

ASecurity

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

  • 24 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added September 8, 2026
code-qualityjavascripttypescriptgojavabashrefactoringcode-reviewgitperformance

Security analysis

A100/100

Pro scans all 21 files and shows the line behind each finding

Scanned September 8, 2026

npx -y skills add mattnigh/skills_collection --skill collection --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Collection?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Collection
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/mattnigh-collection-dffe4a2b/badge)](https://www.skillsdirectory.com/skills/mattnigh-collection-dffe4a2b)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
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."
category: development-practices
priority: medium
tokenEstimate: 1000
agents: [qe-code-reviewer, qe-quality-analyzer, qe-test-refactorer]
implementation_status: optimized
optimization_version: 1.0
last_optimized: 2025-12-03
dependencies: []
quick_reference_card: true
tags: [refactoring, code-quality, technical-debt, maintainability, clean-code]
---

# 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:**
```bash
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
```javascript
// 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
```javascript
// 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
```javascript
// 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

```typescript
// 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
```typescript
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
- [tdd-london-chicago](../tdd-london-chicago/) - TDD refactor phase
- [code-review-quality](../code-review-quality/) - Review refactored code
- [xp-practices](../xp-practices/) - Collective ownership

---

## 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.**

Files in this skill

  • 0Chan-smc__claude-code-workflow-lab__claude__skills__frontend-dev-guidelines__SKILL.md15.1 KB
  • 17hz__nextjs-template__claude__skills__example-skill__SKILL.md316 B
  • 1ambda__dataops-platform__claude__skills__context-synthesis__SKILL.md3.5 KB
  • 1natsu172__dotfiles__claude__skills__git-analysis__SKILL.md5.4 KB
  • 1natsu172__dotfiles__claude__skills__github-pr-best-practices__SKILL.md7.7 KB
  • 23Maestro__prospect-pipeline__claude__skills__npid-fastapi-skill.md26.1 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-code-javascript__SKILL.md15.7 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-code-python__SKILL.md17.5 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-expression-syntax__SKILL.md9.4 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-mcp-tools-expert__SKILL.md12.5 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-node-configuration__SKILL.md16.6 KB
  • 360AYA25__ClaudeN8N__claude__skills__n8n-workflow-patterns__SKILL.md11.2 KB
  • 3x-Projetos__claude-memory-framework__claude__skills__scientist__SKILL.md14.8 KB
  • 5MinFutures__futures-arena__claude__skills__migration-tracker__SKILL.md16.2 KB
  • 5MinFutures__futures-arena__claude__skills__planning-guidelines__SKILL.md11.8 KB
  • 92Bilal26__TaskPilotAI__claude__skills__assessment-builder__SKILL.md17.5 KB
  • 92Bilal26__TaskPilotAI__claude__skills__book-scaffolding__SKILL.md19.1 KB
  • 92Bilal26__TaskPilotAI__claude__skills__code-validation-sandbox__SKILL.md6.2 KB
  • 92Bilal26__TaskPilotAI__claude__skills__exercise-designer__SKILL.md18.1 KB
  • 92Bilal26__TaskPilotAI__claude__skills__learning-objectives__SKILL.md24.5 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…