π‘ What is DRY?
DRY stand for Don't Repeat Yourself, a basic principle of software development aimed at reducing repetition of information.
π€ 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".
β 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.
π‘ 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!
- Interface Segregation Principle (ISP) Explained in 100 Seconds
- Liskov Substitution Principle (LSP) Explained in 100 Seconds
- KISS Design Principle Explained in 100 Seconds
- Golang - How a Chef and Waiter Teach the Single Responsibility Principle
- "Tell, Don't Ask" Principle Explained in 100 Seconds
- Golang Dependency Injection - Just in 5 Minutes!
Follow me to stay updated with my future posts:
Top comments (0)