December 3, 2022 • 3 min read
DRY (Don't Repeat Yourself) principle

The DRY (Don't Repeat Yourself) principle is a software design principle that states that every piece of knowledge in a system should have a single, unambiguous representation. The DRY principle is based on the idea that duplication of code, data, or knowledge results in increased complexity and decreased maintainability of the system.

In other words, the DRY principle encourages software developers to avoid duplicating code or information in their software systems, and instead to find a single source of truth for any given piece of information. This helps to ensure that the system remains consistent and reduces the risk of introducing bugs or errors due to duplicated information becoming out of sync.

The DRY principle is closely related to the principles of modularity and abstraction, as it encourages developers to encapsulate knowledge and behavior within modular components, and to abstract and encapsulate common functionality into reusable components. By adhering to the DRY principle, developers can create more maintainable and scalable software systems.

Example

Suppose you're building a web-based application that displays the current date and time in multiple locations throughout the application. If you were to hard-code the date and time into each location, you would quickly run into problems if you needed to update the format of the date and time, or if you needed to change the time zone.

A better approach would be to extract the code that retrieves and formats the date and time into a separate function or module, and then call that function from each location in the application where you need to display the date and time. This way, you have a single source of truth for the date and time, and you can make changes to the code in one place, rather than having to update it in multiple places throughout the application.

Here's an example in Java that demonstrates the DRY design principle:

import java.text.SimpleDateFormat;
import java.util.Date;

public class DRYExample {
    public static String getCurrentDateTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(new Date());
    }

    public static void main(String[] args) {
        // Usage in multiple locations
        String currentDateTime = getCurrentDateTime();
        System.out.println("Current date and time: " + currentDateTime);
    }
}

In this example, the code to retrieve and format the current date and time is extracted into a separate method getCurrentDateTime(). This method uses a SimpleDateFormat object to format the date and time, and returns the result as a string.

Conclusion

By following the DRY principle, you can make your code more maintainable and easier to change in the future. For example, if you need to change the format of the date and time, you can simply modify the code in the getCurrentDateTime() method, rather than having to update it in multiple places throughout the application.

We use cookies to improve your experience. By using our site, you agree to our use of cookies. Read our Privacy Policy for more information.