DRY stands for Don’t Repeat Yourself. It’s a principle in software development that aims to reduce the repetition of code and favor abstraction and avoid redundancy.
Let’s learn the meaning of DRY.
The DRY principle is stated as “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system”, but what does DRY actually mean?
From the statement, we must first understand what is meant by “knowledge”.
Knowledge can be defined as:
- A precise piece of functionality
- An algorithm
To help you understand the DRY principle in development I will use an example.
Example of DRY Web Development
Let’s imagine we are building a front-end application to a back-end API. Within the front-end application, we have a component that calls a method that authenticates with an API to return a list of users.
The list of users is used in multiple components of the front-end application, so similar methods are created through the application on the components which require a list of users.
Rather than writing the code to authenticate with the API and call an endpoint that returns the list of users in JSON in each component, separate the code, then make it available to all components.
The method we are writing does 2 things:
- Authenticates with the API
- Makes a GET request to /users
These 2 actions can both be their own methods.
If we make the methods available in a service, we would only need to write the methods once. This would help our code follow the DRY principle.
DRY (Dont Repeat Yourself) is a core principle in software development designed to reduce code duplication and improve the maintainability of an application.
Learn about the SOLID Principle.