System Design
10-Min Deep Dive

What is DRY vs WET?

Title DRY vs WET: Understanding the Principles of Code Organization

SEO Keywords code organization, DRY principle, WET principle, software development, programming best practices

Intro

As developers, we're constantly striving to write better code. We want our programs to be efficient, maintainable, and easy to understand. Two principles that can help us achieve these goals are the Don't Repeat Yourself (DRY) principle and the Write Everything Twice (WET) principle. In this post, we'll explore what DRY and WET mean, how they relate to code organization, and why it's essential to follow the DRY principle in software development.

Main Blog Content

What is DRY?

The Don't Repeat Yourself principle is a fundamental concept in programming that suggests you should avoid duplicating code. When you write identical or similar code blocks multiple times in your program, you're creating unnecessary redundancy. This can lead to maintenance nightmares and make it harder to update your code.

For example, let's say you're building a simple calculator app and you have two methods for adding and subtracting numbers:

public int add(int a, int b) {
    return a + b;
}

public int subtract(int a, int b) {
    return a - b;
}

In this example, the code for the add method is identical to the code for the subtract method. If you wanted to change the way numbers are calculated, you'd have to update both methods separately. This is where DRY comes in – by extracting the common logic into a single method, you can avoid duplication:

public int calculate(int a, int op, int b) {
    return op == 1 ? a + b : a - b;
}

// Usage:
int result = calculate(2, 1, 3); // adds 2 and 3
result = calculate(4, 0, 1); // subtracts 4 from 1

What is WET?

The Write Everything Twice principle is the opposite of DRY. It suggests that you should write the same code multiple times to achieve a specific goal or functionality.

Imagine you're building a web application with multiple forms and each form has a "Submit" button with similar logic:

<!-- Form 1 -->
<form>
    <button>Submit</button>
</form>

<!-- Form 2 -->
<form>
    <button>Submit</button>
</form>

<!-- Form 3 -->
<form>
    <button>Submit</button>
</form>

In this example, the "Submit" button is identical across all forms. If you wanted to change the behavior of the submit buttons or add some new functionality, you'd have to update each form individually. This is where WET comes in – by writing the same code multiple times, you can achieve a specific goal:

<!-- Form 1 -->
<form>
    <button onclick="submitForm()">Submit</button>
</form>

<!-- Form 2 -->
<form>
    <button onclick="submitForm()">Submit</button>
</form>

<!-- Form 3 -->
<form>
    <button onclick="submitForm()">Submit</button>
</form>

The Importance of DRY

While WET might seem like a good idea at first, it's not the most efficient or maintainable approach. When you follow the DRY principle, you avoid duplicating code and make your program more:

  • Efficient: You don't have to update multiple places when you want to change some behavior.
  • Maintainable: Your code is easier to understand and modify when it's organized.
  • Scalable: As your program grows, the DRY principle helps you keep it manageable.

In summary, the Don't Repeat Yourself principle encourages you to write unique code that can be reused throughout your program. This leads to more maintainable, efficient, and scalable software development practices.

TL;DR

When writing code, avoid duplicating logic by following the Don't Repeat Yourself (DRY) principle. Instead of repeating yourself, extract common logic into reusable methods or functions. This approach leads to more efficient, maintainable, and scalable software development practices.

Propagate this knowledge. Link copied automatically on click.
What is DRY vs WET? - 10-Minute Engineering Brief | DevExCode | DevExCode