Open /Closed Principles
“Software entities should be open for
extension but closed for modification.”
Open to Extension
• When we write a software component it should be open for future
extension.
• A class can be considered to be open if its functionality can be
enhanced by sub-classing it.
• Stick to the current behaviours
• Keep in mind that behaviour could always change and evolve
• We should anticipate what could change and what could be extended
But Closed for Modifications…
• A class or module is said to be closed, if it’s available for use by other
modules without modifying it.
• Basic requirements for class to be closed is it’s fields and functions
should be finalized and they cannot change.
• The implementation of any software entity should be closed for
modification.
• A class can be considered to be closed if its runtime or compiled class is
available for use as a base class which can be extended by child classes.
• If the behaviour changes , we should not change how a specific entity
work, we just need to extend it.
What is Refactoring ?
• It is the process of restructuring code , while not changing its original
functionality. The goal of refactoring is to improve internal code by
making many small changes without altering the code’s external
behaviour.
• Computer programmers and software developers refactor code to
improve the design, structure and implementation of software.
Refactoring improves code readability and reduces complexities.
Refactoring can also help Software developers find bugs or
vulnerabilities hidden in their software.
Need and Significance of Refactoring :-
• Makes the code easier to understand and read because the goal is to
simplify code and reduce complexities.
• Improves maintainability and makes it easier to spot bugs or make
further changes.
• Encourages a more in-depth understanding of code. Developers have
to think further about how their code will mix with code already in the
code base.
• Focus remains only on functionality. Not changing the code's original
functionality ensures the original project does not lose scope.