MCP HubMCP Hub
스킬 목록으로 돌아가기

refactoring-patterns

wondelai
업데이트됨 2 days ago
4 조회
1,096
121
1,096
GitHub에서 보기
테스팅testingapidesign

정보

이 스킬은 Extract Method나 Replace Conditional과 같은 명명된 리팩토링 패턴을 적용하여 동작을 보존하면서 코드 구조를 안전하게 개선합니다. 레거시 코드 정리나 기능 준비 과정에서 개발자가 코드 스멜, 기술 부채 또는 특정 리팩토링 작업을 언급할 때 트리거됩니다. 이 프레임워크는 안전한 변환 순서와 테스트 가드를 통해 스멜 기반 변환을 제공합니다.

빠른 설치

Claude Code

추천
기본
npx skills add wondelai/skills -a claude-code
플러그인 명령대체
/plugin add https://github.com/wondelai/skills
Git 클론대체
git clone https://github.com/wondelai/skills.git ~/.claude/skills/refactoring-patterns

Claude Code에서 이 명령을 복사하여 붙여넣어 스킬을 설치하세요

문서

Refactoring Patterns Framework

A disciplined approach to improving the internal structure of existing code without changing its observable behavior. Apply these named transformations when reviewing code, reducing technical debt, or preparing code for new features. Every refactoring follows the same loop: verify tests pass, apply one small structural change, verify tests still pass.

Core Principle

Refactoring is not rewriting. It is a sequence of small, behavior-preserving transformations, each backed by tests. You never change what the code does -- you change how the code is organized. The discipline of taking tiny verified steps is what makes refactoring safe. Big-bang rewrites fail because they combine structural change with behavioral change, making it impossible to know which broke things.

The foundation: Bad code is not a character flaw -- it is a natural consequence of delivering features under time pressure. Code smells are objective signals that structure has degraded. Named refactorings are the proven mechanical recipes for fixing each smell. The catalog of smells tells you where to look; the catalog of refactorings tells you what to do.

Scoring

Goal: 10/10. When reviewing or refactoring code, rate the structural quality 0-10 based on adherence to the principles below. A 10/10 means: no obvious smells remain, each function does one thing, names reveal intent, duplication is eliminated, and the test suite covers the refactored paths. Always provide the current score and specific refactorings needed to reach 10/10.

The Refactoring Patterns Framework

Six areas of focus for systematically improving code structure:

1. Code Smells as Triggers

Core concept: Code smells are surface indicators of deeper structural problems. They are not bugs -- the code works -- but they signal that the design is making the code harder to understand, extend, or maintain. Each smell maps to one or more named refactorings that fix it.

Why it works: Without a shared vocabulary of smells, code review devolves into subjective "I don't like this." Named smells give teams objective criteria: "This is Feature Envy -- the method uses six fields from another class and only one of its own." The name points directly to the fix.

Key insights:

  • Smells cluster into five families: Bloaters, Object-Orientation Abusers, Change Preventers, Dispensables, and Couplers
  • Long Method is the most common smell and the gateway to most other refactorings
  • Duplicate Code is the single biggest driver of maintenance cost
  • A method that needs a comment to explain what it does is a smell -- extract and name the block instead
  • Shotgun Surgery (one change requires edits in many classes) and Divergent Change (one class changes for many reasons) are opposites that both signal misplaced responsibilities
  • Primitive Obsession -- using raw strings, ints, or arrays instead of small domain objects -- causes errors and duplication throughout the codebase

Code applications:

ContextPatternExample
Method > 10 linesExtract MethodPull the loop body into calculateLineTotal()
Class > 200 linesExtract ClassMove shipping logic into a ShippingCalculator
Switch on type codeReplace Conditional with PolymorphismCreate subclasses for each order type
Multiple methods use same paramsIntroduce Parameter ObjectGroup startDate, endDate into DateRange
Method uses another object's dataMove MethodMove calculateDiscount() to the Customer class
Copy-pasted logicExtract Method + Pull Up MethodShare via a common method or base class

See: references/smell-catalog.md

2. Composing Methods

Core concept: Most refactoring starts here. Long methods are broken into smaller, well-named pieces. Each extracted piece should do one thing and its name should say what that thing is. The goal is methods you can read like prose -- a sequence of high-level steps, each delegating to a clearly named helper.

Why it works: Short methods with intention-revealing names eliminate the need for comments, make bugs obvious (each method is small enough to verify at a glance), and enable reuse. The cognitive cost of a method call is near zero when the name tells you everything.

Key insights:

  • Extract Method is the single most important refactoring -- master it first
  • If you feel the urge to write a comment, extract the code block and use the comment as the method name
  • Inline Method when a method body is as clear as the name -- indirection without value is noise
  • Replace Temp with Query when a temporary variable holds a computed value that is used in multiple places
  • Split Temporary Variable when one variable is reused for two different purposes
  • Replace Method with Method Object when a method is too tangled to extract from (many local variables referencing each other)

Code applications:

ContextPatternExample
Block with a commentExtract Method// check eligibility becomes isEligible()
Temp used onceInline VariableRemove const price = order.getPrice() if used once
Temp used in multiple placesReplace Temp with QueryReplace let discount = getDiscount() with method calls
Temp assigned twice for different reasonsSplit Temporary VariableIntroduce perimeterWidth and perimeterHeight
Trivial delegating methodInline MethodInline moreThanFiveDeliveries() if it's return deliveries > 5 and only used once
Complex method with many localsReplace Method with Method ObjectMove the method into its own class where locals become fields

See: references/composing-methods.md

3. Moving Features Between Objects

Core concept: The key decision in object-oriented design is where to put responsibilities. When a method or field is in the wrong class -- evidenced by Feature Envy, excessive coupling, or unbalanced class sizes -- move it to where it belongs.

Why it works: Well-placed responsibilities reduce coupling and increase cohesion. When a method lives in the class whose data it uses, changes to that data affect only one class. Misplaced methods create invisible dependencies that cause Shotgun Surgery.

Key insights:

  • Move Method when a method uses more features of another class than its own
  • Move Field when a field is used more by another class than the class it lives in
  • Extract Class when one class does two things -- split along the axis of change
  • Inline Class when a class does too little to justify its existence
  • Hide Delegate to enforce the Law of Demeter -- a client shouldn't navigate a chain of objects
  • Remove Middle Man when a class does nothing but forward calls
  • The tension between Hide Delegate and Remove Middle Man is resolved case by case: hide the delegate when the chain is unstable; remove the middle man when forwarding becomes the entire class

Code applications:

ContextPatternExample
Method envies another classMove MethodMove calculateShipping() from Order to ShippingPolicy
Field used by another class constantlyMove FieldMove discountRate from Order to Customer
God class with 500+ linesExtract ClassPull Address fields and methods into their own class
Tiny class with one fieldInline ClassMerge PhoneNumber back into Contact if no behavior
Client calls a.getB().getC()Hide DelegateAdd a.getCThroughB() so client doesn't know about C
Class only forwards callsRemove Middle ManLet client call the delegate directly

See: references/moving-features.md

4. Organizing Data

Core concept: Raw data -- magic numbers, exposed fields, type codes represented as integers, parallel arrays -- creates subtle bugs and scatters domain knowledge. Replace primitive representations with objects that encapsulate behavior and enforce invariants.

Why it works: An int representing a currency amount has no concept of rounding rules, currency codes, or formatting. A Money object encapsulates all of that. When domain concepts are represented as first-class objects, business rules live in one place, validation happens automatically, and the type system catches errors at compile time.

Key insights:

  • Replace Magic Number with Symbolic Constant as the simplest data refactoring -- it names the intent
  • Replace Data Value with Object (Primitive Obsession cure) -- wrap strings and numbers in domain objects (EmailAddress, Money, Temperature)
  • Encapsulate Field -- never expose a raw field; a getter/setter allows you to add validation, logging, or computation later
  • Encapsulate Collection -- return an unmodifiable view; never let callers mutate your internal list
  • Replace Type Code with Subclasses when the type code affects behavior; use Strategy when subclassing is impractical
  • Change Value to Reference when you need identity semantics (one shared Customer object, not copies)

Code applications:

ContextPatternExample
if (status == 2)Replace Magic Number with Symbolic Constantif (status == ORDER_SHIPPED)
String email passed everywhereReplace Data Value with ObjectCreate EmailAddress class with validation
Public fieldEncapsulate FieldReplace order.total with order.getTotal()
Getter returns mutable listEncapsulate CollectionReturn Collections.unmodifiableList(items)
int typeCode with switchReplace Type Code with SubclassesEmployee -> Engineer, Manager, Salesperson
Duplicated customer recordsChange Value to ReferenceShare one Customer instance via a registry

See: references/organizing-data.md

5. Simplifying Conditional Logic

Core concept: Complex conditionals -- deeply nested if/else trees, long switch statements, null checks scattered everywhere -- are the hardest code to read and the most likely to contain bugs. Named refactorings decompose, consolidate, and replace conditionals with clearer structures.

Why it works: A conditional with six branches and nested sub-conditions requires the reader to simulate every path mentally. Decomposing the condition into well-named methods makes each branch self-documenting. Replacing conditionals with polymorphism eliminates entire categories of "forgot to handle this case" bugs.

Key insights:

  • Decompose Conditional: extract the condition, the then-branch, and the else-branch into named methods
  • Consolidate Conditional Expression: merge multiple conditions that lead to the same result into one named check
  • Replace Nested Conditional with Guard Clauses: handle edge cases early and return, leaving the main path unindented
  • Replace Conditional with Polymorphism: the gold standard for type-based conditionals -- each type knows its own behavior
  • Introduce Special Case (Null Object): eliminate if (x == null) checks by providing an object that represents "nothing" with safe default behavior
  • Introduce Assertion: make assumptions explicit so they fail fast in development

Code applications:

ContextPatternExample
Long if with complex conditionDecompose ConditionalExtract isSummer(date) and summerCharge()
Multiple ifs return same valueConsolidate ConditionalCombine into isDisabled() returning early
Deeply nested if/elseReplace with Guard ClausesCheck edge cases first, return early, flatten the main path
Switch on object typeReplace Conditional with PolymorphismEach type implements its own calculatePay()
if (customer == null) everywhereIntroduce Special CaseCreate NullCustomer with default behavior
Hidden assumption in codeIntroduce Assertionassert quantity > 0 at method entry

See: references/simplifying-conditionals.md

6. Safe Refactoring Workflow

Core concept: Refactoring is only safe when wrapped in tests. The workflow is mechanical: run tests (green), apply one small transformation, run tests (green), commit. If tests go red, revert the last change -- don't debug a broken refactoring.

Why it works: Small steps make it trivial to find what went wrong (it was the last thing you did). Reverting a failed step costs seconds. Debugging a failed big-bang rewrite costs days. Frequent commits create save points you can return to.

Key insights:

  • The refactoring cycle: test -> refactor -> test -> commit (repeat)
  • Rule of Three: tolerate duplication once, note it twice, refactor on the third occurrence
  • Preparatory refactoring: restructure code to make the feature easy before adding the feature
  • Comprehension refactoring: refactor to understand code as you read it -- leave it clearer than you found it
  • Litter-pickup refactoring: small improvements whenever you touch a file (Boy Scout Rule)
  • When NOT to refactor: when it's easier to rewrite from scratch, when there are no tests and adding them first isn't feasible, or when the code will be deleted soon
  • Refactoring and performance: refactor for clarity first, then profile and optimize the measured bottleneck -- refactored code is easier to tune because the hot path is isolated
  • Branch by Abstraction and Parallel Change enable large refactorings in production systems without feature branches

Code applications:

ContextPatternExample
About to add a featurePreparatory RefactoringExtract method to make new feature's insertion point clean
Reading unfamiliar codeComprehension RefactoringRename variables and extract methods to understand intent
Saw a small issue while workingLitter-Pickup RefactoringFix the smell before moving on (Boy Scout Rule)
Third copy of same logicRule of ThreeExtract the shared logic into a common method
Large API change in productionBranch by AbstractionIntroduce abstraction layer, migrate callers, remove old path
Renaming a widely-used methodParallel ChangeAdd new name, deprecate old, migrate callers, remove old

See: references/refactoring-workflow.md

Common Mistakes

MistakeWhy It FailsFix
Refactoring without testsNo safety net -- you can't tell if behavior changedWrite characterization tests first, then refactor
Big-bang rewrite instead of incremental stepsCombines structural and behavioral changes; impossible to debugTake the smallest step possible, run tests after each
Refactoring and adding features at the same timeTwo hats at once -- you can't verify either change in isolationSeparate the hats: refactor first (commit), then add feature (commit)
Renaming without updating all callersBreaks the build or introduces dead codeUse IDE rename refactoring; search for all references
Extracting too many tiny methodsCreates indirection without clarity when names are poorEach extracted method must have a name that removes the need to read the body
Ignoring the smell catalogReinventing fixes instead of applying proven recipesLearn the named smells; each one maps to specific refactorings
Refactoring code that will be deletedWasted effort -- polish on condemned codeAsk first: is this code's lifespan long enough to justify the investment?
Optimizing prematurely during refactoringConflates clarity with performance; optimized code is often harder to readRefactor for clarity first, then profile, then optimize the measured hot path only

Quick Diagnostic

QuestionIf NoAction
Do tests pass before you start?You have no safety netWrite or fix tests first -- do not refactor without green tests
Can you name the smell you're fixing?You're refactoring by instinct, not by catalogIdentify the smell from the catalog, then apply its prescribed refactoring
Is each method under ~10 lines?Long Methods are likely presentApply Extract Method to break long methods into named steps
Does each class have a single reason to change?Divergent Change or Large Class smellApply Extract Class to separate responsibilities
Are there duplicated code blocks?Duplicate Code is the most expensive smellExtract shared logic into a common method or base class
Do conditionals use polymorphism where appropriate?Switch Statements or complex if/else trees remainApply Replace Conditional with Polymorphism
Are you committing after each refactoring step?You risk losing work and mixing changesCommit after every green-to-green transformation
Is the code easier to read after your change?The refactoring may have added complexityRevert and try a different approach

Reference Files

  • smell-catalog.md: Comprehensive catalog of code smells organized by family -- Bloaters, Object-Orientation Abusers, Change Preventers, Dispensables, and Couplers -- with detection heuristics and fix mappings
  • composing-methods.md: Extract Method, Inline Method, Extract Variable, Inline Variable, Replace Temp with Query, Split Temporary Variable, Remove Assignments to Parameters, Replace Method with Method Object -- motivation, mechanics, and examples
  • moving-features.md: Move Method, Move Field, Extract Class, Inline Class, Hide Delegate, Remove Middle Man -- when and how to redistribute responsibilities between objects
  • organizing-data.md: Replace Data Value with Object, Change Value to Reference, Replace Array with Object, Replace Magic Number, Encapsulate Field, Encapsulate Collection, Replace Type Code with Class/Subclasses/Strategy
  • simplifying-conditionals.md: Decompose Conditional, Consolidate Conditional, Replace Nested Conditional with Guard Clauses, Replace Conditional with Polymorphism, Introduce Special Case, Introduce Assertion -- with before/after examples
  • refactoring-workflow.md: The refactoring cycle, when to refactor, when NOT to refactor, refactoring and performance, Branch by Abstraction, Parallel Change

Further Reading

This skill is based on the definitive guide to improving the design of existing code:

About the Author

Martin Fowler is the Chief Scientist at Thoughtworks and one of the most influential voices in software engineering. He is the author of Refactoring: Improving the Design of Existing Code (1999, 2nd edition 2018), which introduced the concept of named, catalog-based refactoring transformations to mainstream software development. Fowler is also the author of Patterns of Enterprise Application Architecture, UML Distilled, and numerous influential articles on software design, agile methodology, and continuous delivery. He was a signatory of the Agile Manifesto and has spent decades advocating for evolutionary design -- the practice of continuously improving code structure through disciplined, incremental refactoring rather than upfront big design. His refactoring catalog, originally written in Java, has been adapted to virtually every programming language and is built into the automated refactoring tools of every major IDE.

GitHub 저장소

wondelai/skills
경로: refactoring-patterns
0
agent-skillsai-skillsclaude-codeclaude-code-marketplaceclaude-code-pluginclaude-code-skills

연관 스킬

evaluating-llms-harness

테스팅

이 Claude Skill은 MMLU, GSM8K를 포함한 60개 이상의 표준화된 학술 과제에서 LLM 성능을 벤치마크하기 위해 lm-evaluation-harness를 실행합니다. 개발자들이 모델 품질을 비교하고, 학습 진행 상황을 추적하거나 학술 결과를 보고할 수 있도록 설계되었습니다. 이 도구는 HuggingFace와 vLLM 모델을 포함한 다양한 백엔드를 지원합니다.

스킬 보기

cloudflare-cron-triggers

테스팅

이 스킬은 cron 표현식을 사용하여 Worker를 스케줄링하기 위한 Cloudflare Cron Triggers 구현에 관한 포괄적인 지식을 제공합니다. 주기적 작업, 유지보수 작업, 자동화된 워크플로우 설정 방법을 다루며, 잘못된 cron 표현식이나 시간대 문제 같은 일반적인 이슈들을 해결하는 방법을 포함합니다. 개발자들은 이를 통해 스케줄된 핸들러 구성, cron 트리거 테스트, Workflows 및 Green Compute와의 연동 작업을 수행할 수 있습니다.

스킬 보기

webapp-testing

테스팅

이 Claude Skill은 Python 스크립트를 통해 로컬 웹 애플리케이션을 테스트하기 위한 Playwright 기반 툴킷을 제공합니다. 프론트엔드 검증, UI 디버깅, 스크린샷 캡처, 로그 확인 기능을 지원하며 서버 라이프사이클을 관리합니다. 브라우저 자동화 작업에 사용하되 컨텍스트 오염을 방지하기 위해 소스 코드를 읽지 않고 스크립트를 직접 실행하세요.

스킬 보기

finishing-a-development-branch

테스팅

이 스킬은 테스트 통과를 확인한 후 체계적인 통합 옵션을 제시하여 개발자가 완성된 작업을 마무리하도록 돕습니다. 구현이 완료된 후 머지, PR 생성, 브랜치 정리와 같은 워크플로우를 안내합니다. 코드가 준비되고 테스트가 완료되었을 때 개발 프로세스를 체계적으로 마무리하기 위해 사용하세요.

스킬 보기