Single Responsibility Principle Flashcards
(24 cards)
What is the Single Responsibility Principle (SRP)?
The Single Responsibility Principle states that a class should have only one reason to change, meaning it should only have one job or responsibility.
True or False: According to SRP, a class can have multiple responsibilities.
False
Fill in the blank: SRP is one of the five principles of ________.
SOLID
What are the benefits of adhering to the Single Responsibility Principle?
Increased maintainability, easier testing, and better separation of concerns.
Which of the following is NOT a consequence of violating SRP? A) Increased complexity B) Easier debugging C) Difficulties in testing
B) Easier debugging
Short Answer: How does SRP relate to code maintainability?
SRP improves code maintainability by ensuring that changes to one responsibility do not affect other parts of the code.
What is a common sign that a class violates SRP?
If a class has multiple methods that handle unrelated tasks or functionalities.
Multiple Choice: Which of the following examples violates SRP? A) A class that handles user authentication B) A class that manages user data and sends notifications
B) A class that manages user data and sends notifications
True or False: SRP applies only to classes and not to functions or modules.
False
Fill in the blank: Classes that follow SRP are typically easier to ________.
test
What is the relationship between SRP and refactoring?
Refactoring often involves breaking down classes that violate SRP into smaller, more focused classes.
Multiple Choice: Which principle complements SRP by promoting loose coupling? A) Open/Closed Principle B) Interface Segregation Principle C) Dependency Inversion Principle
B) Interface Segregation Principle
What does it mean for a class to have a ‘single reason to change’?
It means that any change in requirements should only necessitate changes in one class, not multiple classes.
True or False: SRP can lead to increased code duplication.
True
Short Answer: How can SRP be implemented in a project?
By analyzing class responsibilities and ensuring each class focuses on a single task or functionality.
What is a practical example of SRP in software design?
A class that handles database operations should not also handle user interface logic.
Multiple Choice: Which of the following best describes a class adhering to SRP? A) A class managing user input and database queries B) A class only managing user input
B) A class only managing user input
Fill in the blank: SRP encourages developers to create ________ classes.
smaller
What is the impact of SRP on unit testing?
SRP makes unit testing easier because each class can be tested in isolation.
True or False: SRP is only relevant in object-oriented programming.
False
Short Answer: Name one technique to identify violations of SRP in your code.
Code reviews or static code analysis tools.
What is a common pitfall when applying SRP?
Over-engineering by creating too many small classes for simple functionalities.
Multiple Choice: When should SRP be considered during development? A) Only during the design phase B) Throughout the entire development process
B) Throughout the entire development process
Fill in the blank: The Single Responsibility Principle is primarily concerned with ________ in software design.
responsibility