dry-refactoring
通过 DRY 重构消除代码重复
代码重复会导致维护难题和行为不一致。本技能将引导您通过系统性的四步流程——识别、抽象、替换和验证——进行重构,消除重复代码并提升代码质量。
Baixar o ZIP da skill
Upload no Claude
Vá em Configurações → Capacidades → Skills → Upload skill
Ative e comece a usar
Testar
A utilizar "dry-refactoring". I have these two functions that look almost identical. Help me refactor them.
Resultado esperado:
- 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.
A utilizar "dry-refactoring". My code has the same magic number 0.08 repeated in multiple places for tax calculations.
Resultado esperado:
- 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.
A utilizar "dry-refactoring". Help me refactor my validation logic that is repeated across three different service files.
Resultado esperado:
- 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.
Auditoria de Segurança
SeguroPure 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.
Fatores de risco
🌐 Acesso à rede (2)
⚙️ Comandos externos (62)
Pontuação de qualidade
O Que Você Pode Construir
清理复制粘贴代码
使用系统性的四步工作流识别并消除散布在代码库中的重复代码块。
标准化重构审查
在审查重构变更时应用一致的标准,确保符合 DRY 原则。
建立团队重构标准
为您的团队定义清晰的指南,系统性地解决遗留系统中的代码重复问题。
Tente Estes Prompts
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.
Melhores Práticas
- 应用二次原则:当您第二次编写类似代码时进行重构。
- 使用渐进式重构:进行小的、可测试的变更,而不是大规模重写。
- 始终在重构之前编写或更新测试以确保行为得到保留。
Evitar
- 过度抽象:在理解真正的模式之前就创建通用解决方案。
- 部分替换:清理部分重复代码却留下另一部分。
- 忽视性能:通过抽象引入不必要的函数调用开销。