2. Single Responsibility Principle (SRP) Flashcards
What are the SOLID principles comprised of?
Five individual principles for writing better software, especially in object-oriented languages.
What does the SRP state?
Each software module should have one and only one reason to change.
What is a module in the context of a C# program?
A module in C# might refer to a class or even a single function.
How can we improve the design of our software when it comes to the proximity between what the application does and how it does it, and how?
By separating the “what” from the “how”. We do this through delegation and encapsulation.
How can we improve the design of our software when it comes to the proximity between what the application does and how it does it, and how?
By separating the “what” from the “how”. We do this through delegation and encapsulation.
What should the classes encapsulate?
ps. Encapsulation in the general sense.
Doing a particular task in a particular way.
What can be said about single-purpose classes?
They do a single particular task in a particular way, and they are easy to use.
What can be said about the performance of multipurpose tools vs dedicated tools?
Multipurpose tools don’t perform as well as dedicated tools.
What can be said about the performance of multipurpose tools vs dedicated tools?
Multipurpose tools don’t perform as well as dedicated tools.
What is a responsibility in the context of the SRP?
It’s the answer to the question of how something is done.
It’s a decision our code is making about the specific implementation details of some part of what the application does.
What are some examples of responsibilities?
- Persistence
- Logging
- Validation
- Business Logic
What do responsibilities in our code represent in regards to change and the SRP?
Responsibilities represent things that may change at different times and for different reasons — each responsibility is a different axis of change.
What happens when two or more details are intermixed in the same class?
It introduces tight coupling between these details.
What is tight coupling?
Binding two or more details together in a way that’s difficult to change.
What’s the difficulty that tight coupling brings?
If the tightly coupled details change at different times for different reasons, it’s likely to cause problems in the future with code churn in the class in question.
What’s the difficulty that tight coupling brings?
If the tightly coupled details change at different times for different reasons, it’s likely to cause problems in the future with code churn in the class in question.
What does loose coupling offer?
A modular way to choose which details are involved in a particular operation.
What is a typical example of loose coupling?
One class being responsible for some high-level concern and delegating to other classes who are responsible for the details of how to perform low-level operations.
What is a typical example of loose coupling?
One class being responsible for some high-level concern and delegating to other classes who are responsible for the details of how to perform low-level operations.
What does the seperation of concerns state?
That programs should be separated into distinct sections, each addressing a separate concern or set of information that affects the program.
What can be thought frequently of as low-level plumbing code?
The specific implementation details of how a program does something.
What is the key benefit of following the separation of concerns?
That high-level business logic avoids having to deal with low-level code.
What should ideally be the case in regards to high-level and low-level code’s knowledge of one another?
High-level code should not know about how low-level implementation details are implemented, nor should it be tightly coupled to the specific details.
What does cohesion describe?
How closely related the elements of a class or module are to one another.