DRY (Don’t Repeat Yourself) is a software development principle, the main purpose of which is to reduce the repetition of code.
WET (Write Every Time) is a cheeky abbreviation to mean the opposite i.e. code that doesn’t adhere to DRY principle.
It is clear which one of the two should all developers work in. But hey! someone out there might say it is wrong.
In this article, we will look at the benefits of applying DRY principle to your code. First of all, let’s start with a simple example that illustrates the basic advantage of the DRY principle.
Assume you have many places in your code that need to be executed based on the current user’s role. For instance, createPage() can only be executed if the user is an editor or an administrator, deletePage() only if the user is an administrator too.
Instead of spreading the logic of checking for a user’s role in both createPage and deletePage, we can use one function isPermitted() as below.
//get the current Subject
Subject currentUser = context.getSubject();
if (isPermitted(currentUser)) {
//allow execution of deletePage
} else {
//block execution
}
When you keep the logic of isPermitted() to one place, you avoid duplication and also enable re-use of the code. The added advantage is separation of logic i.e. createPage() and deletePage() don’t need to know how the permission is checked.
As always there is more than get to you every day.
The biggest benefit of using DRY is maintainability.
If the logic of checking permission was repeated all over the code, it becomes difficult to fix issues that arise in the repeated code. When you fix a problem in one, you could easily forget to fix the problem in other occurrences.
Also, if you have to modify the logic, you have to copy-paste all over the place. By having a non-repeated code, you only have to maintain the code in a single place. New logic and bug fixes can be made one place instead of many. This leads to a robust and dependable software.
More often than not, DRY code is more readable. This is not because of the DRY principle itself, but rather because of the extra effort, the developer put into the code to make it follow certain principles such as DRY.
DRY inherently promotes reuse of code because we are merging two or more instances of repeating code into a single block of code. The reusable code pays off in the long run as it speeds development time.
If the management needs to be convinced regarding spending more time on improving the quality of code, this is it – More code can cost more. More code takes more people, more time to maintain and to address bugs. In addition, More time to develop and more bugs lead to a very unhappy customer.
We are not talking about manual testing; We are talking about unit tests and integration tests here. The more paths and functions you have to cover using the tests, the more code you have to write for tests. If code is not repeated, you just have to test one main path. Of course, different behaviors still need to be tested.
With all the advantages of using DRY, there are some pitfalls though.