What is the D.R.Y Principle?
Tags: productivity, refactorit
Published: 2021-02-20 17:51:55



<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>What is the D.R.Y Principle</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>D.R.Y, which stands for "Do Not Repeat Yourself", is a principle of software development&nbsp; aimed at reducing the of repetition code.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The Authors of the book <strong><em>The Pragmatic Programmer</em></strong> described D.R.Y as "<em>every piece of knowledge must have a single, unambiguous, authoritative representation within a system.</em>"</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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. <br><br>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 <em>waste everyone's time</em>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>D.R.Y Violation</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's imagine we have the following class:</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->


```javascript
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 :)");
  }
}
```


<!-- wp:paragraph -->
<p>Our class has methods that allows a player to move left, right, up, and down. However, each method ( <em>moveRight()</em>, <em>moveLeft()</em>, <em>moveUp()</em>, <em>moveDown() </em>) all repeats the same logic.<br><br>If we wanted to perform some more task after moving (similar to <em>rest())</em> 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:</p>
<!-- /wp:paragraph -->


``` javaScript
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 :)");
  }
}
```


<!-- wp:paragraph -->
<p>Here we updated our class to have a single <em>move()</em> method which will handle moving the player. We also included a<em> </em>function, <em>logMessage()</em>, which we'll pass our confirmation messages to. Let's imagine that this function is available for usage across our entire codebase. </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Benefits of Keeping D.RY.</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Maintainability </h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Promotes code reuse</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.).</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Easier to test</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><br>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. <br><br>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, <em>think, learn, create, repeat!</em></p>
<!-- /wp:paragraph -->