SDEV140 Sample Coding Process

The DRY Coding Principle

Introduction

Coding principles exist to help us produce better code. These are generally guidelines. In some organizations, they become standards that are included in code audits. Developers should at least understand the principles, and why they are recommended. This article covers the DRY principle.

Keep DRY

DRY stands for "Don't Repeat Yourself". The purpose of DRY is to emphasize the importance of efficient code. In this case, efficient is achieved by limiting duplicated code. Duplicated code does not run any quicker; it is not more efficient from a performance perspective. But it does produce code that is more consistent and much easier to maintain. That produces more efficient programmer production, and helps reduce errors.

Analyzing a program using the DRY principle focuses on identifying duplicate code. If multiple statements are exactly the same, there is usually a better way to accomplish the same result. Sometimes this is done using a function. Other times, it is just a matter or reorganizing the code. See the example of the code below and how it can be improved.

Example

The following code runs perfectly. There is nothing wrong with it:

# No Discount
if NumPackages < 10:
    print (f"You have not purchased enough packages to receive a discount.")
    print (f"Final sale: " , PkgTotal)
# Discount 1
elif NumPackages >= Break1 and NumPackages <= Break2:
    Discount = PkgTotal * Discount1
    print (f"The number of packages: " , NumPackages)
    print (f"Total Sale: " , PkgTotal)
    print (f"Discount percent: " , (Discount1 * 100) , "%")
    print (f"Discount Amount: " , Discount )
    print (f"Final sale: " , (PkgTotal - Discount))
# Discount 2
elif NumPackages >= Break2 and NumPackages <= Break3:
    Discount = PkgTotal * Discount2
    print (f"The number of packages: " , NumPackages)
    print (f"Total Sale: " , PkgTotal)
    print (f"Discount percent: " , (Discount2 * 100) , "%")
    print (f"Discount Amount: " , Discount)
    print (f"Final sale: " ,(PkgTotal - Discount ))
# Discount 3
elif NumPackages >= Break3 and NumPackages <= Break4:
    Discount = PkgTotal * Discount3
    print (f"The number of packages: " , NumPackages)
    print (f"Total Sale: " , PkgTotal)
    print (f"Discount percent: " , (Discount3 * 100) , "%")
    print (f"Discount Amount: " , Discount)
    print (f"Final sale: " , (PkgTotal - Discount ))
# Discount 4
elif NumPackages >= Break4:
    Discount = PkgTotal * Discount4
    print (f"The number of packages: " , NumPackages)
    print (f"Total Sale: " , PkgTotal)
    print (f"Discount percent: " , (Discount4 * 100) , "%")
    print (f"Discount Amount: " , Discount)
    print (f"Final sale: " , (PkgTotal - Discount ))

But it can be improved considerably, by using the DRY principle.

# No Discount
if NumPackages < 10:
    print (f"You have not purchased enough packages to receive a discount.")
    print (f"Final sale: " , PkgTotal)
# Discount 1
elif NumPackages >= Break1 and NumPackages <= Break2:
    Discount = PkgTotal * Discount1
# Discount 2
elif NumPackages >= Break2 and NumPackages <= Break3:
    Discount = PkgTotal * Discount2
# Discount 3
elif NumPackages >= Break3 and NumPackages <= Break4:
    Discount = PkgTotal * Discount3
# Discount 4
elif NumPackages >= Break4:
    Discount = PkgTotal * Discount4
# Display output

print (f"The number of packages: " , NumPackages)
print (f"Total Sale: " , PkgTotal)
print (f"Discount percent: " , (Discount4 * 100) , "%")
print (f"Discount Amount: " , Discount)
print (f"Final sale: " , (PkgTotal - Discount ))

 

Why DRY

The example above illustrate why DRY is helpful. Consider a request to change the output to read "Final Amount" instead of "Final sale". In the original code, this change would have to be made four times. If the coder misses making the change in one of those sections, the program would not be correct (based on the change request). More importantly, perhaps, the code would have to test multiple inputs - since the original display output is dependent on the entry values and code in different sections.

In the improved version, all print statements are in the same place, and easier to maintain. Any change has to be made in only a single place. Testing the output statements is not dependent on the entry values; the same statements will be used, regardless of the entry values.

Overall, the improved code is considered much "cleaner". Easier to read and maintain. Less prone to creating an error after changes.

DRY is not always something that is evident as one codes. It is often something that is revealed as one reviews their code and realizes it can be cleaned up and made more efficient.

When possible, keep your code DRY.