liveBook · Manning

Separation of concerns

As explained in Grokking Software Architecture

Separation of concerns is a design principle that reduces complexity by dividing a system into distinct parts with separate responsibilities and boundaries.

A codebase can become a junk drawer: change a button label, and payment logic breaks. Separation of concerns (SoC) aims for a toolbox instead. Divide a system into distinct parts, each with a well-defined responsibility and a boundary that says what it should—and should not—do. section 3.2 section 1.5.4

The parts still work together. For an article-favorite button, the UI handles the click and display state, coordinating logic checks the user and requests an update, and the data part stores the user–article relationship. Keeping those jobs apart makes the system easier to navigate, test, and change. section 1.5.4 section 2.3

Untangling order processing

A single order-processing method mixes validation, payment, inventory, and email. A clearer design gives each job a focused service and uses an OrderService to coordinate them. Splitting the method into private helpers is quicker and improves readability, but leaves the core responsibility and testability problems in place. Separate services take more upfront work. section 3.5.2 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.";
        }
    }
}

See how the coordinator receives its services and directs the order workflow without taking over their individual jobs. section 3.5.3

The same idea works at different scales: a focused method, a cohesive class, or a system of separate components. Layered architecture applies it across an application. Cross-cutting concerns need boundaries too: logging can be an injected dependency rather than a job embedded in business logic. section 3.7 section 4.10 section 4.5.3

Separation also helps when the network misbehaves. Keep the business job of fetching data apart from retries and backoff; resilience libraries can wrap fragile calls without putting retry loops throughout business logic. A direct payment API call tangled into checkout makes testing dependent on the network and makes changing providers more disruptive. section 10.4 section 10.4.1

Try a quick boundary check:

  • Name the one job of a method or class. If its description needs “and,” look for responsibilities that could be separated. section 3.2
  • Sketch the responsibilities as boxes and identify who coordinates them before choosing an implementation. section 3.5.2
  • Ask how you would unit-test each part and where a likely change would land. Complicated tests or unrelated changes are clues to tangled boundaries. section 3.4.6

How Grokking Software Architecture covers it

How deep it goes
Defines responsibility boundaries, provides design checks, and walks through a refactoring with alternative approaches and tradeoffs.
Examples
Uses a favorite button, an order-processing refactor, logging, and a fragile payment API call to illustrate boundaries.
What you can do after
Apply separation of concerns by sketching responsibilities, checking boundaries, and comparing refactoring options for maintainability and testability.
How it connects
Connects separation of concerns to cohesion, coupling, SOLID, layered architecture, and dependency injection.

Also explained in

Compare how 25 books explain separation of concerns →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.