Solid principles: 1. Single responsibility principle

The principle of single responsibility says that each class should be responsible for one specific thing, there should be only one reason to modify the class.

This is the most important SOLID principle, thanks to it we will not have classes of monsters after 2000 lines😐, which will be responsible for everything, but we will have many small classes responsible for different parts of the program.

Large classes are always harder to maintain, develop and refactorize, building large classes always sooner or later leads to problems such that if our project will be a class and will be responsible for everything in the future expansion of this class, changing its functionality will also affect completely different areas of this class, which in large projects🖥 leads to the fact that they are simply impossible to expand and modify.

However, by creating many small classes, you increase readability, ease of expansion and, generally speaking, ease of managing the project code.

Let’s move to practical examples, an example of a class that breaks the principle of a single responsibility is the one given in the introduction:

What we have here… Products class, which is responsible for everything, for placing orders, customers, calculating products, adding products, number of variables at the top of the class also does not look inviting, I forgot to mention that the single responsibility principle applies not only to classes, but also to methods, so that one or several methods do not do everything, you also have to try to do the shortest methods, preferably only perform one operation.

And we have here a few methods that do everything. If we were to make a practical project, we could not leave ✋ it that way, it should look like this:

And whether it does not look better? We have specific classes and methods that are responsible for a specific part of the store’s logic, the code has become much more readable, easier to understand and modify, we also the names of classes and methods have been improved. Of course, you can probably improve something in every code, no matter how well it would be written, how easy it would be to read, if all the rules of programming were met, not only SOLID, you can always improve something.

But you will admit that this is a much better solution than the one in which the Single Responsibility principle does not apply.

And below the call in the Main function:




In the next lesson, as you probably guessing, there will be discuss SOLID principle, open-closed.

Link to github with the whole code from this article:

This content also you can find on my steemit blog

And on medium:

As a standard, I remind you about the newsletter, which I send notifications about new entries and additional information about the IT world in general.🙂

And NECESSERILY join the DevmanCommunity community on fb, part of the community is in one place 🙂

– site on fb:ławomir Kowalski

– group on fb: DevmanCommunity

Ask, comment underneath at the end of the post, share it, rate it, whatever you want🙂.

If that post was useful for you share it with your friends :)

Post a comment

Notify about