DEV Community

Cover image for DRY Principle Explained in 100 Seconds
Dzung Nguyen
Dzung Nguyen

Posted on β€’ Edited on

DRY Principle Explained in 100 Seconds

πŸ’‘ What is DRY?

DRY stand for Don't Repeat Yourself, a basic principle of software development aimed at reducing repetition of information.

DRY Thumbnail

πŸ‘€ Origin

The principle has been formulated by Andy Hunt and Dave Thomas in their book The Pragmatic Programmer. It is stated as:

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

DRY Origin

⭐ Why DRY?

βœ… Write code once, use it often.
βœ… Change code in one place, see the change in all instances.
βœ… Less code is good: It saves time and effort, is easy to maintain, and also reduces the chances of bugs.

❌ DRY Violations

⚠️ Copy-Paste Code: Large code sections copied with little or no change.
⚠️ Similar Functions: Nearly identical functions with slight input/output variations.
⚠️ Hardcoded Values: Reusing constants or strings in multiple places instead of a central configuration or constants file.

DRY Violations

πŸ’‘ How to DRY?

βœ… Divide your code and logic into smaller reusable units and use that code by calling it where you want.
βœ… Put business rules, long expressions, if statements, math formulas, metadata, etc. in only one place.

πŸ“° Others

Interested? πŸ˜ƒ Check out other posts from my programming principles series!


Follow me to stay updated with my future posts:

Top comments (0)