The DRY (Don't Repeat Yourself) principle is a software development principle that encourages developers to avoid unnecessary duplication of knowledge and logic in code. It promotes maintaining a single, authoritative representation of each piece of functionality or business rule, making software easier to maintain, modify, and extend.
- Avoids repeating the same business logic across multiple parts of an application.
- Allows changes to shared logic to be made in one place, making maintenance easier.
Example: In an e-commerce application, if multiple services calculate discounts using the same business rule, the logic can be moved into a reusable component rather than maintained separately in every location.
Importance of DRY Principle
Code duplication can make software difficult to maintain, especially as applications grow and requirements change.
- Shared functionality can be updated in one place without modifying several duplicate implementations.
- Using the same business rules across the application helps prevent inconsistent behavior.
- Reusable functions and components make repeated logic easier to organize and understand.
- Shared logic can be tested independently instead of repeatedly testing equivalent implementations.
- Common functionality can be extended without duplicating existing code.
How the DRY Principle Works
The DRY principle involves identifying duplicated knowledge, extracting appropriate reusable logic, and using that logic wherever it is needed.

Follow these steps to eliminate code duplication.
- Identify repeated business rules, calculations, or functionality across the codebase.
- Determine whether the repeated implementations represent the same underlying knowledge.
- Extract the common logic into a suitable function, class, or shared component.
- Replace the duplicated implementations with calls to the reusable component.
- Test the updated code to ensure the original behavior remains correct.
Example of DRY Principle
Consider an e-commerce application that calculates a 10% discount for different product prices.
1. Without DRY Principle
In this example, the discount calculation is repeated for multiple products.
#include <iostream>
using namespace std;
int main() {
double price1 = 1000;
double price2 = 2000;
// Repeated discount calculation
double finalPrice1 = price1 - (price1 * 0.10);
double finalPrice2 = price2 - (price2 * 0.10);
cout << "Product 1: " << finalPrice1 << endl;
cout << "Product 2: " << finalPrice2 << endl;
return 0;
}
public class Main {
public static void main(String[] args) {
double price1 = 1000;
double price2 = 2000;
// Repeated discount calculation
double finalPrice1 = price1 - (price1 * 0.10);
double finalPrice2 = price2 - (price2 * 0.10);
System.out.println("Product 1: " + finalPrice1);
System.out.println("Product 2: " + finalPrice2);
}
}
price1 = 1000
price2 = 2000
# Repeated discount calculation
final_price1 = price1 - (price1 * 0.10)
final_price2 = price2 - (price2 * 0.10)
print("Product 1:", final_price1)
print("Product 2:", final_price2)
let price1 = 1000;
let price2 = 2000;
// Repeated discount calculation
let finalPrice1 = price1 - (price1 * 0.10);
let finalPrice2 = price2 - (price2 * 0.10);
console.log("Product 1:", finalPrice1);
console.log("Product 2:", finalPrice2);
Output
Product 1: 900
Product 2: 1800
Problem: The same discount formula is repeated. If the discount rule changes, every duplicate calculation must be updated.
2. With DRY Principle
The repeated calculation can be extracted into a reusable function.
#include <iostream>
using namespace std;
// Centralized discount calculation
double applyDiscount(double price) {
const double discountRate = 0.10;
return price - (price * discountRate);
}
int main() {
double price1 = 1000;
double price2 = 2000;
// Reuse the same function
cout << "Product 1: " << applyDiscount(price1) << endl;
cout << "Product 2: " << applyDiscount(price2) << endl;
return 0;
}
public class Main {
// Centralized discount calculation
static double applyDiscount(double price) {
final double discountRate = 0.10;
return price - (price * discountRate);
}
public static void main(String[] args) {
double price1 = 1000;
double price2 = 2000;
// Reuse the same function
System.out.println("Product 1: " + applyDiscount(price1));
System.out.println("Product 2: " + applyDiscount(price2));
}
}
# Centralized discount calculation
def apply_discount(price):
discount_rate = 0.10
return price - (price * discount_rate)
price1 = 1000
price2 = 2000
# Reuse the same function
print("Product 1:", apply_discount(price1))
print("Product 2:", apply_discount(price2))
// Centralized discount calculation
function applyDiscount(price) {
const discountRate = 0.10;
return price - (price * discountRate);
}
const price1 = 1000;
const price2 = 2000;
// Reuse the same function
console.log("Product 1:", applyDiscount(price1));
console.log("Product 2:", applyDiscount(price2));
Output
Product 1: 900
Product 2: 1800
Explanation: The discount calculation is defined in one function and reused for multiple products. Any change to the common discount rule can be made in a single location.
Techniques to Apply the DRY Principle
Several software development techniques help reduce unnecessary duplication.
| Technique | Description | Example |
|---|---|---|
| Reusable Functions | Move repeated calculations or operations into reusable functions. | Use a common function to calculate discounts instead of repeating the same formula. |
| Classes and Interfaces | Organize shared responsibilities into classes and use interfaces for common contracts. | Different notification implementations can follow a common Notification interface. |
| Reusable Modules and Libraries | Place commonly used functionality in shared modules or libraries. | Use a shared date-formatting library across multiple modules. |
| Configuration Management | Store shared configuration values in a centralized or authoritative location. | Define timeout values centrally instead of hardcoding them in multiple files. |
| Templates and Generics | Reuse the same logic for different supported data types. | Use a generic sorting function instead of creating separate implementations for each type. |
Real-World Applications of the DRY Principle
The DRY principle is commonly applied in software development to avoid duplicating shared functionality, business rules, and configuration across different parts of an application.
- Reusable functions handle common operations such as string processing, date formatting, and calculations.
- Shared validation logic and business rules help maintain consistent behavior across different modules.
- Common logging utilities, error-handling mechanisms, and centralized configuration reduce duplicated code and inconsistent behavior.
Challenges of the DRY Principle
- Extracting every similar code block can create unnecessary abstractions and make the design harder to understand.
- Shared components can introduce dependencies between modules that would otherwise remain independent.
- Similar-looking code may represent different business rules that need to evolve separately over time.
- Changes to commonly reused functionality can affect multiple components that depend on it.
- Making components overly generic for unrelated requirements can reduce code readability and increase complexity.

