Welcome to the very first lesson of the "Applying Clean Code Principles" course! In this lesson, we will focus on a fundamental concept in clean coding: the DRY ("Don't Repeat Yourself") principle. Understanding DRY is crucial for writing efficient, maintainable, and clean code. This principle is not just important for coding interviews but also in everyday software development. Today, we will dive deep into issues caused by repetitive code and explore strategies to combat redundancy. 🚀
Repetitive functionality in code can introduce several issues that affect the efficiency and maintainability of your software:
-
Code Bloat: Repeating similar code across different parts of your application unnecessarily increases the size of the codebase. This repetition makes the code harder to navigate and increases the chances of introducing errors.
-
Risk of Inconsistencies: When similar pieces of logic are scattered across different areas, it's easy for them to become out of sync during updates or bug fixes. This situation can result in logic discrepancies and potentially introduce new problems.
-
Maintenance Challenges: Updating code often requires modifications in multiple places, leading to increased work and a higher likelihood of errors. Redundant code makes it difficult for developers to ensure all necessary changes have been made consistently.
To adhere to the DRY principle and avoid repeating yourself, several strategies can be employed:
-
Extracting Method: Move repeated logic into a dedicated method that can be called wherever needed. This promotes reuse and simplifies updates.
-
Extracting Variable: Consolidate repeated expressions or values into variables. This centralizes change, reducing the potential for errors.
-
Replace Temp with Query: Use a method to compute values on demand rather than storing them in temporary variables, aiding readability and reducing redundancy.
Consider the following problematic code snippet where repetitive logic is used for calculating the total price based on different shipping methods:
C#1public double CalculateClickAndCollectTotal(Order order) 2{ 3 // Duplicate logic 4 double itemsTotal = 0; 5 foreach (Item item in order.Items) 6 { 7 itemsTotal += item.Price * item.Quantity; 8 } 9 double shippingCost = itemsTotal > 100 ? 0 : 5; 10 return itemsTotal + shippingCost + order.Tax; 11} 12 13public double CalculatePostShipmentTotal(Order order, bool isExpress) 14{ 15 // Duplicate logic 16 double itemsTotal = 0; 17 foreach (Item item in order.Items) 18 { 19 itemsTotal += item.Price * item.Quantity; 20 } 21 double shippingCost = isExpress ? itemsTotal * 0.1 : itemsTotal * 0.05; 22 return itemsTotal + shippingCost + order.Tax; 23}
Both methods contain duplicated logic for calculating the total price of items, making them error-prone and hard to maintain. Now, let's refactor this code.
By consolidating the shared logic into a separate method, we can eliminate redundancy and streamline updates:
C#1public double CalculateClickAndCollectTotal(Order order) 2{ 3 double itemsTotal = CalculateItemsTotal(order); 4 double shippingCost = itemsTotal > 100 ? 0 : 5; 5 return itemsTotal + shippingCost + order.Tax; 6} 7 8public double CalculatePostShipmentTotal(Order order, bool isExpress) 9{ 10 double itemsTotal = CalculateItemsTotal(order); 11 double shippingCost = isExpress ? itemsTotal * 0.1 : itemsTotal * 0.05; 12 return itemsTotal + shippingCost + order.Tax; 13} 14 15private double CalculateItemsTotal(Order order) 16{ 17 double itemsTotal = 0; 18 foreach (Item item in order.Items) 19 { 20 itemsTotal += item.Price * item.Quantity; 21 } 22 return itemsTotal; 23}
By extracting the CalculateItemsTotal
method, we centralize the logic of item total calculation, leading to cleaner, more maintainable code.
Let's look at another example dealing with repeated calculations for discount rates:
C#1public double ApplyDiscount(double price, Customer customer) 2{ 3 double loyaltyDiscount = customer.LoyaltyLevel * 0.02; 4 price *= (1 - loyaltyDiscount); 5 // Additional discounts 6 double seasonalDiscount = 0.10; 7 price *= (1 - seasonalDiscount); 8 return price; 9}
Here, the discount rates are scattered throughout the code, which complicates management and updates.
We can simplify this by extracting the discount rates into variables:
C#1public double ApplyDiscount(double price, Customer customer) 2{ 3 double loyaltyDiscount = customer.LoyaltyLevel * 0.02; 4 double seasonalDiscount = 0.10; 5 6 double totalDiscount = 1 - loyaltyDiscount - seasonalDiscount; 7 price *= totalDiscount; 8 9 return price; 10}
Now, with the totalDiscount
variable, the logic is cleaner, more readable, and allows changes in just one place. 🎉
Our final example involves temporary variables that lead to repetition:
C#1public bool IsEligibleForDiscount(Customer customer) 2{ 3 bool newCustomer = customer.SignUpDate > DateTime.Now.AddMonths(-3); 4 return newCustomer && customer.PurchaseHistory.Count > 5; 5} 6 7public bool IsEligibleForLoyaltyProgram(Customer customer) 8{ 9 bool newCustomer = customer.SignUpDate > DateTime.Now.AddMonths(-3); 10 return newCustomer || customer.LoyaltyLevel > 3; 11}
The variable newCustomer
is used in multiple places, causing duplicated logic.
Let's refactor by extracting the logic into a method, reducing duplication and enhancing modularity:
C#1public bool IsEligibleForDiscount(Customer customer) 2{ 3 return IsNewCustomer(customer) && customer.PurchaseHistory.Count > 5; 4} 5 6public bool IsEligibleForLoyaltyProgram(Customer customer) 7{ 8 return IsNewCustomer(customer) || customer.LoyaltyLevel > 3; 9} 10 11private bool IsNewCustomer(Customer customer) 12{ 13 return customer.SignUpDate > DateTime.Now.AddMonths(-3); 14}
By creating the IsNewCustomer
method, we've simplified the code and made it more maintainable. 🚀
In this lesson, you learned about the DRY principle and strategies like Extracting Method, Extracting Variable, and Replace Temp with Query to eliminate code redundancy. These strategies help to create code that is easier to maintain, enhance, and understand. Next, you'll have the opportunity to apply these concepts in practical exercises, strengthening your ability to refactor code and uphold clean coding standards. Happy coding! 😊