The DRY principle and why you should use it

Pety Ialimijoro
4 min readJul 15, 2018

--

DRY stands for Don’t Repeat Yourself, it is a software development principle aiming to reduce code duplication which can lead to a poor refactoring and a poor maintenance. In other word, if you have to copy and paste the same exact lines of codes which provide the same functionality, then chances are that your codes don’t follow the DRY at all.

Example Case

Let say you’re building a simple PHP web application, you have three PHP scripts: index.php for the home page, users.php for displaying all users and user.php for displaying a specific user.Of course in a real PHP web application you’ll have a more html markups, some CSS, JavaScript and Databases but for this example we’ll keep it simple just to demonstrate the DRY principle.

The non DRY approach

The codes will looks like the following :

On the index.php, we have a header which contains the navigation menu, and on section we have the content.

On the users.php, we have the same layout but the content is now a list of users from our users.json data. We are fetching the data by using PHP function file_get_contents() which reads a file and returns it to a string, then we use the json_decode() function which converts a json string format to a PHP variable, then we can use the ->users to get the users data because the return value of json_decode() is a PHP stdClass Object. After that we’re just looping trough the users and them inside a <li> tag, note that the <?= is just a shortcut of <?php echo.

For user.php, we just pass an id with url parameter to find out which user do we want, then we display the user information on section if the user with a given id is found, otherwise we will render a paragraph informing that the User is not found.

What’s wrong with this codes according to DRY?

  1. The HTML layout markup

Notice that all of our pages have the same HTML markups except the section content and we’re just copying all of them within our pages. This is a code duplication and imagine if one day we’ll decide to change just the navigation menu, then we will have to change all the 3 files. You may say that it’s just 3 files but this is just for example, on a real web project you will probably have like 10 PHP files which share the same layout.

2. The way we retrieve the users data

json_decode(file_get_contents(‘users.json’))->users is the code responsible for retrieving the users data from our json, we have it within our users.php on line 2 and within the user.php on line 3. Again, there is a duplication and imagine if we will change our json users to something like members for example, then we will have to change 2 files. In real web project, it can be the name of database table, so you will have to change all of the file and code which query the given table, it can be a big problem and very frustrating.

The DRY approach

  1. Create a base templates

As we said earlier, all of our pages have the same layout and HTML markup except the section content, so what we need to do is to create a 2 files called header.php and footer.php, then we will just require them on our index.php, users.php and user.php. That way when we need to change the navigation menu for example or add some other meta tag, we will change the header.php only.

2. Create a model for retrieving the users data

We will create a file called userModel.php under a directory called models, it will have one function called getAllUsers() that we can use to get all the users and another function called getUserById() to get a specific user by id. This gives us more flexibility, let say that in our json data, we’ll change the “users” proprety to “members”, then we will just change the line 3 of the userModel.php to return json_decode(file_get_contents(‘users.json’))->members and all the pages work as before.

Our codes look like the following now :

Our code looks more organized and easier to maintain after implementing the DRY principle. This example is pretty simple but in a big project, the DRY principle is a must for a long run success.

To summarize, if you have a code duplication on your code, then you need to figure out how you can do to make some kind of adjustment in order to follow the DRY principle. Trust me, it will save your time whenever you need to change something on your code.

The DRY principle is not for PHP development only, it can be used with anything related to software development.

--

--