Wednesday, 6 December 2017

React makes OCP so easy it's a joke

Composing components in React means that following the Open Closed Principle (the O in SOLID) is so easy that it's laughable.

By composing, or decomposing, React components together new functionality can be added and functionality can be removed without making any changes to the underlying components. Want to make sure that you dont show a profile page unless someone has been logged in? Wrap your profile page in a
RedirectToLoginIfUnauthorised
component, that checks for the user and redirects to login if they have not yet logged in.

As a bonus when you split and compose your functionality like this you end up with smaller components, that do less. So by following the Open Closed Principle in this way you get the Single Responsibility Principle for free.

Clean Code keeps on delivering.

No comments:

Post a Comment