refactor
Fungsinya
Skill ini memandu agent Anda untuk memperbaiki struktur kode yang sudah ada dalam langkah-langkah kecil yang diuji, tanpa mengubah fungsinya. Skill ini ditujukan untuk developer yang ingin membersihkan kode lama atau mengurangi utang teknis secara aman.
Kapan dipakai
- βSaat Anda ingin merapikan kode yang berantakan atau lama tanpa mengubah perilakunya.
- βSaat Anda ingin menghilangkan code smell, seperti fungsi yang duplikat atau terlalu panjang.
- βSaat Anda ingin rencana refactoring disetujui sebelum ada perubahan dibuat.
Link skill
https://github.com/luongnv89/claude-howto/blob/main/03-skills/refactor/SKILL.mdSalin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.
Cara pakai
Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.
Pesan siap tempel
Untuk Claude Code, Codex, atau Cursor: jalankan di folder proyek, lalu skill langsung terpasang.
Unduh SKILL.md lalu taruh di folder skill agen Anda.
Contoh permintaan ke AI
βBisakah Anda membantu saya merefaktor modul penagihan lama ini dengan aman, dimulai dari rencana yang bisa saya setujui?β
5 skill baru setiap minggu. 1 menit baca.
Cara kerjanya
Agent Anda meneliti kode, memeriksa cakupan tes, mengidentifikasi code smell, dan menulis rencana. Lalu agent membuat perubahan kecil satu per satu dan meminta persetujuan Anda pada setiap fase.
Konten teknis (SKILL.md asli)
Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.
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
Pertanyaan umum
Apakah perilaku kode berubah?
Skill ini menyebutkan perilaku harus tetap sama, sehingga refactoring hanya mengubah struktur internal.
Apakah perlu tes?
Skill ini menilai cakupan tes terlebih dahulu dan menggunakan tes sebagai jaring pengaman untuk setiap perubahan.
Apakah agent akan mengubah kode tanpa bertanya?
Tidak. Skill ini menyebutkan persetujuan pengguna diperlukan pada setiap fase.
Detail
Deskripsi asli dari pembuat
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
Popularitas
- Bintang GitHub
- 46k
- Peringkat
- #6324
- Lisensi
- MIT
- Diperbarui
- 2/11/2026
Komentar
- β¦
Skill lain dari repositori ini
lesson-quiz
46kMemberi Anda kuis tentang satu pelajaran tutorial Claude Code dengan 10 pertanyaan, menilai jawaban Anda, dan menunjukkan topik yang perlu diulang.
self-assessment
46kMemberi Anda kuis tentang 10 area fitur Claude Code, lalu skor per topik dan jalur belajar untuk apa yang harus dipelajari berikutnya.
blog-draft
46kMemandu agent Anda menyusun draf postingan blog dari ide dan sumber Anda, langkah demi langkah, dengan versi yang dicatat selama proses.
claude-md
46kMembantu agent Anda membuat atau memperbaiki file CLAUDE.md, panduan proyek yang memperkenalkan agent AI pada basis kode Anda.
Skill serupa
diffs
394kMemungkinkan agent Anda menghasilkan diff perubahan yang nyata dan dapat dibagikan sebagai tautan viewer atau file gambar, bukan ringkasan yang ditulis manual.
coding-agent
394kMembantu agent Anda menyerahkan pekerjaan pemrograman besar ke Codex, Claude Code, atau OpenCode di latar belakang, lalu memeriksa kemajuannya.
gh-issues
394kMembantu agent Anda menemukan issue GitHub, memilih yang dikerjakan, dan membuka pull request perbaikan, termasuk tindak lanjut komentar tinjauan.