de-dupe
Scannednpx machina-cli add skill saadjs/agent-skills/de-dupe --openclawDe-dupe and DRY Refactor
Follow a safe, repeatable workflow to find duplicated code, extract shared helpers, and reduce maintenance burden while preserving behavior.
Workflow
-
Check for uncommitted changes before refactor.
- Run
git status -sband note touched files. - If the tree is dirty, prefer working incrementally and avoid mixing unrelated changes.
- Run
-
Identify duplication targets.
- Scan for obvious repeats in the touched files.
- Use
rgto find similar blocks or repeated strings.- Example:
rg -n "functionName|error message|regex literal" src/ - Example:
rg -n "(\w+\([^)]*\))" src/then narrow to common blocks.
- Example:
-
Decide if extraction is warranted.
- Extract when logic is repeated 2+ times and the abstraction is stable.
- Do not extract when it makes call sites harder to read or behavior diverges.
-
Extract shared utilities.
- Create a small function with a focused signature.
- Place in an existing shared module or create a new
utils/sharedfile. - Keep side effects explicit and minimize hidden dependencies.
-
Replace duplicates.
- Update call sites to use the new helper.
- Remove redundant code and keep names consistent.
-
Verify behavior.
- Update or add tests when behavior is non-trivial.
- Run relevant tests or linters if available.
Heuristics
- Prefer simple helpers over deep class refactors.
- Keep params explicit; avoid passing large context objects without need.
- Extract constants for repeated literals (strings, regexes, numbers).
- If duplication is across layers, consider moving logic to the lowest shared layer.
Guardrails
- Avoid introducing new dependencies for small refactors.
- Do not change public APIs unless explicitly requested.
- Preserve performance characteristics; avoid extra allocations in hot paths.
Notes for Codex
- Use
rgfor fast discovery and validate with context before editing. - Keep the diff small and focused; do not mix refactors with unrelated changes.
Source
git clone https://github.com/saadjs/agent-skills/blob/main/skills/de-dupe/SKILL.mdView on GitHub Overview
This skill guides a safe, repeatable workflow to identify duplicated logic, extract shared utilities, and reduce maintenance burden while preserving behavior. It emphasizes checking for uncommitted changes before refactors and favors incremental, non-disruptive improvements.
How This Skill Works
Follow a six-step workflow: check for uncommitted changes with git status -sb; identify duplicates with rg to locate repeated blocks; decide to extract when logic repeats 2+ times and is stable. Extract a small shared utility into a utils/shared module, replace duplicates at call sites, and remove redundant code. Finally, verify behavior by updating tests and running relevant checks.
When to Use It
- You need to de-duplicate logic across files to DRY code
- You spot repeated utilities or patterns that can be shared
- You want to extract common functions into a utilities/shared module
- You’re reducing tech debt by consolidating duplicates without changing behavior
- You’re preparing a refactor and must check for uncommitted changes first
Quick Start
- Step 1: Run git status -sb to check for a dirty tree and touched files
- Step 2: Use rg to locate duplicates and identify touch points (e.g., rg -n "functionName|error message|regex literal" src/)
- Step 3: Extract common logic into a small utils/shared function, replace call sites, and run tests/linters
Best Practices
- Prefer simple helpers over large refactors
- Keep explicit params; avoid passing large context objects unnecessarily
- Extract constants for repeated literals (strings, regexes, numbers)
- Do not change public APIs unless explicitly requested
- Preserve performance characteristics; minimize allocations in hot paths
Example Use Cases
- Extract a duplicated string formatting helper used across modules into a shared utility
- Pull out repeated error-message construction into a common function
- Create a utils function for repeated regex validations and reuse it
- Consolidate similar API response shaping into a single shared utility
- Update tests to cover the new shared function and ensure behavior stays the same