Don’t Repeat Yourself
The DRY principle - short for “don’t repeat yourself” - comes from The Pragmatic Programmer.
Notes
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
Following this principle is one of the best ways to prevent bugs and move faster. Every duplicated piece of knowledge is a bug waiting to happen. Many development techniques are really just ways to prevent and eliminate duplication, and many smells are just ways to detect existing duplication.
When knowledge is duplicated, changing it means making the same change in several places. Leaving duplication introduces a risk that the various duplicate implementations will slowly diverge, making them harder to merge and making it more likely that a bug remains in one or more incarnations after being fixed.
Duplication leads to frustration and paranoia. Rampant duplication is a common reason that developers reach for a Grand Rewrite.
Duplicated Knowledge vs Duplicated Text
It’s important to understand that this principle states that knowledge should not be repeated; it does not state that text should never be repeated.
For example, this sample does not violate the DRY principle, even though the word “save” is repeated several times:
However, this code contains duplicated knowledge that could be extracted:
Notes
Chú ý tới duplicated knowledge hơn là duplicated text
Application
The following smells may point towards Duplicated Code and can be avoided by following the DRY principle:
- Shotgun Surgery⭐ caused by changing the same knowledge in several places.
- Long Parameter List⭐ caused by not encapsulating related properties.
- Feature Envy⭐ caused by leaking internal knowledge of a class that can be encapsulated and reused.
Making behavior easy to reuse is essential to avoiding duplication. Developers won’t be tempted to copy and paste something that’s easy to reuse through a small, easy to understand class or method. You can use these solutions to make knowledge easier to reuse:
- Extract Class⭐ to encapsulate knowledge, allowing it to be reused.
- Extract Method⭐ to reuse behavior within a class.
- Extract Partial to remove duplication in views.
- Extract Validator to encapsulate validations.
- Replace Conditional with Null Object to encapsulate behavior related to nothingness.
- Replace Conditional With Polymorphism to make it easy to reuse behavioral branches.
- Replace Mixin With Composition to make it easy to combine components in new ways.
- Use Convention Over Configuration to infer knowledge, making it impossible to duplicate.
Applying these techniques before duplication occurs will make it less likely that duplication will occur. If you want to prevent duplication, make knowledge easier to reuse by keeping classes small and focused.