Single Responsibility Principle: Why Each Class Should Do Only One Thing
The Single Responsibility Principle (SRP) is a foundational software design concept stating that a class should have only one reason to change. Classes that handle multiple responsibilities — such as validation, database access, notifications, and logging all at once — are known as 'God Classes' and are considered an anti-pattern. A useful analogy compares a Swiss Army knife, which is versatile but fragile as a whole, to a scalpel, which is purpose-built and easier to maintain. In practice, SRP encourages splitting responsibilities into dedicated classes, such as separating UserValidator, UserRepository, EmailService, and Logger into distinct units. This principle aligns closely with layered architectures like MVC, Hexagonal, and Clean Architecture, where each layer owns a clearly defined responsibility.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in