dry-refactoring
Eliminate Code Duplication with DRY Refactoring
Code duplication leads to maintenance nightmares and inconsistent behavior. This skill guides you through a systematic 4-step process to identify, abstract, replace, and verify refactoring changes that eliminate repetition and improve code quality.
تنزيل ZIP المهارة
رفع في Claude
اذهب إلى Settings → Capabilities → Skills → Upload skill
فعّل وابدأ الاستخدام
اختبرها
استخدام "dry-refactoring". I have these two functions that look almost identical. Help me refactor them.
النتيجة المتوقعة:
- I can see the duplication between these functions. Let me apply the DRY workflow:
- Step 1 - Identify: Both functions share the same structure for calculating discounts above a threshold. The only difference is the variable names.
- Step 2 - Abstract: Create a single function that takes the amount and threshold as parameters.
- Step 3 - Replace: Update both call sites to use the new abstraction.
- Step 4 - Verify: Let me help you write tests to confirm the behavior remains identical.
استخدام "dry-refactoring". My code has the same magic number 0.08 repeated in multiple places for tax calculations.
النتيجة المتوقعة:
- I can help you eliminate these magic numbers. Here is the approach:
- Step 1 - Identify: The value 0.08 appears in calculate_tax_1, calculate_tax_2, and calculate_total.
- Step 2 - Abstract: Create a constant TAX_RATE = 0.08 in a central location.
- Step 3 - Replace: Replace all occurrences with TAX_RATE.
- Step 4 - Verify: Run tests to confirm tax calculations remain correct.
استخدام "dry-refactoring". Help me refactor my validation logic that is repeated across three different service files.
النتيجة المتوقعة:
- I see you have similar validation patterns across services. Let me guide you through DRY refactoring:
- Step 1 - Identify: All three services perform email validation, password validation, and role checks.
- Step 2 - Abstract: Create a validation utility module with reusable functions.
- Step 3 - Replace: Import and use the utility functions in each service.
- Step 4 - Verify: Ensure all validation behavior is preserved and add comprehensive tests.
التدقيق الأمني
آمنPure documentation skill containing only guidance and code examples for DRY refactoring. No executable code, no network calls, no file access, and no system commands. All 92 static findings are false positives caused by the analyzer misinterpreting markdown documentation patterns as security issues.
عوامل الخطر
🌐 الوصول إلى الشبكة (2)
⚙️ الأوامر الخارجية (62)
درجة الجودة
ماذا يمكنك بناءه
Clean Up Copy-Paste Code
Identify and eliminate duplicate code blocks scattered across your codebase using the systematic 4-step workflow.
Standardize Refactoring Reviews
Apply consistent criteria when reviewing refactoring changes for adherence to DRY principles.
Establish Team Refactoring Standards
Define clear guidelines for your team to systematically address code duplication in legacy systems.
جرّب هذه الموجهات
Help me identify duplicate code in my function that calculates prices. Here is my code: [paste code]
I have three similar validation functions. Help me extract the common logic into a single reusable function following the DRY workflow.
My codebase has magic numbers like 0.08 for tax rates appearing in many places. Guide me through replacing them with named constants.
Guide me through a complete DRY refactoring of my order calculation module. I want to identify all duplication, abstract it properly, and verify the changes.
أفضل الممارسات
- Apply the Rule of Two: Refactor when you write similar code for the second time.
- Use progressive refactoring: Make small, testable changes rather than large rewrites.
- Always write or update tests before refactoring to ensure behavior is preserved.
تجنب
- Over-abstracting: Creating generic solutions before understanding the true pattern.
- Partial replacement: Leaving some duplicate code while cleaning others.
- Ignoring performance: Introducing unnecessary function call overhead through abstraction.