home / skills / citypaul / .dotfiles / refactoring

This skill helps you assess refactoring opportunities after tests pass, ensuring improvements are safe, justified, and well-documented.

npx playbooks add skill citypaul/.dotfiles --skill refactoring

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.2 KB
---
name: refactoring
description: Refactoring assessment and patterns. Use after tests pass (GREEN phase) to assess improvement opportunities.
---

# Refactoring

Refactoring is the third step of TDD. After GREEN, assess if refactoring adds value.

## When to Refactor

- Always assess after green
- Only refactor if it improves the code
- **Commit working code BEFORE refactoring** (critical safety net)

### Commit Before Refactoring - WHY

Having a working baseline before refactoring:
- Allows reverting if refactoring breaks things
- Provides safety net for experimentation
- Makes refactoring less risky
- Shows clear separation in git history

**Workflow:**
1. GREEN: Tests pass
2. COMMIT: Save working code
3. REFACTOR: Improve structure
4. COMMIT: Save refactored code

## Priority Classification

| Priority | Action | Examples |
|----------|--------|----------|
| Critical | Fix now | Mutations, knowledge duplication, >3 levels nesting |
| High | This session | Magic numbers, unclear names, >30 line functions |
| Nice | Later | Minor naming, single-use helpers |
| Skip | Don't change | Already clean code |

## DRY = Knowledge, Not Code

**Abstract when**:
- Same business concept (semantic meaning)
- Would change together if requirements change
- Obvious why grouped together

**Keep separate when**:
- Different concepts that look similar (structural)
- Would evolve independently
- Coupling would be confusing

## Example Assessment

```typescript
// After GREEN:
const processOrder = (order: Order): ProcessedOrder => {
  const itemsTotal = order.items.reduce((sum, item) => sum + item.price, 0);
  const shipping = itemsTotal > 50 ? 0 : 5.99;
  return { ...order, total: itemsTotal + shipping, shippingCost: shipping };
};

// ASSESSMENT:
// ⚠️ High: Magic numbers 50, 5.99 → extract constants
// ✅ Skip: Structure is clear enough
// DECISION: Extract constants only
```

## Speculative Code is a TDD Violation

If code isn't driven by a failing test, don't write it.

**Key lesson**: Every line must have a test that demanded its existence.

❌ **Speculative code examples:**
- "Just in case" logic
- Features not yet needed
- Code written "for future flexibility"
- Untested error handling paths

**What to do**: Delete speculative code. Add behavior tests instead.

---

## When NOT to Refactor

Don't refactor when:

- ❌ Code works correctly (no bug to fix)
- ❌ No test demands the change (speculative refactoring)
- ❌ Would change behavior (that's a feature, not refactoring)
- ❌ Premature optimization
- ❌ Code is "good enough" for current phase

**Remember**: Refactoring should improve code structure without changing behavior.

---

## Commit Messages for Refactoring

```
refactor: extract scenario validation logic
refactor: simplify error handling flow
refactor: rename ambiguous parameter names
```

**Format**: `refactor: <what was changed>`

**Note**: Refactoring commits should NOT be mixed with feature commits.

---

## Refactoring Checklist

- [ ] All tests pass without modification
- [ ] No new public APIs added
- [ ] Code more readable than before
- [ ] Committed separately from features
- [ ] Committed BEFORE refactoring (safety net)
- [ ] No speculative code added
- [ ] Behavior unchanged (tests prove this)

Overview

This skill performs a refactoring assessment and recommends concrete changes after tests are green. It inspects code structure, duplication, naming, complexity and speculative code to prioritize safe improvements. The goal is to preserve behavior while increasing readability and maintainability. It emphasizes committing a working baseline before any refactor work.

How this skill works

After detecting a GREEN test phase, the skill analyzes the code for risk patterns: duplicated knowledge, long functions, magic numbers, deep nesting, and untested speculative code. It classifies issues into priority levels (Critical, High, Nice, Skip) and produces a focused checklist and suggested commit message for the change. The skill refuses to propose behavior-changing edits and highlights when a change would require new tests.

When to use it

  • Immediately after tests pass (GREEN phase) and before merging
  • When you need to improve readability or reduce technical debt without changing behavior
  • Before a larger feature, to make code safer to extend
  • When you spot duplicated business logic or confusing names
  • When code smells like speculative or dead logic

Best practices

  • Always commit the working code before refactoring to provide a revert point
  • Only refactor if it demonstrably improves structure or reduces duplication
  • Avoid speculative refactoring — require a test that demands the new code
  • Keep refactoring commits isolated and use clear messages (format: refactor: <what changed>)
  • Use the priority classification to scope work in a single session

Example use cases

  • Extract constants for magic numbers found in shell scripts (High priority)
  • Split a 40-line shell function into smaller helpers with tests (High priority)
  • Remove duplicated config parsing logic across dotfiles and consolidate into a single helper (Critical/High)
  • Delete unneeded speculative branches or flags that lack test coverage (Speculative code violation)
  • Rename ambiguous parameters and variables to reveal intent before adding features

FAQ

Should I always refactor after tests pass?

Assess every time tests pass, but only refactor when it improves clarity, removes duplication, or reduces risk. If it’s not clearly beneficial, skip it.

What if refactoring requires behavior changes?

Behavior changes are not refactoring. Add or change tests first, then implement the behavior change in a separate feature commit.

How do I handle small cleanups vs large refactors?

Classify by priority: do Critical and High in the current session, schedule Nice items later, and skip already-clean code. Keep commits small and focused.