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.
DRY – Simple example
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 (
isPerm
itted(currentUser)) {
//
allow execut
ion
of deleteP
age
} else {
//block execut
ion
}
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.
Advantages of DRY
I- Maintainability
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.
II- Readability
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.
III- Reuse
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.
IV- Cost
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.
V- Testing
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.
Caution
With all the advantages of using DRY, there are some pitfalls though.
- Not all code needs to be merged into one piece. Some times 2 pieces of code can look similar but with subtle differences. When to merge these prices of code into one and when to leave them separated needs to be thought over carefully.
- If the code is “over dried”, it becomes difficult to read and understand. I have also seen developers trying to apply DRY principle even when there is only one instance of a block of code. While I appreciate their thinking and foresight into making the code better and reusable, I wouldn’t bother to make the code follow DRY principle until the situation to re-use it is needed.
- Often missed, DRY is not to be limited to just the code. It is to be applied in equal measure to database design, documentation, testing code, etc.
What is a benefit of drying up code? 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.
more What code is code 8? Code 08 (or Code B) is
the most common driver's license, allowing you to drive any vehicle (except motorcycles and agricultural vehicles) lighter than 3 500 Kg.
more Is a CPT code a diagnosis code? 2.
The CPT code describes what was done to the patient during the consultation, including diagnostic, laboratory, radiology, and surgical procedures while the ICD code identifies a diagnosis and describes a disease or medical condition. 3. CPT codes are more complex than ICD codes.
more What is medical benefit vs pharmacy benefit? Medical benefit drugs are ones that are injected or infused by a healthcare professional in an out-patient clinic or infusion centers. Whilst, pharmacy benefit drugs are self-administered and include orals, self-injectable, or a route of administration a patient can manage at home.
more Is a HCPCS code a CPT code? HCPCS (Healthcare Common Procedures Coding System) CMS includes two levels in its Healthcare Common Procedures Coding System:
HCPCS Level I is the CPT coding system; HCPCS Level II is usually referred to as HCPCS codes, described above.
more What is Code 7 in police code? In police jargon, “Code 7" means “
taking a lunch break.” But Hill's Code 7 restaurant is on an extended break. The 1st Street eatery, patronized by cops, politicians and reporters, has gone out of business.
more What is pharmacy benefit vs medical benefit? Medical benefit drugs are ones that are injected or infused by a healthcare professional in an out-patient clinic or infusion centers. Whilst, pharmacy benefit drugs are self-administered and include orals, self-injectable, or a route of administration a patient can manage at home.
more Is MICR code and IFSC code same? IFSC is an 11-digit alpha-numeric code that uniquely identifies a bank-branch participating in the RBI regulated funds transfer system. MICR is a 9 digit unique code that identifies bank and bank branches in the ECS for the transfer of funds via cheque.
more Is a bank code a sort code? What is a sort code? A sort code is an important factor of your bank account.
A sort code is a 6 digit number that identifies your bank. It's usually split up into pairs; the first two digits identify which bank it is and the last four digits refer to the specific branch of the bank, where you opened the account.
more Is ICD code the same as diagnosis code? Diagnosis codes, such as the ICD-10-CM, are officially called the International Classification of Diseases, 10th Revision, Clinical Modification. These codes describe an individual's disease or medical condition.
more Is a CPT code a procedure code? CPT codes ® , or the Current Procedural Terminology codes, are
five-digit procedure codes that describe the service rendered by the healthcare professional. The MNT codes 97802, 97803, and 97804 are CPT ® codes that RDNs use on claims to report nutrition services provided by the RDN.
more