Is your code DRY or WET? (2024)

Don’t Repeat Yourself(DRY) is a software development principle, the main aim of which is to reduce repetition of code.

Write Every Time(WET) is a cheeky abbreviation to mean the opposite i.e. code that doesn’t adhere to DRY principle.

It is quite obvious which one of the two should all developers be aiming for. But hey!, someone out there might be proving me wrong at this very moment.

In this post, we look at the benefits of applying DRY principle to your code. Firstly, we will start with a simple example that illustrates the basic advantage of the DRY principle.

DRY - Simple example

Assume you have many places in your code that need to be executed based on the current user’s role. For instance, createPage() can only be executed if the user is an editor or an administrator, deletePage() only if the user is an administrator etc.

Instead of spreading the logic of checking for a user’s role in both createPage and deletePage, we could use a single function isPermitted() as below.

//get the current SubjectSubject currentUser = context.getSubject();if (isPermitted(currentUser)) { //allow execution of deletePage} else { //block execution}

By keeping the logic of isPermitted() to one place, you avoid duplication and also enable re-use of the code. The added advantage is separation of logic i.e. createPage() and deletePage() don’t need to know how the permission is checked.

As always there is more than meets the eye.

Advantages of DRY

Maintainability

The biggest benefit of using DRY is maintainability.

If the logic of checking permission was repeated all over the code, it becomes difficult to fix issues that arise in the repeated code. When you fix a problem in one, you could easily forget to fix the problem in other occurrences.

Also, if you have to modify the logic, you have to copy-paste all over the place. By having non-repeated code, you only have to maintain the code in a single place. New logic and bug fixes can be made one place instead of many. This leads to a robust and dependable software.

Readability

More often than not, DRY code is more readable. This is not because of the DRY principle itself, but rather because of the extra effort the developer put in to the code to make it follow certain principles such as DRY.

Reuse

DRY inherently promotes reuse of code because we are merging 2 or more instances of repeating code into a single block of code. Reusable code pays of in the long run as it speeds development time.

Cost

If the management needs to be convinced regarding spending more time on improving the quality of code, this is it – More code costs more. More code takes more people more time to maintain and to address bugs. More time to develop and more bugs leads to a very unhappy customer. Enough said!

Testing

We are talking about unit tests and integration tests here, not manual testing. The more paths and functions you have to cover using the tests, the more code you have to write for tests. If code is not repeated, you just have to test one main path. Of course, different behaviors still need to be tested.

Caution

With all the advantages of using DRY, there are some pitfalls though.

  1. Not all code needs to be merged into one piece. Some times 2 pieces of code can look similar but with subtle differences. When to merge these prices of code into one and when to leave them separated needs to be thought over carefully.
  2. If the code is “over dried”, it becomes difficult to read and understand. I have also seen developers trying to apply DRY principle even when there is only one instance of a block of code. While I appreciate their thinking and foresight into making the code better and reusable, I wouldn’t bother to make the code follow DRY principle until the situation to re-use it is needed.
  3. Often missed, DRY is not to be limited to just the code. It is to be applied in equal measure to database design, documentation, testing code etc.
Is your code DRY or WET? (2024)

FAQs

Is your code DRY or WET? ›

In a practical sense, WET code typically involves the repeated writing of the same code throughout a project, whereas DRY code

DRY code
"Don't repeat yourself" (DRY) is a principle of software development aimed at reducing repetition of information which is likely to change, replacing it with abstractions that are less likely to change, or using data normalization which avoids redundancy in the first place.
https://en.wikipedia.org › wiki › Don't_repeat_yourself
would represent the repeated reference of that code.

What is wet vs DRY code? ›

Don't Repeat Yourself(DRY) is a software development principle, the main aim of which is to reduce repetition of code. Write Every Time(WET) is a cheeky abbreviation to mean the opposite i.e. code that doesn't adhere to DRY principle. It is quite obvious which one of the two should all developers be aiming for.

Can code be too DRY? ›

The Pitfalls of “Too DRY Code”:

While DRY code and simple solutions have their advantages, adhering to these principles too rigidly can lead to several issues: Abstraction Overload: Overusing abstraction in the name of reusability can lead to complex and hard-to-follow code.

What is the wet code rule? ›

DRY means “Don't repeat yourself” so every piece of your code must be in only one place. It is a prohibition to duplicate. At the opposite, the acronym WET “Write Every Time” or “Write Everything Twice” considers that we can duplicate code without problems.

What is one way to keep your code DRY? ›

Understanding the DRY Principle

In simpler terms, if you find yourself writing the same code in multiple places, STOP! There's probably a better way to do it. You should aim to write code that can be reused instead of repeated.

What is kiss code? ›

The KISS principle is an acronym for "Keep It Simple, Stupid." This principle states that we should not add unnecessary complexity to our software. Instead, we should keep our code simple and focused on the task at hand. This will make it easier to understand and maintain our code.

Should test code be DRY? ›

You should favor DRY over DAMP in the production code, and DAMP over DRY in the test code.

What is considered too DRY? ›

The ideal relative humidity level for maximum comfort and health is between 40% and 50%, and anything below 30% is considered too dry. Humidity levels naturally drop along with outdoor temperatures as cold air can't hold the same amount of moisture as warm air.

Why do we write DRY code? ›

"Don't repeat yourself" (DRY) is a principle of software development aimed at reducing repetition of information which is likely to change, replacing it with abstractions that are less likely to change, or using data normalization which avoids redundancy in the first place.

What is the DRY code method? ›

DRY stands for Don't Repeat Yourself. It's a software development principle with the goal of removing logic duplication. DRY was first introduced in the book The Pragmatic Programmer and ensures that a particular logic appears only once in the codebase.

What is the difference between wet and damp? ›

Wet is defined as "covered with visible free moisture," damp is a "moderate covering of moisture," and moist is "slightly damp but not quite dry to the touch." Thus wet indicates the highest level of moisture and moist indicates the lowest level.

What is violating the DRY principle? ›

Fun fact: Violations of the DRY principle are commonly known as WET (which translates to "Write everything twice," "Write every time," or "We enjoy typing," depending on whom you ask). Programmers and their fun abstractions!

Which is a benefit of DRY code? ›

Why should you go DRY first? One of the most important benefits is readability. The code itself will not become more readable than it was, but you will have a lot less code coming from a single source. Less code means better performance and faster execution.

What is the difference between wet and DRY specimen? ›

Dry mount: A specimen is placed on a slide with or without a coverslip over it. This method is useful for viewing dry specimens such as hair or pollen. Wet mount: A specimen is placed on a drop of water on the slide, with a coverslip placed over it. This is often used for viewing cells.

What does it mean to dry run code? ›

A dry run is the process of a programmer manually working through their code to trace the value of variables. There is no software involved in this process. Traditionally, a dry run would involve a print out of the code.

What is the difference between wet and DRY sales? ›

'Wet' refers to drink sales while 'dry' refers to food sales.

What is DRY and wet testing? ›

In a wet lab, testing and analyses are performed using physical samples, chemicals and liquids. Biological samples, drugs and fluids are all tested in a wet lab. In other words, “wet” materials are tested. In a dry lab, testing and analyses is performed using data, coding and computer systems.

Top Articles
Latest Posts
Article information

Author: Catherine Tremblay

Last Updated:

Views: 5965

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Catherine Tremblay

Birthday: 1999-09-23

Address: Suite 461 73643 Sherril Loaf, Dickinsonland, AZ 47941-2379

Phone: +2678139151039

Job: International Administration Supervisor

Hobby: Dowsing, Snowboarding, Rowing, Beekeeping, Calligraphy, Shooting, Air sports

Introduction: My name is Catherine Tremblay, I am a precious, perfect, tasty, enthusiastic, inexpensive, vast, kind person who loves writing and wants to share my knowledge and understanding with you.