Open-Closed Workshop

0
Language

Level

Beginner

Access

Paid

Certificate

Paid

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.

Add your review

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 the Query, 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 our Query class, and extract it to a service provider. With this tweak, you’ll never have to modify the Query class again. Instead, create a new featured collection, add a new item to your service provider’s $collections array, 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?”

User Reviews

0.0 out of 5
0
0
0
0
0
Write a review

There are no reviews yet.

Be the first to review “Open-Closed Workshop”

×

    Your Email (required)

    Report this page
    LiveTalent.org
    Logo
    LiveTalent.org
    Privacy Overview

    This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.