SOLID Design Principles: The Open-Closed Principle

Nisanur Sakacı
Orion Innovation techClub
3 min readFeb 24, 2022

I. Introduction

SOLID principles allow writing “clean code” in object-oriented programming. Solid principles enable faster editing of the developed project.

Solid principles require 5 basic rules.

· S stands for SRP (Single Responsibility Principle)

· O stands for OCP (Open Closed Principle)

· L stands for LSP (Liskov Substitution Principle)

· I stand for ISP (Interface Segregation Principle)

· D stands for DIP (Dependency Inversion Principle).

II. O: Open-Closed Principle (OCP)

The Open-Closed principle was developed by Bertrand Meyer. According to the Open-Closed principle, a program should be open to development and closed to change. In other words, new modules and new behavior patterns can be added to a program. New classes can be derived from the written code. But the program cannot be changed. The main code should always remain the same.

The Open-Closed Principle aims not to break the main code while fulfilling customer requests. It is against the Open-Closed principle to disrupt the flow of the program written while making optional additions.
Many problems can occur in codes written in a way that does not comply with the Open-Closed Principle. If changes are made as the add-ons are entered into the program, different unwanted parts may need to be changed in subsequent additions. In fact, the workload is much higher in complex large programs.

Considering a program that inherits as above, changes made in the main parts will affect the derived classes or functions. The workload will increase and the parts that need to be changed will increase. Classes that inherit from the modified program may be affected by this change. Inherited classes also need to be rearranged. In addition, this may cause spaghetti code to occur.

OCP can be explained with an example. For employees of a company, the accountant is asked to calculate the tax liability. In the program used, personal information is collected and tax is calculated. A flat tax of 25% is charged for male taxpayers and 20% for female taxpayers.
As a result of the new system change, tax reductions will be provided to elderly women. Company employees want to learn about their new tax obligations. The accountant wants to know the results according to the new regulation.
The program will first be written without using OCP.

Before OCP (Individual.cs)
Before OCP (TaxCalculator.cs)
Before OCP (Gender.cs)
Before OCP (CharteredAccountant.cs)

Errors may occur when making changes to the program without using OCP. In the tax deduction example, the tax deduction applied to elderly women was made by creating a new case in the TaxCalculator.cs class. Sometimes these changes may not be possible as a result of the code becoming complex. When OCP is applied, new additions can always be made and problems disappear.
The example can be edited using OCP as follows.

After OCP (Individual.cs)
After OCP (TaxCalculator.cs)
After OCP (Male.cs)
After OCP (Female.cs)
After OCP (SrCitizenFemale.cs)
After OCP (CharteredAccountant.cs)

--

--