It was all rainbows and chocolate until I wanted the objects I created to share features and to reduce redundancy in the code I wrote. I started writi

Key Principle to writing better Javascript Classes and modules

submited by
Style Pass
2024-06-11 13:00:07

It was all rainbows and chocolate until I wanted the objects I created to share features and to reduce redundancy in the code I wrote. I started writing classes for this and I was happy per say. Some redundancy still remained until I read about the second letter of the SOLID principle, Open-closed principle

If you have been writing JavaScript for more than 3 days then you have used this principle before. Shocking right? Well, I guess that’s the cool thing about JavaScript. My favorite definition of this principle is by Bertrand Meyer.

If a user wants to change how a certain method is implemented in the parent class then they should simply override the method.

My chicken has these two behaviors but how my chicken does these two behaviors is different. Firstly, it doesn’t hop and it doesn’t chew very loudly.

Well by tweaking my animal class to match these chicken characteristics then all should be fine. But this is not the best approach because what if I want to add another animal let’s say a puppy? we cannot have our puppy show the characteristics of a chicken.

Leave a Comment