Junior — Middle
How can you determine that a class violates the single responsibility principle?
sobes.tech AI
Answer from AI
A class violates the Single Responsibility Principle (SRP) if it performs multiple different tasks or is responsible for different aspects of logic. Signs of violation:
- The class contains methods related to different functional areas.
- Changing one functionality requires modifying the class, which may affect other parts.
- The class is difficult to test due to multiple unrelated responsibilities.
For example, if a class is responsible for both business process logic and database operations, this violates SRP. In such cases, it is advisable to split responsibilities into separate classes.