refactor
Skill này làm gì
Nó hướng dẫn agent của bạn cải thiện cấu trúc mã có sẵn bằng các bước nhỏ, có kiểm thử, mà không thay đổi hành vi. Nó dành cho lập trình viên muốn dọn dẹp mã cũ hoặc giảm nợ kỹ thuật một cách an toàn.
Khi nào nên dùng
- ✓Khi bạn muốn dọn dẹp mã lộn xộn hoặc mã cũ mà không thay đổi hành vi của nó.
- ✓Khi bạn muốn loại bỏ các mùi mã, chẳng hạn hàm bị trùng lặp hoặc quá dài.
- ✓Khi bạn muốn một kế hoạch tái cấu trúc được duyệt trước khi thay đổi bất kỳ thứ gì.
Liên kết skill
https://github.com/luongnv89/claude-howto/blob/main/03-skills/refactor/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Bạn có thể giúp tôi tái cấu trúc module tính phí cũ này một cách an toàn, bắt đầu bằng một kế hoạch để tôi duyệt không?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Agent nghiên cứu mã, kiểm tra độ phủ kiểm thử, xác định các mùi mã và viết kế hoạch. Sau đó nó thực hiện các thay đổi nhỏ, từng bước và xin bạn duyệt ở mỗi giai đoạn.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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
- Behavior Preservation: External behavior must remain unchanged
- Small Steps: Make tiny, testable changes
- Test-Driven: Tests are the safety net
- Continuous: Refactoring is ongoing, not a one-time event
- 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:
- Scope: Which files/modules/functions need refactoring?
- Goals: What problems are you trying to solve? (readability, performance, maintainability)
- Constraints: Are there any areas that should NOT be changed?
- Timeline pressure: Is this blocking other work?
- 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
Check for existing tests
# Look for test files find . -name "*test*" -o -name "*spec*" | head -20Run existing tests
# JavaScript/TypeScript npm test # Python pytest -v # Java mvn testCheck coverage (if available)
# 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:
- Write tests first (recommended)
- Add tests incrementally during refactoring
- 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:
- Write failing test (red)
- Make it pass (green)
- 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
| Smell | Signs | Impact |
|---|---|---|
| Long Method | Methods > 30-50 lines | Hard to understand, test, maintain |
| Duplicated Code | Same logic in multiple places | Bug fixes needed in multiple places |
| Large Class | Class with too many responsibilities | Violates Single Responsibility |
| Feature Envy | Method uses another class's data more | Poor encapsulation |
| Primitive Obsession | Overuse of primitives instead of objects | Missing domain concepts |
| Long Parameter List | Methods with 4+ parameters | Hard to call correctly |
| Data Clumps | Same data items appearing together | Missing abstraction |
| Switch Statements | Complex switch/if-else chains | Hard to extend |
| Speculative Generality | Code "just in case" | Unnecessary complexity |
| Dead Code | Unused code | Confusion, maintenance burden |
Analysis Steps
- **Automated
Câu hỏi thường gặp
Nó có thay đổi hành vi không?
Skill nói hành vi phải giữ nguyên, nên tái cấu trúc chỉ thay đổi cấu trúc bên trong.
Có cần kiểm thử không?
Skill đánh giá độ phủ kiểm thử trước và dùng kiểm thử làm lưới an toàn cho mỗi thay đổi.
Nó có tự thay đổi mã mà không hỏi không?
Không. Skill nói cần sự duyệt của người dùng ở mỗi giai đoạn.
Chi tiết
Mô tả gốc của tác giả
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.
repo: luongnv89/claude-howto
Độ phổ biến
- Sao GitHub
- 46k
- Vị trí xếp hạng
- #6324
- Giấy phép
- MIT
- Cập nhật
- 2/11/2026
Bình luận
- …
Thêm skill từ kho mã này
lesson-quiz
46kKiểm tra bạn về một bài học hướng dẫn Claude Code với 10 câu hỏi, chấm điểm câu trả lời và chỉ ra các chủ đề nên ôn lại.
self-assessment
46kĐưa bạn qua một bài trắc nghiệm về 10 mảng tính năng của Claude Code, rồi cho điểm theo chủ đề và một lộ trình học tiếp theo.
blog-draft
46kHướng dẫn agent của bạn viết nháp bài blog từ ý tưởng và nguồn tài liệu của bạn, từng bước, với các phiên bản được theo dõi khi làm.
claude-md
46kGiúp agent của bạn tạo hoặc cải thiện file CLAUDE.md, tài liệu dự án giới thiệu codebase cho một agent AI.
Skill tương tự
diffs
394kGiúp agent của bạn tạo một diff có thể chia sẻ về các chỉnh sửa, dưới dạng liên kết xem hoặc file ảnh, thay vì một bản tóm tắt viết tay.
coding-agent
394kGiúp agent của bạn giao một việc lập trình lớn cho Codex, Claude Code hoặc OpenCode chạy nền, rồi theo dõi tiến độ của nó.
gh-issues
394kGiúp agent của bạn tìm issue trên GitHub, chọn những issue cần xử lý và mở pull request với bản sửa, kể cả theo dõi nhận xét đánh giá.