skills/refactoring/SKILL.md
Safely restructure code without changing behavior using Extract Method, Rename, Move Method techniques. Use when preparing code for new features, improving code quality incrementally, cleaning up messy code, reducing duplication, or simplifying complex logic. Make sure to use this skill whenever the user mentions refactoring, cleaning up code, making code more readable, splitting large files or methods, or removing code smells.
npx skillsauth add elct9620/ai-coding-skills refactoringInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
| Condition | Pass | Fail | |-----------|------|------| | Existing code modification | Need to change existing code | Writing new code only | | Code comprehension issues | Code hard to understand/extend | Code is clear | | Feature preparation | Preparing for new functionality | Direct implementation possible | | Incremental improvement | Improving quality step by step | No improvement needed |
Apply when: Any condition passes
1. Ensure tests exist (or add them)
↓
2. Make small change
↓
3. Run tests
↓
4. Commit if green
↓
5. Repeat
Behavior is more than the type signature. A change that leaves names and types untouched can still alter what a return value promises, when effects become observable, or how failures are reported. If callers would need to reason differently about the function after the change, it is a behavior change rather than a refactor, even when the compiler is satisfied.
The semantic contract includes at least:
If any of these shift, update the contract explicitly (rename, change return type, document the new guarantee, migrate callers) rather than hiding the change inside a refactor.
| Technique | When to Use | Before → After |
|-----------|-------------|----------------|
| Extract Method | Long method, repeated code | Inline code → Named method |
| Extract Class | Class has multiple responsibilities | One class → Two classes |
| Move Method | Method uses another class more | A.method() → B.method() |
| Rename | Name doesn't reveal intent | d → elapsedDays |
| Technique | When to Use | Before → After |
|-----------|-------------|----------------|
| Replace Conditional with Polymorphism | Type-based switching | if/switch → Subclasses |
| Replace Magic Number | Unexplained literals | 86400 → SECONDS_PER_DAY |
| Remove Dead Code | Unused code | Code → Nothing |
| Simplify Conditional | Complex boolean logic | Nested ifs → Guard clauses |
| Technique | When to Use | Before → After |
|-----------|-------------|----------------|
| Extract Interface | Need to mock or swap | Concrete → Interface + Concrete |
| Inject Dependency | Hard-coded dependency | new Dep() → Constructor param |
| Replace Inheritance with Delegation | Inheritance misused | extends → has-a |
| Criterion | Pass | Fail | |-----------|------|------| | Test coverage | Tests exist and pass | No tests or failing tests | | Behavior understanding | Current behavior understood | Unclear behavior | | Clear goal | Refactoring goal defined | No clear objective | | Team awareness | Team knows the scope | Undisclosed changes |
| Criterion | Pass | Fail | |-----------|------|------| | Single focus | One refactoring at a time | Multiple simultaneous changes | | Test validation | Tests run after each change | No test verification | | Incremental commits | Commit after each step | Large uncommitted changes | | Behavior preservation | Return-value meaning, completion timing, side-effect ordering, and error model all unchanged | Semantic contract shifted even though the signature stayed the same |
| Criterion | Pass | Fail | |-----------|------|------| | Tests passing | All tests still pass | Tests failing | | Code clarity | Code is cleaner/clearer | Same or worse clarity | | No new features | No functionality added | Features added | | Review completed | Changes reviewed | No review |
| Smell | Signal | Refactoring | |-------|--------|-------------| | Long Method | Hard to describe in one sentence | Extract Method | | Large Class | Multiple unrelated responsibilities | Extract Class | | Long Parameter List | Parameters that always travel together | Introduce Parameter Object | | Duplicated Code | Same logic in multiple places | Extract Method/Class | | Feature Envy | Method uses another class's data more than its own | Move Method | | Data Clumps | Same data groups appear together | Extract Class | | Explanatory Comment | Comment restates the code in prose | Extract Method with an intent-revealing name |
development
Plan how a system is structured — which layers, modules, or contexts exist and how code is laid out — driven by the forces at play, not by a fixed pattern. The default is to add no extra structure; structure is introduced only when complexity calls for it, and the chosen shape (Clean Architecture, DDD, DCI, or another) is judged in the moment. Use this skill when following a recorded structure in docs/architecture.md, or when deciding the structural shape for a non-trivial feature, a new module, or a restructuring — especially after a design-forces memo recommends a layered or partitioned structure.
development
Surface design forces and lay out the option space — including framework defaults, scaffolds, and deferring — before committing to Clean Architecture, DDD, or any heavier pattern. Produces a Design Analysis Memo at the start of /write, /refactor, or non-trivial /fix. Make sure to use this skill whenever the user starts a non-trivial feature, restructures code, or asks "do we really need Clean Architecture / DDD / pattern X here".
development
Write tests using TDD (Red-Green-Refactor) and AAA pattern. Use for every new feature, behavior change, or bug fix. Covers unit, integration, and E2E test selection. Make sure to use this skill whenever the user asks to add tests, fix a bug (tests should come first), implement a feature with test coverage, or asks about what kind of tests to write — even for small one-line changes or trivial-looking fixes.
development
Prevent security vulnerabilities through threat modeling, trust boundary analysis, and defense in depth. Use when writing code that crosses trust boundaries, handles authentication or authorization, processes external input, manages secrets, or stores sensitive data. Make sure to use this skill whenever the user works on login flows, processes data from external sources, builds interfaces between systems, manages credentials, or writes code that moves data across trust zones — even for seemingly simple changes like accepting a new parameter or calling an external service.