What is the D.R.Y. Principle in Coding?

We have heard of various tips and tricks about how we can do certain things better, but some of them are not among the common ones you find online. Coding is a difficult profession that requires a lot of focus and knowledge if you want to write good and clean code.

It is important to write clean code, not just for yourself, but for the sake of any developer out there who will eventually interact with your code. There are many things you could do to improve your coding skills.

One of the principles that is often applied is D.R.Y. Here is more about it and what it actually means.

D.R.Y. Explained

D.R.Y. simply means Don’t repeat yourself and is one of the simplest, in theory, principles in coding that every developer should follow. This principle was invented by Dave Thomas and Andy Hunt in their book The Pragmatic Programmer. 

This principle is applied very broadly by the two, to anything from systems, databases, test plans and even documentation. When everything has its own function and you don’t have any repetitions or duplicates, not to mention patterns, you can change things within the code with relative ease.

Why The Principle Is Good

Since everything has its own abstraction and/or function, you don’t have to worry about breaking the chain of events. When you change one line of code, variable or function, you shouldn’t have to change anything else if you want to see an immediate result.

If a function, for example, is written multiple times within a certain code, you would have to change every iteration of the function to keep everything operational. With this principle, all the elements change predictably and by following certain logical rules.

Contrasting Principles

This is just one way you could look at programming, of course. There are always contrasting principles and one of them is the AHA principle. It is not an exclamation but rather something once more, logical. It stands for “avoid hasty abstractions”, meaning that you should always take the time to write your code properly, before opting to use abstractions.

This is almost the exact opposite of the DRY principle. It was created by Kent C. Dodds. The principle he based it one comes from Sandi Metz which goes something like “preferring duplication over the wrong abstractions”. 

Both principles are sound, when they are applied correctly and by a programmer willing to take things slowly.

The DRY principle stands for don’t repeat yourself and is a great principle if you know which abstraction to write and when. The principle is worth the knowledge of the programmer implementing it.

Previous post What is Bootstrap?