DRY
Definition
Don't Repeat Yourself (DRY) is a principle of software development aimed at reducing repetition of software patterns, replacing it with abstractions or using data normalization to avoid redundancy.
Why It Matters
The DRY principle is fundamental to writing maintainable code. When code is repeated in multiple places, making a change requires finding and updating every single instance, which is inefficient and error-prone. By abstracting the repeated code into a single, reusable function or class, you only need to change it in one place.
Contextual Example
If you find yourself copying and pasting the same 10 lines of code into three different parts of your application, you should apply the DRY principle by extracting that code into its own function and calling that function from the three different places.
Common Misunderstandings
- The opposite of DRY is sometimes called WET ("Write Everything Twice" or "We Enjoy Typing").
- Over-applying DRY can lead to overly complex abstractions. Sometimes, a small amount of duplication is more maintainable than a convoluted abstraction.