Refactor logo

Refactor

CommunityPopular
luongnv89
refactor

Systematic code refactoring based on Martin Fowler's methodology. Use when users ask to refactor code, improve code structure, reduce technical debt, clean up legacy code, eliminate code smells, or improve code maintainability. This skill guides through a phased approach with research, planning, and safe incremental implementation.

Overview

Publisherluongnv89
Repositoryclaude-howto
Skill namerefactor
Stars
41.5K
Forks
5.1K
Bundled files
5
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 5 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by luongnv89 on GitHub. Read the source before you install it.

Installation

Install the Refactor AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/luongnv89/claude-howto.git /tmp/claude-howto
mkdir -p .claude/skills
cp -r /tmp/claude-howto/03-skills/refactor .claude/skills/refactor
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Refactor in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Refactor on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Refactor is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Code Refactoring Skill

A systematic approach to refactoring code based on Martin Fowler's Refactoring: Improving the Design of Existing Code (2nd Edition). This skill emphasizes safe, incremental changes backed by tests.

"Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure." — Martin Fowler

Core Principles

  1. Behavior Preservation: External behavior must remain unchanged
  2. Small Steps: Make tiny, testable changes
  3. Test-Driven: Tests are the safety net
  4. Continuous: Refactoring is ongoing, not a one-time event
  5. Collaborative: User approval required at each phase

Workflow Overview

Phase 1: Research & Analysis
Phase 2: Test Coverage Assessment
Phase 3: Code Smell Identification
Phase 4: Refactoring Plan Creation
Phase 5: Incremental Implementation
Phase 6: Review & Iteration

Phase 1: Research & Analysis

Objectives

  • Understand the codebase structure and purpose
  • Identify the scope of refactoring
  • Gather context about business requirements

Questions to Ask User

Before starting, clarify:

  1. Scope: Which files/modules/functions need refactoring?
  2. Goals: What problems are you trying to solve? (readability, performance, maintainability)
  3. Constraints: Are there any areas that should NOT be changed?
  4. Timeline pressure: Is this blocking other work?
  5. Test status: Do tests exist? Are they passing?

Actions

  • Read and understand the target code
  • Identify dependencies and integrations
  • Document current architecture
  • Note any existing technical debt markers (TODOs, FIXMEs)

Output

Present findings to user:

  • Code structure summary
  • Identified problem areas
  • Initial recommendations
  • Request approval to proceed

Phase 2: Test Coverage Assessment

Why Tests Matter

"Refactoring without tests is like driving without a seatbelt." — Martin Fowler

Tests are the key enabler of safe refactoring. Without them, you risk introducing bugs.

Assessment Steps

  1. Check for existing tests

    bash
    # Look for test files
    find . -name "*test*" -o -name "*spec*" | head -20
  2. Run existing tests

    bash
    # JavaScript/TypeScript
    npm test
    
    # Python
    pytest -v
    
    # Java
    mvn test
  3. Check coverage (if available)

    bash
    # JavaScript
    npm run test:coverage
    
    # Python
    pytest --cov=.

Decision Point: Ask User

If tests exist and pass:

  • Proceed to Phase 3

If tests are missing or incomplete: Present options:

  1. Write tests first (recommended)
  2. Add tests incrementally during refactoring
  3. Proceed without tests (risky - requires user acknowledgment)

If tests are failing:

  • STOP. Fix failing tests before refactoring
  • Ask user: Should we fix tests first?

Test Writing Guidelines (if needed)

For each function being refactored, ensure tests cover:

  • Happy path (normal operation)
  • Edge cases (empty inputs, null, boundaries)
  • Error scenarios (invalid inputs, exceptions)

Use the "red-green-refactor" cycle:

  1. Write failing test (red)
  2. Make it pass (green)
  3. Refactor

Phase 3: Code Smell Identification

What Are Code Smells?

Symptoms of deeper problems in code. They're not bugs, but indicators that the code could be improved.

Common Code Smells to Check

See references/code-smells.md for the complete catalog.

Quick Reference
SmellSignsImpact
Long MethodMethods > 30-50 linesHard to understand, test, maintain
Duplicated CodeSame logic in multiple placesBug fixes needed in multiple places
Large ClassClass with too many responsibilitiesViolates Single Responsibility
Feature EnvyMethod uses another class's data morePoor encapsulation
Primitive ObsessionOveruse of primitives instead of objectsMissing domain concepts
Long Parameter ListMethods with 4+ parametersHard to call correctly
Data ClumpsSame data items appearing togetherMissing abstraction
Switch StatementsComplex switch/if-else chainsHard to extend
Speculative GeneralityCode "just in case"Unnecessary complexity
Dead CodeUnused codeConfusion, maintenance burden

Analysis Steps

  1. Automated Analysis (if scripts available)

    bash
    python scripts/detect-smells.py <file>
  2. Manual Review

    • Walk through code systematically
    • Note each smell with location and severity
    • Categorize by impact (Critical/High/Medium/Low)
  3. Prioritization Focus on smells that:

    • Block current development
    • Cause bugs or confusion
    • Affect most-changed code paths

Output: Smell Report

Present to user:

  • List of identified smells with locations
  • Severity assessment for each
  • Recommended priority order
  • Request approval on priorities

Phase 4: Refactoring Plan Creation

Selecting Refactorings

For each smell, select an appropriate refactoring from the catalog.

See references/refactoring-catalog.md for the complete list.

Smell-to-Refactoring Mapping
Code SmellRecommended Refactoring(s)
Long MethodExtract Method, Replace Temp with Query
Duplicated CodeExtract Method, Pull Up Method, Form Template Method
Large ClassExtract Class, Extract Subclass
Feature EnvyMove Method, Move Field
Primitive ObsessionReplace Primitive with Object, Replace Type Code with Class
Long Parameter ListIntroduce Parameter Object, Preserve Whole Object
Data ClumpsExtract Class, Introduce Parameter Object
Switch StatementsReplace Conditional with Polymorphism
Speculative GeneralityCollapse Hierarchy, Inline Class, Remove Dead Code
Dead CodeRemove Dead Code

Plan Structure

Use the template at templates/refactoring-plan.md.

For each refactoring:

  1. Target: What code will change
  2. Smell: What problem it addresses
  3. Refactoring: Which technique to apply
  4. Steps: Detailed micro-steps
  5. Risks: What could go wrong
  6. Rollback: How to undo if needed

Phased Approach

CRITICAL: Introduce refactoring gradually in phases.

Phase A: Quick Wins (Low risk, high value)

  • Rename variables for clarity
  • Extract obvious duplicate code
  • Remove dead code

Phase B: Structural Improvements (Medium risk)

  • Extract methods from long functions
  • Introduce parameter objects
  • Move methods to appropriate classes

Phase C: Architectural Changes (Higher risk)

  • Replace conditionals with polymorphism
  • Extract classes
  • Introduce design patterns

Decision Point: Present Plan to User

Before implementation:

  • Show complete refactoring plan
  • Explain each phase and its risks
  • Get explicit approval for each phase
  • Ask: "Should I proceed with Phase A?"

Phase 5: Incremental Implementation

The Golden Rule

"Change → Test → Green? → Commit → Next step"

Implementation Rhythm

For each refactoring step:

  1. Pre-check

    • Tests are passing (green)
    • Code compiles
  2. Make ONE small change

    • Follow the mechanics from the catalog
    • Keep changes minimal
  3. Verify

    • Run tests immediately
    • Check for compilation errors
  4. If tests pass (green)

    • Commit with descriptive message
    • Move to next step
  5. If tests fail (red)

    • STOP immediately
    • Undo the change
    • Analyze what went wrong
    • Ask user if unclear

Commit Strategy

Each commit should be:

  • Atomic: One logical change
  • Reversible: Easy to revert
  • Descriptive: Clear commit message

Example commit messages:

refactor: Extract calculateTotal() from processOrder()
refactor: Rename 'x' to 'customerCount' for clarity
refactor: Remove unused validateOldFormat() method

Progress Reporting

After each sub-phase, report to user:

  • Changes made
  • Tests still passing?
  • Any issues encountered
  • Ask: "Continue with next batch?"

Phase 6: Review & Iteration

Post-Refactoring Checklist

  • All tests passing
  • No new warnings/errors
  • Code compiles successfully
  • Behavior unchanged (manual verification)
  • Documentation updated if needed
  • Commit history is clean

Metrics Comparison

Run complexity analysis before and after:

bash
python scripts/analyze-complexity.py <file>

Present improvements:

  • Lines of code change
  • Cyclomatic complexity change
  • Maintainability index change

User Review

Present final results:

  • Summary of all changes
  • Before/after code comparison
  • Metrics improvements
  • Remaining technical debt
  • Ask: "Are you satisfied with these changes?"

Next Steps

Discuss with user:

  • Additional smells to address?
  • Schedule follow-up refactoring?
  • Apply similar changes elsewhere?

Important Guidelines

When to STOP and Ask

Always pause and consult user when:

  • Unsure about business logic
  • Change might affect external APIs
  • Test coverage is inadequate
  • Significant architectural decision needed
  • Risk level increases
  • You encounter unexpected complexity

Safety Rules

  1. Never refactor without tests (unless user explicitly acknowledges risk)
  2. Never make big changes - break into tiny steps
  3. Never skip the test run after each change
  4. Never continue if tests fail - fix or rollback first
  5. Never assume - when in doubt, ask

What NOT to Do

  • Don't combine refactoring with feature additions
  • Don't refactor during production emergencies
  • Don't refactor code you don't understand
  • Don't over-engineer - keep it simple
  • Don't refactor everything at once

Quick Start Example

Scenario: Long Method with Duplication

Before:

javascript
function processOrder(order) {
  // 150 lines of code with:
  // - Duplicated validation logic
  // - Inline calculations
  // - Mixed responsibilities
}

Refactoring Steps:

  1. Ensure tests exist for processOrder()
  2. Extract validation into validateOrder()
  3. Test - should pass
  4. Extract calculation into calculateOrderTotal()
  5. Test - should pass
  6. Extract notification into notifyCustomer()
  7. Test - should pass
  8. Review - processOrder() now orchestrates 3 clear functions

After:

javascript
function processOrder(order) {
  validateOrder(order);
  const total = calculateOrderTotal(order);
  notifyCustomer(order, total);
  return { order, total };
}

References

Scripts

  • scripts/analyze-complexity.py - Analyze code complexity metrics
  • scripts/detect-smells.py - Automated smell detection

Version History

  • v1.0.0 (2025-01-15): Initial release with Fowler methodology, phased approach, user consultation points

Last Updated: August 4, 2026 Claude Code Version: 2.1.220 Sources:

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Refactor AI skill do?

Systematic code refactoring based on Martin Fowler's methodology. Use when users ask to refactor code, improve code structure, reduce technical debt, clean up legacy code, eliminate code smells, or improve code maintainability. This skill guides through a phased approach with research, planning, and safe incremental implementation.

Why use Refactor on TypingMind?

Because you install it once and use it with any model. Refactor is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Refactor in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/luongnv89/claude-howto/tree/main/03-skills/refactor. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Refactor?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Refactor?

As many as you like. As long as a model supports skills, you can use Refactor with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Refactor AI skill free?

Yes. It is published on GitHub by luongnv89 under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇