Software design principles: DRY

Featured on daily.dev

Today we'll be exploring a design principle called DRY it stands for Don't Repeat Yourself. Of course, a pretty obvious one, meaning you shouldn't type code more than once.

The principle states: "Every piece of logic must have a single unambiguous representation within a system".

Of course, with the upcoming of component-based frameworks, we see less and less of reused codes.

DRY Examples

I'm going to demonstrate some simple use-cases, but they should give you a good understanding of what DRY means.

const foods = ['๐Ÿง€','๐ŸŒถ','๐Ÿ‰'];
const animals = ['๐Ÿฆž','๐Ÿ','๐Ÿ•'];

revFoods = foods.reverse();
revAnimals = animals.reverse();

This is only a very simple function, but this can be converted to a DRY part:

const foods = ['๐Ÿง€','๐ŸŒถ','๐Ÿ‰'];
const animals = ['๐Ÿฆž','๐Ÿ','๐Ÿ•'];

let reverse = (input) => {
  return input.reverse();
}

revFoods = reverse(foods);
revAnimals = reverse(animals);

Why would you do this? Well, think of this code as a little bit more complicated, a full sorting function, and all of a sudden the sorting key changes, you now have to make changes twice, instead of doing it once.

Another good example is validations; this is were I personally fail sometimes.

<?php
class Validator {
    public function validate(array $post)
    {
        if(!isset($post['title']) {
            throw new \Exception('validation failed, no title set');
        }
        if(!isset($post['date']) {
            throw new \Exception('validation failed, no date set');
        }
        if(!isset($post['description']) {
            throw new \Exception('validation failed, no description set');
        }
    }
}

While it's not specifically a violation, we could enhance this and make our lives easier in case that exception might ever change.

<?php
class Validator {

    private $validateAttributes = [
        'title', 
        'date', 
        'description'
    ];

    public function validate(array $post)
    {
        foreach ($this->validateAttributes as $attribute) {
        if (!isset($post[$attribute])) {
            throw new \Exception('validation failed, no '.$attribute.' set');
        }
    }
    }
}

There you go. I hope you learned something about not repeating yourself in code.

Looking forward to hearing what kind of things you do that you could optimize?

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Code Alt's photo

nice ๐Ÿ‘Œ

Chris Bongers's photo

Thanks! ๐ŸคŸ

Diego Malta's photo

Really the first example makes it difficult to understand but the article was very good. Congrats!

Chris Bongers's photo

Hey Diego,

Thanks for this feedback, think you're right and the first one actually made things more confusing in stead of giving a super high-level overview

Strahinja Djuric's photo

DRY principle serves to prevent you from copy/pasting code all around, to reduce code base, and for easier maintenance. I think that the first example is not a really good example of the DRY principle. The other one is totally fine.

Chris Bongers's photo

I tried to explain the first one is not really about the function, but about the DRY principle.

You must see that reverse as a very extensive function.

Thanks for your comment though, appreciate it โœŒ๏ธ