Single Responsibility Principle

Taylan Can Hardal
2 min readFeb 13, 2022

--

Hey everybody it is first principle of SOLID Principles. This principle means that a class or method should only be responsible for one job. Do one job and do it best.

Let’s say there is a class for UserRegister, this class should only be about registering transactions. Shouldn’t use it to control transactions or any different processes. It should only be used for registration processes.

Why are these principles important?

This principles are not rule. The developers use for clean code. Coding can be done without this principles but diffucult to read and debug. On the other hand, developers who have just joined the team can adapt to the project more easily.

Example Project

In order to learn in detail, the correct usage and the wrong usage should be examined together.

Wrong Usage :

It has a class called AddUser. This class is architected to add a new User, but there is a different function inside the class. This function checks User datas. This situation does not comply with the Single Responsibility Principle.

Now let’s make this code suitable for Single Responsibility.

Correct Usage :

AddUser class splited two different classes. Which names are AddUser and UserValidator classes. In this way, the class has been fitted for Single Responsibility Principle.

You can continue reading here :

Referances :

bolŞans…

--

--

Taylan Can Hardal
Taylan Can Hardal

Written by Taylan Can Hardal

Software Engineer @Turkcell. Savin’ the Earth. Somewhere

No responses yet