A soccer midfielder has a job to do—not to play goalie, coach the team, and act as the physio too. The single responsibility principle (SRP) applies the same idea to software: a class should have one well-defined responsibility and one reason to change. section 3.4.1
A reason to change usually comes from a distinct requirement or area of concern. Sales-tax rules, database storage, and confirmation-email formatting can change independently, so putting them in one class tangles their work. A database change may then require retesting unrelated business logic and formatting. section 3.4.1
From jack-of-all-trades to focused classes
A Player class that handles player actions, tactical decisions, and database saving has several reasons to change. Split those jobs among Player, TacticsEngine, and PlayerRepository so each class has a focused purpose. section 3.4.1
Listing 3.4 “After”: Following SRP
// Responsibility 1: Manages only the player’s state and actions
class Player
{
constructor(name) { this.name = name; }
dribbleBall() { /* … */ }
}
// Responsibility 2: Manages only tactical decisions
class TacticsEngine { determinesBestPosition(player) { /* … */ } }
// Responsibility 3: Manages only data saving
class PlayerRepository { saveStats(player) { /* … */ } }Compare the three focused classes and the job assigned to each. section 3.4.1
SRP is a direct application of separation of concerns at class level and aims for high cohesion: the parts of a class work toward one purpose. The same thinking can guide a single function or a larger component, although the tactics differ with scale. section 3.4.1 section 3.7
A practical check when code feels crowded:
- Describe what the unit does. If the description needs several “and”s, look for distinct jobs. section 2.4.2
- Ask which requirements could change independently, then group related work around a clear purpose. section 3.4.1
- Separate the jobs and, where needed, give a coordinator the focused job of directing the workflow. section 3.5.3
Listing 3.15 “After” High-Level Coordinator Class
// This high-level class coordinates the work.
class OrderService {
// Dependencies are "injected" through the constructor
constructor(validator, paymentService, inventoryManager,
notificationService) {
this.validator = validator;
this.paymentService = paymentService;
this.inventoryManager = inventoryManager;
this.notificationService = notificationService;
}
processOrder(order) {
this.validator.validate(order);
if (this.paymentService.processPayment(order)) {
this.inventoryManager.updateInventory(order);
this.notificationService.sendConfirmationEmail(order);
return "Order processed successfully.";
} else {
return "Payment failed.";
}
}
}This order coordinator delegates validation, payment, inventory, and notification work to separate services. section 3.5.3
Focused code is easier to understand, test, and change, but separation does not solve every maintenance problem. For example, extracting a hard-coded tax rate into a named constant makes it easier to find, yet changing it still requires a deployment. Separating logging through an injected dependency also adds some setup complexity in exchange for easier substitution and testing. section 2.4.2 section 4.5.3







