OOP · Design Principles
DRY (Don't Repeat Yourself)
Improves flexibility and scalability.
- Improves flexibility and scalability.
- Reduces development time.
- Simplifies testing and debugging.
- Makes team collaboration easier.
- Produces clean and professional software.
Best Practices
- Apply Design Principles from the beginning of a project.
- Choose simple solutions whenever possible.
- Avoid unnecessary complexity and duplication.
- Keep classes focused on a single purpose.
- Prefer loose coupling and high cohesion.
- Review and refactor code regularly.
Common Mistakes
- Ignoring code duplication.
- Creating overly complex solutions.
- Building features that are not currently needed.
- Creating tightly coupled classes.
- Misusing inheritance where composition is more appropriate.
- Applying principles without understanding the problem.
Real-World Problem
Imagine you're developing an Employee Management System. Initially, the application contains only one module for calculating employee salaries. Since the project is small, developers write the salary calculation logic directly inside the class.
As the application grows, new modules such as Payroll, HR Reports, Bonus Calculation, Tax Management, and Performance Evaluation also require salary calculations. Instead of reusing the existing logic, developers copy and paste the same code into each module.
After several months, the company changes the salary calculation formula. Developers now have to search every module where the duplicated code exists and update each copy individually. Missing even one copy results in incorrect salary calculations and inconsistent application behavior.
Problem The same salary calculation logic is repeated in multiple classes. Every time the business rule changes, developers must modify the code in several places instead of updating it once. This duplication increases maintenance effort, introduces inconsistencies, and makes the application harder to understand and manage.
Pain Points When the salary calculation formula changes, developers must modify the same logic in every class where it has been copied. This increases the chances of mistakes, creates inconsistent behavior, and consumes unnecessary development time. Code duplication also makes applications larger and more difficult to maintain.
Solution
The DRY (Don't Repeat Yourself) principle states that every piece of knowledge or logic should exist only once in a software system.
Instead of copying the same code into multiple classes, place the common logic inside a reusable method or class. Other modules should call that method whenever they need the functionality. This ensures that future changes need to be made in only one place.

Java Program 1 – DRY Violation
Java Program 2 – DRY Applied

Benefits
Eliminates duplicate code. Makes applications easier to maintain. Improves code reusability. Reduces the chance of bugs.

Every piece of knowledge should live in exactly one place.
// BAD: the same tax rule copy-pasted everywhere
// double a = price + price * 0.18;
// double b = fee + fee * 0.18;
public class Tax {
private static final double GST = 0.18; // one source of truth
public static double withTax(double amount) {
return amount + amount * GST;
}
public static void main(String[] args) {
System.out.println(withTax(100)); // reused, never duplicated
System.out.println(withTax(250));
}
}