refactor
ما الذي تفعله
يرشد وكيلك إلى تحسين بنية الكود القائم في خطوات صغيرة ومختبرة دون تغيير ما يفعله. وهو مناسب للمطورين الذين يريدون تنظيف كود قديم أو تقليل الديون التقنية بأمان.
متى تستخدمها
- ✓عندما تريد تنظيف كود فوضوي أو قديم دون تغيير سلوكه.
- ✓عندما تريد إزالة روائح الكود، مثل الدوال المكررة أو الطويلة جدًا.
- ✓عندما تريد الموافقة على خطة إعادة الهيكلة قبل إجراء أي تغييرات.
رابط المهارة
https://github.com/luongnv89/claude-howto/blob/main/03-skills/refactor/SKILL.mdانسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.
طريقة الاستخدام
أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.
رسالة جاهزة للصق
لـ Claude Code أو Codex أو Cursor: شغّله داخل مجلد مشروعك وستُثبَّت المهارة.
نزّل ملف SKILL.md وضعه في مجلد مهارات وكيلك.
مثال على ما تطلبه من الذكاء الاصطناعي
“هل يمكنك مساعدتي في إعادة هيكلة وحدة الفوترة القديمة هذه بأمان، بدءًا بخطة أستطيع الموافقة عليها؟”
5 مهارات جديدة كل أسبوع. قراءة في دقيقة واحدة.
كيف تعمل
يدرس الوكيل الكود ويفحص تغطية الاختبارات ويحدد روائح الكود، ثم يكتب خطة. بعد ذلك يُجري تغييرات صغيرة واحدًا تلو الآخر، ويطلب موافقتك في كل مرحلة.
المحتوى التقني (ملف SKILL.md الأصلي)
النص الأصلي من المؤلف، مباشرة من 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
الأسئلة الشائعة
هل تغيّر السلوك؟
تقول المهارة إن السلوك يجب أن يبقى كما هو، فتغيّر إعادة الهيكلة البنية الداخلية فقط.
هل تحتاج إلى اختبارات؟
تقيّم المهارة تغطية الاختبارات أولًا، وتستخدم الاختبارات شبكة أمان لكل تغيير.
هل سيغيّر الكود دون أن يسأل؟
لا. تقول المهارة إن موافقة المستخدم مطلوبة في كل مرحلة.
التفاصيل
الوصف الأصلي من المؤلف
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
الشعبية
- نجوم GitHub
- 46k
- الترتيب
- #6324
- الترخيص
- MIT
- آخر تحديث
- 2/11/2026
التعليقات
- …
المزيد من المهارات من هذا المستودع
lesson-quiz
46kيختبرك في درس واحد من دروس Claude Code بعشرة أسئلة، ويحسب درجتك، ويشير إلى الموضوعات التي تحتاج إلى مراجعة.
self-assessment
46kيعطيك اختبارًا في 10 مجالات من ميزات Claude Code، ثم درجة حسب الموضوع ومسارًا تعليميًا لما يجب دراسته بعد ذلك.
blog-draft
46kيرشد وكيلك خطوة بخطوة في كتابة مسودة مدونة من أفكارك ومصادرك، مع تتبع النسخ أثناء التقدم.
claude-md
46kيساعد وكيلك على إنشاء ملف CLAUDE.md أو تحسينه، وهو دليل المشروع الذي يعرّف الوكيل الذكي بقاعدة الكود.
مهارات مشابهة
diffs
394kيتيح لوكيلك إنتاج فرق تعديلات حقيقي يمكن مشاركته، كرابط عرض أو ملف صورة، بدلاً من ملخص مكتوب يدوياً.
coding-agent
394kيساعد وكيلك على تسليم مهمة برمجة كبيرة إلى Codex أو Claude Code أو OpenCode كعامل في الخلفية، ثم متابعة تقدمها.
gh-issues
394kيساعد وكيلك على إيجاد مشكلات GitHub واختيار ما ستعمل عليه، وفتح طلبات سحب بالإصلاحات، ومتابعة تعليقات المراجعة.