askill
refactoring

refactoringSafety --Repository

When to refactor vs leave alone, code smell detection, safe refactoring steps, and common transformation patterns. Use when asked to refactor code, clean up technical debt, or when reviewing code that shows structural problems.

1 stars
1.2k downloads
Updated 2/6/2026

Package Files

Loading files...
SKILL.md

Refactoring

Decision Tree

Code looks messy → Should you refactor?
    ├─ Is it blocking the current task? → Yes → Refactor the minimum needed
    ├─ Is it actively causing bugs? → Yes → Refactor with tests first
    ├─ Will you touch this code again soon? → Yes → Refactor now (Boy Scout Rule)
    ├─ Is it working and rarely changed? → Leave it alone
    ├─ Does the user explicitly ask for refactoring? → Yes → Refactor per their scope
    └─ Is it just "not how I'd write it"? → Leave it alone

Code Smells → Refactoring

SmellHow to DetectRefactoring
Long function (>30 lines)Scrolling to read itExtract Method — pull out logical blocks
Duplicate codeSame 3+ lines in 2+ placesExtract shared function or module
Deep nesting (>3 levels)Arrow-shaped codeEarly returns, guard clauses, extract helper
God class (does everything)500+ lines, 10+ methodsSplit by responsibility into focused classes
Primitive obsessionPassing 5+ params, string IDs everywhereIntroduce value objects or typed IDs
Feature envyMethod uses another class's data more than its ownMove method to the class it envies
Shotgun surgeryOne change requires editing 10 filesGroup related logic into one module
Dead codeUnused imports, unreachable branchesDelete it (git has history if you need it back)
Magic numbers/stringsif (status === 3)Extract named constant: STATUS_ACTIVE = 3
Boolean blindnessdoThing(true, false, true)Use options object or named parameters

Safe Refactoring Process

1. Ensure tests exist (write them first if they don't)
2. Make ONE small change
3. Run tests
4. Commit
5. Repeat

Never refactor and change behavior in the same commit. Refactoring = same behavior, better structure. New behavior = new commit.

Common Refactoring Patterns

Extract Early Returns (flatten nesting)

// Before
function process(user) {
  if (user) {
    if (user.active) {
      if (user.hasPermission) {
        return doWork(user);
      }
    }
  }
  return null;
}

// After
function process(user) {
  if (!user) return null;
  if (!user.active) return null;
  if (!user.hasPermission) return null;
  return doWork(user);
}

Replace Conditional with Polymorphism

// Before
function getArea(shape) {
  switch (shape.type) {
    case 'circle': return Math.PI * shape.radius ** 2;
    case 'rectangle': return shape.width * shape.height;
  }
}

// After — each shape knows how to calculate its own area
class Circle { getArea() { return Math.PI * this.radius ** 2; } }
class Rectangle { getArea() { return this.width * this.height; } }

Replace Magic Values with Constants

// Before
if (retries > 3) { setTimeout(fn, 5000); }

// After
const MAX_RETRIES = 3;
const RETRY_DELAY_MS = 5000;
if (retries > MAX_RETRIES) { setTimeout(fn, RETRY_DELAY_MS); }

Anti-Patterns

Anti-PatternFix
Refactoring without testsWrite tests first, then refactor
Big-bang rewriteIncremental, one smell at a time
Refactoring code you won't touch againLeave working code alone
Mixing refactoring with feature workSeparate commits: refactor, then feature
Premature abstractionWait for 3 instances before extracting (Rule of Three)
Renaming everything for styleOnly rename if current name is misleading
Refactoring in a time crunchAdd a TODO, come back when there's time
"Just a quick cleanup" that touches 50 filesScope it down — small, reviewable changes

Install

Download ZIP
Requires askill CLI v1.0+

AI Quality Score

AI review pending.

Metadata

Licenseunknown
Version-
Updated2/6/2026
PublisherBigPapiCB

Tags

No tags yet.