SRP at Different Levels
What you'll learn: How the Single Responsibility Principle applies not just to classes, but to functions, modules, and even entire services.
Beyond Classes
You've seen SRP applied to classes—each class should have one reason to change. But this powerful concept scales up and down throughout your software architecture.
SRP at the Function Level
Each function should do one thing well. A function named getUserData shouldn't also validate credentials, format output, and log analytics. It should retrieve user data—period. Other responsibilities belong in other functions.
Think of functions like kitchen tools: a whisk whisks, a peeler peels. A "multi-tool gadget" that tries to do everything usually does nothing well.
SRP at the Module Level
A module (a collection of related classes or functions) should have a single, focused purpose. A UserAuthentication module handles authentication concerns—not email sending, not data visualization, not billing logic.
Each module becomes a cohesive unit with one reason to change: when requirements around its specific domain evolve.
SRP at the Microservice Level
In distributed architectures, each service should own one business capability. A "User Service" manages user profiles. A separate "Payment Service" handles transactions. A "Notification Service" sends messages.
When payment logic changes, only the Payment Service needs updating—not a monolithic application where everything is tangled together.
The Pattern
At every level, SRP means:
- One clear purpose
- One reason to change
- High cohesion within
- Loose coupling between
Key Takeaway: The Single Responsibility Principle isn't just a class-level rule—it's a universal design principle that applies to functions (do one thing), modules (one domain), and services (one business capability), creating systems that are easier to understand, change, and maintain.