- Home
- Skills
- Yyh211
- Claude Meta Skill
- Dry Refactoring
dry-refactoring_skill
- Python
189
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill yyh211/claude-meta-skill --skill dry-refactoring- SKILL.md25.1 KB
Overview
This skill guides systematic refactoring to eliminate code duplication using the DRY (Don't Repeat Yourself) principle. It presents a practical 4-step workflow—from identifying repetition to verifying the refactor—so you can safely consolidate repeated logic. The guidance is language-agnostic but includes Python-friendly examples and test-focused validation steps.
How this skill works
The skill inspects code for copy-paste patterns, repeated literals (magic numbers/strings), similar control structures, and semantic duplicates. It helps you extract unchanging logic into functions, classes, modules, or configuration constants, replace all occurrences with the new abstraction, and validate the changes with unit, integration, and performance tests. It also offers tactics for edge cases like optional parameters, callbacks, or intentionally preserved special cases.
When to use it
- You see identical or highly similar code blocks across the codebase.
- Multiple places use the same magic number, string, or configuration literal.
- You notice similar control flow (if/else, switch/case) doing the same work.
- You want to extract and centralize business logic for easier maintenance.
- You find recurring validation, calculation, or transformation logic.
Best practices
- Apply the Rule of Two: start refactoring when a pattern appears twice.
- Identify invariant vs. variant parts; parameterize the variant parts.
- Prefer small, focused abstractions (functions/modules) over mega-general utilities.
- Keep interfaces simple (aim for ≤ 4 parameters) and use descriptive names.
- Replace all occurrences consistently; remove old code to avoid mixed implementations.
- Write tests for the new abstraction and run integration/performance checks.
Example use cases
- Consolidate repeated tax or discount calculations into a single utility with constants for rates.
- Replace multiple duplicated input validation blocks with a reusable validator function.
- Extract repetitive data aggregation loops into a shared helper like calculate_items_total.
- Convert duplicated caching logic into a generic Cache<T> class used by different domains.
- Centralize API endpoints and credentials into configuration constants to remove magic strings.
FAQ
Start when you encounter the second occurrence but avoid over-abstraction; prefer specific, readable abstractions and expand only when true reuse emerges.
What if repeated pieces have small differences?
Parameterize the differences, use optional args or callbacks, or keep a specialized implementation if the business logic truly differs.
How do I ensure refactoring is safe?
Write unit tests for the abstraction, run integration tests that cover affected features, perform basic performance benchmarks, and use code review to validate design.