Upskill tutorial for DRY principles

Hud Wahab
4 min readJul 4, 2023

--

Day 4: Dispatcher functions. Constant variables.

Hi 👋 I am Hud, a postdoc for engineering data science at the AI Manufacturing Center in Laramie, Wyoming. My funding is running out (AaAaaA !), so while I am actively looking for a new job, instead of doing the 205th coding certificate to prove my worthiness — I thought I’d do design challenges and document how I spend my time upskilling so other engineers can do the same.

Nowadays, certificates are everywhere. Documenting small upskill projects that you can later show off is the best way to get recognition as a professional engineer.

This is day 4 of a 30-day design challenge. Follow along and let me know if you get stuck!

DRY debrief

The Don’t Repeat Yourself (DRY) principle is a coding principle that promotes code reusability and maintainability. It suggests that when writing code, developers should avoid duplicating logic or functionality in multiple places. Instead, they should strive to create reusable code elements that can be called and reused throughout the codebase.

The DRY principle encourages modular and efficient code design by abstracting common functionality into reusable components. This reduces redundancy, makes the code easier to understand, and simplifies future changes and updates. By following the DRY principle, developers can save time and effort in the long run, as they only need to modify a single instance of the code to update the behavior across the application.

DRY is closely related to other software engineering principles like abstraction, encapsulation, and modularity. It helps improve code quality, readability, and maintainability, making it an essential principle for creating efficient and scalable software systems.

TL;DR Decoupling challenge

Download the provided code for the challenge.

Task 1: Identify code repetition

  • Analyze the provided codebase and understand its structure and functionality.
  • Look for patterns or sections of code that are repeated in multiple places.
  • Identify at least three instances where code repetition occurs.

Task 2: Refactor the code

  • Determine the reusable code elements that can be created to eliminate repetition.
  • Create functions or modules that encapsulate the duplicated logic or functionality.
  • Modify the existing code to call and utilize these reusable elements instead of repeating the code.

Task 3: Test the refactored code

  • Verify that the refactored code produces the same results as the original code.
  • Conduct thorough testing to ensure the changes did not introduce any new bugs or issues.
  • Validate that the refactored code is more maintainable and adheres to the DRY principle.

Task 4: Document the changes

  • Document the areas of code where repetition was identified and the corresponding refactorings made.
  • Describe the new reusable code elements and how they are used in place of the repetitive code.
  • Explain the benefits of the refactored code, such as improved readability, maintainability, and reduced redundancy.

Identify repeats

Let’s have a look at the original code. The functions calculate_total_price and calculate_discounted_price both calculate the total price of a list of Item objects, but they do so independently of each other. This results in duplicated code, which violates the DRY principle.

The generate_order_confirmation_email and generate_order_shipping_notification functions also have duplicated code in terms of the email body, which could be extracted into a separate function to avoid duplication.

The same goes for the process_online_order and process_in_store_order functions — they share a lot of similar logic, but have some minor differences.

Solutions

Reuse functions!

By defining this function, we can avoid duplicating the logic for calculating the total price in multiple places. Instead, any code that needs to calculate the total price can simply call the calculate_total_price function, which promotes code reuse and makes the code more maintainable.

Remove hardcoding

Sometimes it’s not trivial to remove all duplication. Often a good designer has to learn how to balance the trade-off between simplification and the time taken to implement the DRY principle. Don’t be a DRY principle maximalist! We can see the example below:

By extracting the SENDER variable into a separate constant, the generate_order_confirmation_email and generate_order_shipping_notification functions remove duplication by avoiding the need to repeat the sender email address in multiple places. Instead, both functions can simply reference the SENDER constant, which promotes code reuse and makes the code more maintainable.

This also makes it easier to update the sender email address in the future, as it only needs to be changed in one place (the SENDER constant) rather than in multiple places throughout the code.

Create a dispatcher function

We can improve the DRY principle by defining a single process_order function that handles both online and in-store orders. This function takes in an Order object and delegates the processing to either process_online_order or process_in_store_order based on the order type -this is known as the dispatcher function.

By defining this function, the code avoids duplicating the logic for processing orders in multiple places. Instead, any code that needs to process an order can simply call the process_order function, which promotes code reuse and makes the code more maintainable.

Additionally, the process_online_order and process_in_store_order functions themselves are relatively simple and do not contain any duplicated logic, which further improves the DRY principle.

Conclusion

Congratulations! You finished Day 4 from the 30-day design challenge.

If you have reached this far, you know how to:

  • Simple reuse functions for de-duplication
  • Balance the trade-offs in de-duplication and effort taken
  • Extract and separate variable constants
  • Create dispatcher functions to delegate other functions

Check out the day 5 challenge!

Also, you can access the full 30-day GitHub repository here.

💡 My goal here is to help engineering data scientists upskill in design. I’d like to hear from you! Was this helpful? Anything I can improve? Connect with me on LinkedIn | Medium

--

--

Hud Wahab

🤖 Senior ML Engineer | Helping machine learning engineers design and productionize ML systems. | Let's connect: https://rb.gy/vb6au