agentleFS
kayamankayaman/skills/dry-principle/SKILL.md

dry-principle

Skill3 starsChanged 8 months agoCC0-1.0View on GitHub

Enforces the DRY (Don't Repeat Yourself) principle correctly, distinguishing true knowledge duplication from incidental code similarity. Use when evaluating whether to extract shared code, deciding if duplication is acceptable, applying the Rule of Three, avoiding premature abstraction, or reviewing code for the wrong abstraction anti-pattern.

The file

Show its rules and text

We split this skill into the rules it gives an agent

21 rules. About 0 are common, 20 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈20

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    If yes → true DRY violation — extract a single authoritative representation

    line 30
  2. Distinctive:

    If no → incidental similarity — leave them separate.

    line 31
  3. Distinctive:

    MUST eliminate knowledge/logic duplication — this is the most dangerous form

    line 45
  4. Distinctive:

    SHOULD eliminate code duplication only after confirming it represents the same knowledge

    line 46
  5. Distinctive:

    SHOULD use shared schemas or code generation to reduce representation duplication across boundaries

    line 47
  6. Distinctive:

    Every extraction creates a dependency.

    line 69
  7. Distinctive:

    MUST weigh the coupling cost of extraction against the maintenance cost of duplication

    line 71
  8. Distinctive:

    SHOULD prefer duplication over coupling across team or service boundaries (Winters et al., *Software Engineering at Google*)

    line 73
  9. Distinctive:

    MUST NOT extract a shared abstraction on the first occurrence

    line 85
  10. Distinctive:

    SHOULD tolerate duplication on the second occurrence, noting it for future refactoring

    line 86
  11. Distinctive:

    SHOULD extract on the third occurrence, when the true pattern is clear

    line 87
  12. Distinctive:

    MUST verify that all three instances represent the same knowledge before extracting

    line 88
  13. Distinctive:

    SHOULD optimize for change first — future requirements are unknown

    line 104
  14. Distinctive:

    SHOULD abstract when you are confident about the use cases, not before

    line 105
  15. Distinctive:

    SHOULD NOT set a fixed numeric threshold — the right moment depends on how well you understand the pattern

    line 106
  16. Distinctive:

    When you encounter similar code, follow this sequence:

    line 112
  17. Distinctive:

    How many occurrences?: If fewer than three, tolerate the duplication.

    line 116
  18. Distinctive:

    What's the coupling cost?: If extraction creates a dependency across service, team, or deployment boundaries, prefer duplication.

    line 118
  19. Very specific:

    Is the abstraction clear?: If you can name the extracted concept and it represents a single, stable piece of knowledge, extract it.

    line 120
  20. Distinctive:

    Can you undo it?: If the extraction turns out to be wrong, you must be willing to re-inline.

    line 122
  21. Distinctive:

    When evaluating duplication, verify:

    line 141

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

DRY Principle

Reference: The Pragmatic Programmer (Hunt & Thomas), Tidy First? (Beck), Software Engineering at Google (Winters et al.)

When to Apply

  • Evaluating whether two similar code blocks should be extracted into a shared abstraction
  • Reviewing code for duplicated business rules, logic, or knowledge
  • Deciding whether an existing abstraction is the right one or should be reverted to duplication
  • Refactoring a codebase with repeated logic that has begun to diverge
  • Assessing whether a shared library or utility is worth the coupling cost

What DRY Actually Means

The formal definition from Hunt & Thomas (1999): "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."

DRY is about knowledge duplication, not code duplication. Two identical-looking code blocks that represent different business concepts are NOT a DRY violation. Two different-looking code blocks that encode the same business rule ARE a violation.

The Acid Test

"If this business rule changes, would ALL of these locations need to change in exactly the same way?"

  • If yes → true DRY violation — extract a single authoritative representation
  • If no → incidental similarity — leave them separate; they will likely diverge

Five Types of Duplication

TypeDescriptionExampleFix
CodeCopy-pasted blocksSame validation logic in two controllersExtract shared function
Knowledge / LogicSame business rule in multiple placesTax calculation in order service AND invoice serviceSingle authoritative implementation
DocumentationComments restating what code doesx = x + 1 // increment xDelete the comment; the code is clear
DataDerived values stored independentlyStoring total separately from itemsDerive on read, or use a single source of truth
RepresentationSame concept across system boundariesUser defined separately in frontend and backendShared schema (Protobuf, OpenAPI, JSON Schema)
  • MUST eliminate knowledge/logic duplication — this is the most dangerous form
  • SHOULD eliminate code duplication only after confirming it represents the same knowledge
  • SHOULD use shared schemas or code generation to reduce representation duplication across boundaries

When DRY Becomes Harmful

The Wrong Abstraction (Metz)

"Duplication is far cheaper than the wrong abstraction."

The pattern that leads to the wrong abstraction:

  1. Developer sees duplication and extracts it into a shared abstraction
  2. New requirement is almost the same — add a parameter and conditional
  3. Repeat step 2 several times
  4. The abstraction becomes an incomprehensible tangle of conditionals
  5. Sunk cost fallacy prevents reverting: "We already built this; let's just add one more parameter"

The remedy: The fastest way forward is back. Re-introduce duplication, then re-abstract based on the actual patterns that emerge.

Coupling Cost

Every extraction creates a dependency. Extracting a shared utility between two services couples them — changes to the utility require coordinating both services.

  • MUST weigh the coupling cost of extraction against the maintenance cost of duplication
  • Duplication between independently deployed services is often the right choice
  • SHOULD prefer duplication over coupling across team or service boundaries (Winters et al., Software Engineering at Google)

Rule of Three

Don Roberts, popularized by Fowler: "The first time you do something, just do it. The second time, wince at the duplication but do it anyway. The third time, refactor."

With only two examples, you cannot reliably identify the true pattern. By three, the commonalities become visible and the abstraction has a solid foundation.

Rules

  • MUST NOT extract a shared abstraction on the first occurrence
  • SHOULD tolerate duplication on the second occurrence, noting it for future refactoring
  • SHOULD extract on the third occurrence, when the true pattern is clear
  • MUST verify that all three instances represent the same knowledge before extracting

AHA Programming

"Avoid Hasty Abstractions" — coined by Kent C. Dodds (2020): "I'm fine with code duplication until you feel pretty confident that you know the use cases. At that point, the commonalities will 'scream at you for abstraction.'"

AHA is the synthesis of DRY and WET ("Write Everything Twice"):

ApproachRuleRisk
DRYNever repeat yourselfPremature abstraction; wrong abstraction
WETWrite everything twice before extractingArbitrary threshold; ignores knowledge duplication
AHAAbstract when the pattern screams for itRequires judgment; no fixed threshold
  • SHOULD optimize for change first — future requirements are unknown
  • SHOULD abstract when you are confident about the use cases, not before
  • SHOULD NOT set a fixed numeric threshold — the right moment depends on how well you understand the pattern

Decision Framework

When you encounter similar code, follow this sequence:

  1. Is it the same knowledge? Apply the acid test. If different business rules happen to look similar, stop — leave them separate.
  2. How many occurrences? If fewer than three, tolerate the duplication. Note it with a comment if helpful.
  3. What's the coupling cost? If extraction creates a dependency across service, team, or deployment boundaries, prefer duplication.
  4. Is the abstraction clear? If you can name the extracted concept and it represents a single, stable piece of knowledge, extract it. If the name is vague (CommonUtils.processData) or requires parameters to handle divergent cases, the abstraction is wrong.
  5. Can you undo it? If the extraction turns out to be wrong, you must be willing to re-inline. Sunk cost is not a reason to keep a bad abstraction.

Common Pitfalls

PitfallSignalRemedy
Premature extractionShared function has boolean flags or parameters that select between behaviorsRe-inline; the two uses represent different knowledge
Utility dumping groundUtils class grows to hundreds of methodsBreak by domain concept; many "utilities" belong on domain objects
Cross-boundary couplingShared library forces coordinated deploymentsDuplicate within each boundary; sync schema separately
Sunk cost preservation"We already built this abstraction, let's keep extending it"Re-introduce duplication; re-abstract from scratch
DRY on testsExtracting test setup until tests are unreadableTests SHOULD be readable in isolation; duplication in tests is often acceptable
Documentation DRYCode and comments say the same thingDelete the comment; trust the code

Checklist

When evaluating duplication, verify:

  • The acid test confirms this is knowledge duplication, not incidental code similarity
  • Three or more occurrences exist (Rule of Three) — or the pattern is unmistakably clear
  • The extracted abstraction has a clear, specific name representing one concept
  • No boolean flags or type-switching parameters are needed to handle different use cases
  • The coupling cost of extraction does not exceed the maintenance cost of duplication
  • Cross-boundary duplication is preferred over cross-boundary coupling
  • Existing wrong abstractions have been identified and considered for re-inlining
  • Tests remain readable — test duplication is acceptable for clarity

Key References

BookAuthor(s)PublisherYear
The Pragmatic Programmer (2nd ed.)Hunt & ThomasAddison-Wesley2020
Tidy First?Kent BeckO'Reilly2023
A Philosophy of Software Design (2nd ed.)John OusterhoutStanford2021
Software Engineering at GoogleWinters, Manshreck, WrightO'Reilly2020
Refactoring (2nd ed.)Martin FowlerAddison-Wesley2018
Five Lines of CodeChristian ClausenManning2021

Online References

  • Sandi Metz, "The Wrong Abstraction" (blog post, 2016)
  • Kent C. Dodds, "AHA Programming" (blog post, 2020)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.