Skillpedia
برمجة عامة46k · #6324

refactor

ما الذي تفعله

يرشد وكيلك إلى تحسين بنية الكود القائم في خطوات صغيرة ومختبرة دون تغيير ما يفعله. وهو مناسب للمطورين الذين يريدون تنظيف كود قديم أو تقليل الديون التقنية بأمان.

متى تستخدمها

  • ✓عندما تريد تنظيف كود فوضوي أو قديم دون تغيير سلوكه.
  • ✓عندما تريد إزالة روائح الكود، مثل الدوال المكررة أو الطويلة جدًا.
  • ✓عندما تريد الموافقة على خطة إعادة الهيكلة قبل إجراء أي تغييرات.

رابط المهارة

https://github.com/luongnv89/claude-howto/blob/main/03-skills/refactor/SKILL.md

انسخه والصقه في الذكاء الاصطناعي الذي تستخدمه (Claude أو ChatGPT أو Cursor) واطلب منه اتباع التعليمات.

طريقة الاستخدام

أسهل طريقة: انسخ الرسالة والصقها في الذكاء الاصطناعي الذي تستخدمه. سيقرأ المهارة وينفّذ تعليماتها.

رسالة جاهزة للصق

استخدم هذه المهارة لمساعدتي: https://github.com/luongnv89/claude-howto/blob/main/03-skills/refactor/SKILL.md اقرأ ملف SKILL.md في هذا الرابط ونفّذ تعليماته لمهمتي.
تريد أن يجد الذكاء الاصطناعي مهارات مثل هذه بنفسه؟ثبّت خادم Skillpedia MCP المجاني: سطر واحد، ويبحث في 15,000+ مهارة قبل كل مهمة.ثبّته في دقيقة واحدة →

مثال على ما تطلبه من الذكاء الاصطناعي

“هل يمكنك مساعدتي في إعادة هيكلة وحدة الفوترة القديمة هذه بأمان، بدءًا بخطة أستطيع الموافقة عليها؟”

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

  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

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

    # JavaScript/TypeScript
    npm test
    
    # Python
    pytest -v
    
    # Java
    mvn test
    
  3. Check 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:

  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

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

  1. **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

هل هذه المهارة مفيدة؟

0

سجّل الدخول للتصويت.

التعليقات

  • …

المزيد من المهارات من هذا المستودع

مهارات مشابهة