Back to Academy
coding principles
Single Responsibility Principle
The Single Responsibility Principle (SRP) states that a class or module should have one, and only one, reason to change. Each unit of code should encapsulate a single piece of functionality. When a class handles multiple concerns — such as data access, business logic, and formatting — a change in any one of those areas forces the class to be modified, increasing the risk of bugs. By separating responsibilities, code becomes easier to test, maintain, and extend independently.
Use Cases
- •Splitting a monolithic service class into focused helpers (e.g., separate validation, persistence, and notification logic)
- •Keeping React components focused on rendering while extracting data fetching into hooks or services
- •Designing microservices where each service owns a single bounded context
- •Creating utility modules that each handle one cross-cutting concern (logging, auth, caching)
Visualization
Implementation
Output
Click "Run Code" to see output...