Skip to Content

DRY - Don’t Repeat Yourself 🌵

The Principle

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

Benefits

  • Reduces duplication
  • Improves maintainability
  • Avoids inconsistencies
  • Less code to write and maintain
  • Promotes abstraction*
⚠️

Note: When creating abstractions, favor composition over inheritance - small reusable components that can be composed flexibly over monolithic inheritance trees biting you back on long-term.

How to Apply DRY

  • Refactor duplicate code into shared functions or components
  • Share and reuse data, templates, configuration instead of duplicating
  • Use variables, constants, config files over hard-coded values
  • Create abstractions* and higher level concepts that can be reused
  • Automate repetitive tasks instead of doing them manually

Examples

  • Reuse utilities or components across a project
  • Consume APIs instead of duplicating client code
  • Create libraries to share logic between applications
  • Use shared configuration like API keys, templates, boilerplate

DRY aims to reduce repetition and improve code reuse and maintenance. It promotes creating abstractions and leveraging existing capabilities over reinventing the wheel.

Last updated on