Fundamental
DRY Principles

Understanding the DRY (Don't Repeat Yourself) Principle

What is the DRY Principle?

DRY stands for Don't Repeat Yourself. It is a key principle in programming and software development that encourages avoiding duplication of code. The idea is to have a single, clear, and authoritative representation of every piece of logic within your codebase.

In short: "Say it once and only once."


Why is DRY Important?

  1. Reduces Redundancy: Helps eliminate repetitive code, making the codebase smaller and easier to manage.
  2. Improves Maintainability: Changes need to be made in only one place, reducing the risk of errors.
  3. Enhances Readability: Clean, concise code is easier for developers to understand.
  4. Promotes Reusability: Code can be reused in different parts of the application without rewriting.

How to Apply the DRY Principle

1. Extract Repeated Code into Functions

When you notice similar code blocks, refactor them into reusable functions.

Example:

Without DRY:

function calculateRectangleArea(width, height) {
    return width * height;
}
 
function calculateTriangleArea(base, height) {
    return 0.5 * base * height;
}
 
function calculateCircleArea(radius) {
    return Math.PI * radius * radius;
}

With DRY:

function calculateArea(shape, ...dimensions) {
    switch (shape) {
        case 'rectangle':
            return dimensions[0] * dimensions[1];
        case 'triangle':
            return 0.5 * dimensions[0] * dimensions[1];
        case 'circle':
            return Math.PI * dimensions[0] * dimensions[0];
    }
}

2. Use Loops Instead of Repetition

Avoid writing repetitive tasks manually; use loops to handle similar tasks.

Example:

Without DRY:

console.log('User 1 logged in');
console.log('User 2 logged in');
console.log('User 3 logged in');

With DRY:

for (let i = 1; i <= 3; i++) {
    console.log(`User ${i} logged in`);
}

3. Abstract Common Logic

Move shared logic into a common module or utility.

Example:

Without DRY:

function getUserFullName(user) {
    return user.firstName + ' ' + user.lastName;
}
 
function getAdminFullName(admin) {
    return admin.firstName + ' ' + admin.lastName;
}

With DRY:

function getFullName(person) {
    return person.firstName + ' ' + person.lastName;
}

4. Use Templates or Frameworks

For repetitive HTML or UI components, use templates or framework components.

Example:

  • Avoid: Manually copying and pasting similar HTML code.
  • Do: Use reusable components in frameworks like React or Angular.

Common Mistakes Against DRY

  1. Over-Abstracting: Making code overly abstract and difficult to understand by trying to reuse too much.
  2. Copy-Pasting Code: Duplicating code instead of refactoring.
  3. Lack of Modularity: Writing tightly coupled code that cannot be reused in other parts of the application.

Real-World Analogy

Imagine you're writing instructions for assembling furniture. Instead of repeating "Attach screw A to panel B" for every panel, you write it once and refer to it wherever needed. This saves space and avoids confusion if the instruction changes later.


Conclusion

The DRY principle emphasizes writing clean, reusable, and modular code. By reducing redundancy, you create a more maintainable and scalable codebase.