In this article we will explore one of the main software design principles – D.R.Y, its benefits, and downfall of violating this principle. I will also provide short code snippet to help demonstrate this principle.

What is the D.R.Y Principle

D.R.Y, which stands for “Do Not Repeat Yourself”, is a principle of software development  aimed at reducing the of repetition code.

The Authors of the book The Pragmatic Programmer described D.R.Y as “every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

Knowledge would be your business logic, and algorithms or a variable used in your application. So, in simple terms what the definition is saying is that every piece of individual logic or algorithm or info should live and be expressed in a single location(function etc.) within your code.

The alternative to this would be to have the same thing expressed in two or more places. If you change one, then you’ll have to remember to change the others – and waste everyone’s time.

D.R.Y Violation

Let us take a look at simple piece of code that violates the D.R.Y principle. We will then we update our code to adhere to the D.R.Y principle.

Let’s imagine we have the following class:

class Player {
  constructor() {
    this.position = null;
  }

  moveRight() {
    this.position = "Right";
    console.log("Moved player -- Right");
    this.rest();
  }

  moveLeft() {
    this.position = "Left";
    console.log("Moved player -- Left");
    this.rest();
  }

  moveUp() {
    this.position = "Up";
    console.log("Moved player -- Up");
    this.rest();
  }

  moveDown() {
    this.position = "Down";
    console.log("Moved player -- Down");
    this.rest();
  }

  rest(){
    console.log("Taking a break...... Drinking some water :)");
  }
}

Our class has methods that allows a player to move left, right, up, and down. However, each method ( moveRight(), moveLeft(), moveUp(), moveDown() ) all repeats the same logic.

If we wanted to perform some more task after moving (similar to rest()) then we will have to update each of our existing methods to include that new piece of code. We could update our class to the following:

class Player {
  constructor() {
    this.position = null;
  }

  move(direction) {
    this.position = direction;
    logMessage(`Moved player -- ${direction}`);
    this.rest();
  }

  rest(){
    console.log("Taking a break...... Drinking some water :)");
  }
}

Here we updated our class to have a single move() method which will handle moving the player. We also included a function, logMessage(), which we’ll pass our confirmation messages to. Let’s imagine that this function is available for usage across our entire codebase.

Benefits of Keeping D.RY.

Adhering to the D.R.Y principle provides many benefits. Some of which are mentioned below. While reading these benefits you’ll also get a picture of what life is like when you fail to follow the D.R.Y principle.

Maintainability

If you have repeated logic all over your application then it will be a nightmare to make updates. If you fix an issue or make an update in one place then you have to remember to make the changes in the other places – and we’re likely to forget all the places that needs this fix. However, if we have our logic in one place then we only need to make a fix in that one place making our code easier to maintain.

Promotes code reuse

With the D.R.Y principle, logic that was expressed across two or more instances in now available at a single source. Whenever you need that piece of logic you can simply reuse the existing source (variable, function etc.).

Easier to test

If a piece of logic is duplicated across your application codebase then you’ll need to write test to cover the various areas where the duplication is present. Having a single place test test makes would make testing much easier.


In this article we took a look at the D.R.Y principle, and it’s benefits. We also got an insight into how difficult life can get when we violate this principle. We should always try to refactor and remove duplication from code as much as possible.

If you found this article helpful then leave a comment below. Also spread the knowledge with your peers by sharing this article. Until next time, think, learn, create, repeat!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: