Open-Closed Workshop
In this workshop, we'll focus exclusively on the open-closed principle (the O in SOLID). Using real-life code, line by line, we'll refactor a confusing class into something far more manageable and extendable. Once finished, we'll have gained the luxury of introducing new functionality, not by modifying existing code, but by instead writing a new class.
Progress
Series Info
- Episodes
- 5
- Run Time
- 48m
- Difficulty
- Advanced
- Last Updated
- Sep 22, 2020
- Version
- Latest
Series Episodes
- Episodes (5)
What We Are Refactoring
In order to provide a practical, real-life example of the open-closed principle, we'll use a heavily-modified version of existing code from the Laracasts codebase, itself. This should open up all sorts of refactoring possibilities. Now before we get started, let's first take a few minutes to discuss what we're working with, and where we want to go.Low-Hanging Refactors
Let's begin our refactoring by applying the squint test. If we squint our eyes and scan theQuery, what immediately jumps out at us? Indentation? Common method names? Multiple return types? Let's see!Remove Conditionals and Embrace the Pipeline
Let's keep going. In this episode, I'll show you how to remove a conditional by reassigning the "check" to the underlying class.Extract What Changes into a Service Provider
Let's take the only remaining thing that changes in ourQueryclass, and extract it to a service provider. With this tweak, you'll never have to modify theQueryclass again. Instead, create a new featured collection, add a new item to your service provider's$collectionsarray, and you're done.Discussing Responsibility
As we reach the end of this series, let's take a few moments to discuss responsibility and awareness. I find that it's useful to frequently ask myself, "Should this class be aware of that particular thing?"
