Join Morten Rand-Hendriksen for an in-depth discussion in this video Introducing Don't Repeat Yourself (DRY) as a practice, part of WordPress Developer Tips: DRY Development.
- If this is your first encounter with dry development techniques, you're probably wondering what dry means. Dry is an acronym that stands for don't repeat yourself, and it's one of the core principles of programming. Anytime you have a block of code that will be repeated multiple times in the same file or across multiple different files within the same project, you may want to take that block of code and break it out into it's own function or even it's own file so that you can call in the code anytime you need it, rather than having to copy out the code and place it into the individual files of your project.
At the core of dry development techniques lies a simple question that should be asked anytime you're starting to write a line of code. "Am I going to do this again?" If the answer is yes, then you probably want to package up that code and place it in its own function or in its own file. If the answer is no, and your writing code that's only going to happen once in file and one location, then you don't have to worry about dry development techniques. There are many benefits to applying dry development techniques to your projects. Most prominently, when you use dry development techniques, you write code once and then you can reuse that code over and over again any place you want within your project.
This has some added benefits. First of all, since you only write the code once, if you need to change the code, you only change it in one location and then you can see those changes propagate to where ever that code is being called in. This is a major benefit to applying dry development techniques rather than writing individual blocks of code. Let's say for example you have a custom area within your site that displays some specific information and then you realize that you have to change that information. Well, if you didn't apply dry development principles, you have to go into every single file where that block of code was placed and then make those changes in each file.
If on the other hand, you did apply dry development techniques, then you only have to go make the change in the location where your function or file is sitting and then those changes will apply everywhere the function or file is being called in. As a result of all this, you get code that is much easier to maintain and easier to understand. Every piece of code has been modularized so that you can easily find it, you can see where it's being displayed and you can see the overall structure of your files. Overall, dry development techniques encourages you to have a more structured approach to development.
Rather than looking at each individual template as a standalone unit, you start thinking about how the different pieces of code relate to each other, where they repeat, and how they can be slotted and modified and maybe moved around within your overall structure. That way you make much leaner code that requires less maintenance and you make code that easier to understand when someone comes to it for the first time. The most important thing I want you to walk away with from watching this course, is that you need to aim to stay dry at all times.
By that I mean dry development techniques are applied as you move forward with your project. Every time you write a line of code, you need to ask yourself, "Am I going to do this again?" and if so apply dry development techniques. As you progress through your project and you realize that you are repeating some pieces of code, or you realize there are certain sections that you maybe want to move into multiple different templates, then you should also apply dry development techniques. In my own practice, when I create new themes, I often find myself repeating code and then going back in time finding the original piece of code breaking it out into a function or its own file and then calling it back in.
That may mean I need to add conditional statements to my code to make sure it only triggers when necessary and it also means I often have to go back and refactor a code I just wrote, but the end result is far more understandable and easier to maintain. So, as you move forward with any project, aim to stay dry at all times.
Find more information in the WordPress Developer Tips course.