What is the principle of Single Responsibility in SOLID?
Many people work in different fields of programming, but what distinguishes a professional programmer from a mediocre or bad programmer is learning to design good code. Good and standard design in programming makes the process of changing the code easier due to the features it creates in the program. By doing so, the financial and time costs of changing the code and maintaining the program are greatly reducing.
The world we live in is a world of complexities, and the need to change and adapt to those changes is essential. This world is a cruel place for programmers who are afraid of making changes to their code or waste a lot of time on it! Therefore, it is better to learn the basics of good code design as soon as possible, to make our plans to make changes and features or new features simple, standard and clean.
Single Responsibility
Good code design helps us to easily cope with program changes and reduce the complexity of our programs and increase code readability. Some may think that complex code that no one understands is a sign of a smart and good programmer. No! You should know that a good programmer is not someone whose code can understand a computer, but a programmer which human can read code easily.
We used the term code design several times! You may be wondering what design has to do with programming! So to get acquainted with the term Single Responsibility, it is better to first get acquainted with the design and what it is. Design in programming is actually the process of coding, testing, and reproducing it. So with that in mind, a programmer is a software designer.
In short, to achieve a good design, we need the uniqueness and internal coherence of the modules (which are the same classes in object-oriented programming) as well as the avoidance of entanglement. You should know that Single Responsibility is the solution to reach the first condition. Single Responsibility is one of the five principles of SOLID (read solid) and the first of them. In SOLID, this principle is the Single Responsibility Principle, or SRP for short.
principle of Single Responsibility
In this principle, it is said that in order to achieve a good design, we need each class to play only one goal and task in the program. The work of all class methods must be in line with that goal, and using any method with tasks that deviate from the main goal is wrong, causing entanglement of tasks in the program and thus complexity of the code, thus reducing the reusability of the code. . Also, each class method must perform only one task.
Take smartphones, for example. These phones usually have many features such as cameras and camcorders, radio, voice recorder and …. But who can claim that a cell phone can do better than a professional camera and camcorder? Who uses their mobile phone to record podcasts or music tracks and ignores specialized recording devices?
In principle, Single Responsibility, for example, if we have an Authentication class, all class methods need to be user authentication. For example, we can have Login () or Logout () methods, but having methods like showPosts () to display user posts in this principle is wrong.
Conclusion
In this article, we got acquainted with the first of the important principles of SOLID in software design. The principle of Single Responsibility states that each module or program class should only work towards one goal, and that it is wrong to have methods that have marginal or irrelevant tasks. This rule also applies to methods. That is, each class method must have only one task. By observing this principle, we can reach the first sign of good design, which is the coherence of the program modules. Have you ever experienced a mistake in not following this principle? What problems did this mistake cause you? We are happy to read your comments!