Open / Close Principle (OCP)
Definition :
"A class should be open to extension and closed to modification"
Another statement we can make is :
Let's have an exemple :
look at the following bad exemple of code :
<button class="button">Action Button</button>
<div class="context">
<button class="button">Action Button</button>
</div>.button {
/* ... button's styles */
color: green;
}
.context {
/* ... context's styles */
}
.context .button {
color: blue;
margin-left: 16px;
}There is multiple mistakes here regarding either the OCP and the SRP (open/close and single responsibility) :
we modify the color of the button depending on his context.
we modify the
.buttonclass by adding a propertywe add to the button the responsibility of the margin
To follow both the OCP and the SRP, we could have done :
<button class="button">Action Button</button>
<div class="context">
<button class="
context__action
button button--blue
">
Action Button
</button>
</div>.button {
/* ... button's styles */
color: green;
}
.button--blue {
color: green;
}
.context {
/* ... context's styles */
}
.context__action {
margin-left: 16px;
}Note that in the previous exemple, we used the BEM naming convention, more detailed informations about BEM will follow.
Now you can see that we extended the .button class with a .button--blue option, or more precisely a modifier (to follow the BEM convention).
this modifier allow us to use the blue variation anywhere we see fit in the code source.
Secondly, instead of adding the margin directly to the .button trough an override, we created a class attached to the .context class called .context__action that is responsible for creating the required white space.
In the real world, both button and context are being instantiated in different places (even different files)
This approach has multiple benefits.
By not modifying button at all from context, we keep the component decoupled from one another. Deleting the
buttoncss file will delete all instances of thebuttonclass of the code baseWe are not tied to structure (using a
buttonwith.context__action, it could be for exemple, a link instead)It's more flexible and safe, because for instance, if we need to add another
buttonintocontext, we maybe don't want it to have amargin-leftin this specific use-case
Last updated
Was this helpful?