mcpField
D
dont-repeat-yourself

dont-repeat-yourself

github.com/yanko-belovDevelopment1395 installsUpdated 40w ago
Repository

About dont-repeat-yourself

dont-repeat-yourself is a coding skill that guides developers to keep each piece of logic or knowledge in one place. It focuses on spotting copy-paste patterns and extracting shared code with parameters for differences.

Key features

  1. 01Detect duplicated code and logic
  2. 02Extract shared functions and validators
  3. 03Parameterize differences in similar code
  4. 04Centralize repeated constants and rules
  5. 05Use duplication signals to guide refactoring

Use cases

  1. 01Avoid copying existing code into a new feature
  2. 02Consolidate validation repeated across forms
  3. 03Fix calculations maintained in multiple files
  4. 04Refactor similar functions with small differences

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/yanko-belov/code-craft --skill dont-repeat-yourself

After installing, type/dont-repeat-yourselfin Claude Code to confirm it loaded.

Details

From the project README

DRY (Don't Repeat Yourself)

Overview

Every piece of knowledge must have a single, unambiguous representation in the system.

If you find yourself writing the same logic twice, extract it. Duplication is a bug waiting to happen.

When to Use

  • Writing code similar to existing code
  • Copy-pasting and modifying
  • Making the same change in multiple files
  • Validation logic repeated across forms
  • Same calculations in different places

The Iron Rule

NEVER duplicate logic. Extract and reuse.

No exceptions:

  • Not for "it's faster to copy"
  • Not for "they're slightly different"
  • Not for "I'll refactor later"
  • Not for "it's just a few lines"

Detection: The Copy-Paste Smell

If you're about to copy code and modify it, STOP:

// ❌ VIOLATION: Duplicated validation
function validateRegistrationEmail(email: string): boolean {
  return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}

function validateProfileEmail(email: string): boolean {
  return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); // Same logic!
}

// ✅ CORRECT: Single source of truth
function validateEmail(email: string): boolean {
  return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}

// Reuse everywhere
const isValidRegistration = validateEmail(regEmail);
const isValidProfile = validateEmail(profileEmail);

Detection: The "Change in Multiple Places" Test

If fixing a bug requires changing multiple locations, you have duplication:

// ❌ Bug in tax calculation requires changes in 3 files
// cart.ts:      const tax = price * 0.08;
// checkout.ts:  const tax = price * 0.08;
// invoice.ts:   const tax = price * 0.08;

// ✅ Single source of truth
// tax.ts:       export const calculateTax = (price: number) => price * TAX_RATE;

The Correct Pattern: Extract and Parameterize

When code is "almost the same", extract the common part and parameterize the differences:

// ❌ VIOLATION: Similar functions with minor differences
function formatUserName(user: User): string {
  return `${user.firstName} ${user.lastName}`;
}

function formatAdminName(admin: Admin): string {
  return `${admin.firstName} ${admin.lastName} (Admin)`;
}

// ✅ CORRECT: Parameterized
function formatName(person: { firstName: string; lastName: string }, suffix?: string): string {
  const name = `${person.firstName} ${person.lastName}`;
  return suffix ? `${name} (${suffix})` : name;
}

Pressure Resistance Protocol

1. "It's Faster to Copy"

Pressure: "I'll just copy this and modify it"

Response: Copying creates two places to maintain. Bugs will diverge.

Action: Extract shared logic first, then use it in both places.

2. "They're Slightly Different"

Pressure: "The functions are almost the same but not quite"

Response: "Almost the same" = extract common part, parameterize differences.

Action: Identify what's shared, extract it, make differences parameters.

3. "It's Just a Few Lines"

Pressure: "It's only 3 lines, not worth extracting"

Response: 3 lines duplicated 5 times = 15 lines to maintain. Bugs multiply.

Action: Extract even small duplications. Name them well.

4. "I'll Refactor Later"

Pressure: "Ship now, DRY it up later"

Response: You won't. Duplication spreads. DRY now takes 2 minutes.

Action: Extract before committing the duplication.

Red Flags - STOP and Reconsider

If you notice ANY of these, you're about to violate DRY:

  • Ctrl+C / Ctrl+V in your workflow
  • "This is similar to that other function"
  • Same regex/validation in multiple places
  • Identical error handling patterns repeated
  • Same data transformation logic duplicated
  • Constants defined in multiple files

All of these mean: Extract to a shared location.

Types of Duplication

TypeExampleSolution
CodeSame function body twiceExtract function
LogicSame algorithm, different namesExtract and parameterize
DataSame constant in multiple filesCentralize constants
StructureSame class shape repeatedExtract interface/base
KnowledgeBusiness rule in multiple placesSingle source of truth

Quick Reference

SymptomAction
Copy-pasting codeExtract shared function
Same validation twiceCreate validator module
Same constant in filesCreate constants file
Similar functionsExtract + parameterize
Bug fix needs multiple changesConsolidate to one place

Common Rationalizations (All Invalid)

ExcuseReality
"It's faster to copy"It's slower to maintain duplicates.
"They're slightly different"Extract common, parameterize differences.
"Just a few lines"Few lines × many places = many bugs.
"I'll refactor later"You won't. Extract now.
"Different contexts"Same logic = same code, regardless of context.
"More readable as copies"Named, extracted functions are more readable.

The Bottom Line

One piece of knowledge. One place in code.

When writing similar code: stop, find the existing code, extract if needed, reuse. Duplication is the root of maintenance nightmares.

Comments

Markdown supported