Welcome to the very first lesson of the "Applying Clean Code Principles in PHP" 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. 🚀
Understanding the Problem
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 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 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.
DRY Strategies
To adhere to the DRY principle and avoid repeating yourself, several strategies can be employed:
Extracting Method: Move repeated logic into a dedicated function that can be called whenever 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 function to compute values on demand rather than storing them in temporary variables, aiding in readability and reducing redundancy. Note: computing values on demand, instead of re-using previously computed variables, can have a slight performance impace. Most of the time this isn't an issue, but as always, weigh out the pros and cons!
Extracting Method
Extracting Method: Refactored
Extracting Variable
Extracting Variable: Refactored
Replace Temp with Query
Replace Temp with Query: Refactored
Summary and Preparation for Practice
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! 😊
Both functions 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 function, we can eliminate redundancy and streamline updates: