Skip to main content

Open Close Principle and the Role of Agile

Open Close Principle

Open-Close principle stands for "Open for extension and Closed for Modification".

  • Once written, a piece of code should not be modified again (unless it's a bugFix of course) - closed for modification.
  • In Object Oriented Programming inherit from the base class to create a new extended class with additional features.
  • In react if we create a modal, buttons, cards, etc components, we should never need to change those components. If we need our modal look like card, we use card inside the modal, we put as many buttons where ever we need inside the modal as per the requirements.
  • If a particular configuration / arrangements of components get's re-used multiple places, we can define it as another high level component. Such as a popup with specific spacing and styling guidelines for a particular app can be created using a base Modal component. Thus the Popup component becomes the extended component of Modal, keeping the Modal component unchanged.
  • All the components, utilities, helpers should follow this principle.

Role of Agile

Agile means the ability to move quickly and easily. It doesn't necessarily means to move faster, but rather jump from one feature to another or one requirement to another without much friction.

  • From the start, on the very first implementation, our code might not be perfect, which does not require any changes at all. (Closed for Modification). We need to improve the code in iterations. Sprints are for iterations.
  • Work should be split into smaller stories and tasks. Sprints should be short, spanning 1 week or 2 weeks.
  • Within a sprint we should be able to implement something small, or a small part of a requirement and ready to deploy. The feature might not be complete, that's OK, but it must be ready to deploy. Even the chronology of implementation should not matter, and everything should be cleanly decoupled. If we implement logout feature first, then in the next sprint login feature is planned, by the end of current sprint we should be able to deploy the logout feature.
  • And in next sprint when we start to work on the login feature, we realize that the logout code needs some changes! That's a sign, ideally the code should never require any modification to make it work with different implementations of login. So we should take a follow up task first to refactor the logout code. If the sprints are small, we must have implemented small things, which we can refactor in no-time.
  • The purpose of Agile is to let us know fast how bad our code is! The fact that our code written in previous sprint requires changing tells us that our code didn't follow open-close principle correctly! The faster we identify it, the better. Because once a heavy technical debt is accumulated, It'll get very difficult to fix!

Additional Learning