Skip to content
Low Level Design Mastery Logo
LowLevelDesign Mastery

DRY Principle

Don't Repeat Yourself - write code once, use it everywhere!

The DRY principle (Don’t Repeat Yourself) is one of the most fundamental principles in software development. The DRY principle states that every piece of knowledge must have a single, unambiguous representation within a system. Understanding the DRY principle is essential for writing maintainable, clean code.

DRY Principle helps you:

  • Reduce duplication - Write code once, use it everywhere
  • Easier maintenance - Change code in one place
  • Consistency - Same logic behaves the same everywhere
  • Less bugs - Fix bugs once, not in multiple places
  • Better readability - Less code to read and understand
Diagram

Without DRY Principle, you might:

  • Duplicate code - Same logic repeated in multiple places
  • Inconsistent behavior - Same logic implemented differently
  • Maintenance nightmare - Need to update code in many places
  • More bugs - Fix bugs in multiple places, easy to miss some
  • Harder to test - Test same logic multiple times
Diagram

Let’s see a simple example showing the problem and solution:

Diagram
Diagram

Here’s a more realistic example showing DRY in action:


Apply DRY Principle when:

Same logic appears multiple times - Extract to function/class
Business rules are repeated - Centralize in one place
Configuration values duplicated - Use constants/config
Similar code patterns - Create reusable abstractions
Data structures repeated - Define once, reuse

Diagram

Don’t over-apply DRY when:

Code is similar but different - Don’t force abstraction
Premature abstraction - Wait until you see actual duplication
Over-engineering - Simple duplication might be fine
Performance critical - Sometimes duplication is faster


  • DRY Principle = Don’t Repeat Yourself
  • Single source of truth - Every piece of knowledge in one place
  • Easier maintenance - Change once, affects all uses
  • Consistency - Same behavior everywhere
  • Balance - Don’t over-abstract, eliminate real duplication

Remember: DRY is about eliminating real duplication, not creating unnecessary abstractions. Use it wisely! 🎯

💡 Time to Practice!

Now that you understand the concepts, put them into practice with our interactive playground. Build UML diagrams, write code, and get AI-powered feedback.

Browse All Problems