✳️ DRY Principle 🍁
Tags: design, productivity, patterns, principle
Published: 2022-01-30 18:12:11

# :eight_spoked_asterisk: DRY Principle

## Don’t Repeat Yourself

This is one of the principles of software development aimed at reducing repetition of software patterns, replacing it with abstractions or using appropriate data normalization to avoid redundancy.

### :diamond_shape_with_a_dot_inside: "Every piece of knowledge"

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

By the term `Every piece of knowledge  ` may be an algorithm, some specific functional logic, a pattern of similar code snippets with similar business logic which could have been repeated at a number of places across your solution. This causes maintainability of the solution difficult, while bringing consistency and synchronisation among different aspects of the application time consuming, when more and more repeatable blocks of the same aspect is scattered across your solution.

By following DRY it is expected that these repeatable modules can be abstracted and used across various other modules, so that you need it not repeat it and it could be reused efficiently. At the same time, when you have the opportunity to refine this abstraction you can refine it at one place, so that it happens to be consistent across your solution.

> An abstraction is a general idea rather than one relating to a particular object, person, or situation

### :dart: Premature Abstraction

You shouldn’t apply the DRY principle if your business logic doesn’t have any duplication yet. As a rule of thumb, trying to apply DRY to something which is only used in one place can lead to premature generalisation.

You will spend time to create abstractions which might be only used in one place, for a very long time. Requirements can change very quickly like today, everybody will tell you that you need some generalisation `for the future reuse`, and tomorrow, everybody will forget about it.

Eventually you have introduced more complexity and tight coupling in your code for no short term benefits.

### :negative_squared_cross_mark: Code duplication is not knowledge duplication

> Duplication is far cheaper than the wrong abstraction.

Code duplication is different from knowledge duplication, since code duplication, largely related to the developers and it could be more attributed to readability and clarity of the module they are working on. But functional business logic is what we called here as piece of knowledge, which should be accounted for in DRY principle.

* Knowledge duplication can be taken as DRY principle violation.
* Code duplication need not necessarily violate the DRY principle.

### :rainbow: Applications of DRY

DRY could be applied quite broadly in the various areas of software development like the below listed.

- database schemas
- test plans
- the build systems
- and also documentations

### :anchor: DRY Benefits

- every piece of code is unique
- less code is good
- easy to maintain
- reduces the chances of bugs.

### :trident: Trade-offs

While you apply DRY principle, you could have made your code easier to understand, but the scope of your changes would become wider. if there is any significant change in underlying contract, you may have to make sure every other integrations are fine with it.

In short, trying to apply DRY everywhere can also yield two undesirable results while creating these abstractions.

- Unnecessary coupling
- Unnecessary complexity

In these situations, the cost of involved in the either side for maintainability should be weighted before taking a call on going for further abstractions.

### :rotating_light: Tools not Rules

Again with any other principle, they are not to be considered as rules you should follow. But these are great tools for you to go in the right path in software engineering. It’s your job to adapt them depending on the requirement you may encounter.

:repeat: reposted from :link: [Dev Post](https://devpost.hashnode.dev/dry-principle)

Thanks for supporting! 🙏

Would be really great if you like to [:coffee: Buy Me a Coffee](https://www.buymeacoffee.com/AravindVCyber), to help boost my efforts.

<a href='https://ko-fi.com/X8X0CITDJ' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://cdn.ko-fi.com/cdn/kofi3.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>

<a href="https://www.buymeacoffee.com/AravindVCyber"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=AravindVCyber&button_colour=BD5FFF&font_colour=ffffff&font_family=Cookie&outline_colour=000000&coffee_colour=FFDD00" /></a>

:repeat: Original post at :link: [Dev Post](https://devpost.hashnode.dev/dry-principle)