Urgent.News

What's breaking now, across thousands of outlets.

Tech

DRY vs WET Code: When Repeating Yourself Is Actually Better

Every developer has heard the advice: "Don't Repeat Yourself." But what if following that principle too aggressively makes your code harder to maintain? Sometimes, duplicating a few lines of code is better than creating an abstraction that complicates everything. This is where DRY vs WET becomes interesting. What Is DRY Code? DRY (Don't Repeat Yourself) is a software development principle that…

Repetitive lines of code can sometimes be a good thing, according to a piece of advice that challenges the conventional wisdom of "Don't Repeat Yourself." This concept, known as DRY vs WET code, presents a nuanced view of when it's beneficial to duplicate code rather than always striving for abstraction.

DRY stands for "Don't Repeat Yourself," a principle that advocates for minimizing duplication by having just one authoritative representation of every piece of knowledge within a system. For example, in C#, instead of defining two separate methods that both calculate tax in the same way, DRY would suggest creating a single method. While this leads to less duplicated code and easier maintenance overall, the principle isn't without its pitfalls.

WET, or "Write Everything Twice," is the opposite of DRY. It refers to code that repeats itself rather than being abstracted into a unified form. At first glance, WET might seem like a code maintenance nightmare, but there are situations where a little duplication can actually improve understandability. For instance, two validation methods that check for non-empty emails might seem like candidates for abstraction.

However, if these methods need to evolve in different ways in the future, keeping them separate can be advantageous. This approach avoids unnecessary coupling between the code.

The "Rule of Three" is a practical guideline in this debate. The rule suggests examining code duplication at three points: the first occurrence should be written as-is, the second occurrence should prompt a notice but not an immediate abstraction, and the third occurrence should evaluate whether a shared abstraction is necessary. This heuristic helps developers avoid premature abstraction, which can introduce complexity and make future changes more difficult.

When to choose DRY:

- Multiple components share the same business rule.

- Duplicated logic must always remain consistent.

- Changes should propagate across all consumers.

- The abstraction has a clear responsibility.

When to choose WET:

- Similar code serves different business purposes.

- Requirements are likely to evolve independently.

- Extracting shared logic introduces unnecessary complexity.

- A little duplication makes the code easier to understand.

The conclusion from this debate is that good software engineering involves more than just minimizing duplicated code. It's about finding the right balance between reusability, readability, maintainability, and flexibility. While DRY is valuable for preventing duplicated knowledge, there are times when deliberately using WET code can prevent the creation of an abstraction that might not suit future needs.

In essence, duplicating code can sometimes be a wiser choice than committing to an abstraction that might not hold up to future changes.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Hibernate Under the Hood. Part 7: Performance and Common Pitfalls

Performance issues are rarely the fault of Hibernate itself; more often, they are caused by its misuse. The key to success is identifying these issues before they manifest in production.

  • Enable SQL and Hibernate statistics logging to uncover performance issues
  • Address N+1 problem with JOIN FETCH, batch fetching, or DTO projections
  • Disable OSIV in production to avoid unexpected queries and connection pool issues

AWS Agent Registry: How Runtime Tool Discovery Replaces Hardcoded Agent Wiring

Hardcoded tool catalogs break when your agent needs to choose between 200 MCP servers instead of five. AWS Agent Registry solves this by moving tool discovery from build time to runtime.

  • AWS Agent Registry replaces hardcoded tool wiring with runtime tool discovery.
  • Three-step process: Discover, Instantiate, Execute.
  • Metadata includes unique ID, description, schema definitions, and tags.

PNG to WebP for Online Store Product Images: Speed Up Product Pages

Product images are the heaviest thing on most e-commerce pages, and they're also the thing that matters most — they're what the customer actually looks at.

  • Converting PNG to WebP reduces product page file sizes
  • WebP maintains visual quality while optimizing for speed
  • Simple conversion process improves conversion rates and SEO

More from Wednesday 21 October →