Separation of Concerns

Keep different jobs, like fetching, validating, and displaying data, in separate places instead of tangled together.

What is it?

Picture one function that runs when a user submits a form. It checks the email, sends the data to the server, formats the response as HTML, and updates the page. It works.

Now you need to reuse the validation, or test it, or the designer wants a different success message. Each small request means wading through the whole function without breaking the other three things it does.

The fix is to give each responsibility, or concern, its own place: one piece validates the email, one sends the request, one updates the screen. This is separation of concerns. Each piece is small enough to read in one sitting, test alone, and reuse without dragging the others along.

Where you draw the lines varies by project. What matters is drawing them at all, so "validate this", "fetch that", and "display this" don't share the same block of code.

Explain like I'm 10

A restaurant kitchen has separate stations for chopping, grilling, and plating, not one cook doing everything at one counter. Each station has one job, so the kitchen can scale and one mistake doesn't ruin every dish.

Examples

Before: everything tangled together

function submitForm(email, password) {
  // validation
  if (!email.includes("@")) {
    alert("Invalid email");
    return;
  }

  // network call
  fetch("/api/signup", {
    method: "POST",
    body: JSON.stringify({ email, password }),
  }).then((res) => {
    // updating the UI
    document.getElementById("status").innerText = "Signed up!";
  });
}

Validation, the network request, and the UI update are mixed in one function. You can't test validation without also making a real network call.

After: each concern separated

function isValidEmail(email) {
  return email.includes("@");
}

function signup(email, password) {
  return fetch("/api/signup", {
    method: "POST",
    body: JSON.stringify({ email, password }),
  });
}

function showStatus(message) {
  document.getElementById("status").innerText = message;
}

async function submitForm(email, password) {
  if (!isValidEmail(email)) return showStatus("Invalid email");
  await signup(email, password);
  showStatus("Signed up!");
}

Now you can unit-test isValidEmail with no network, reuse signup from another form, and change how showStatus renders without touching the other two.

How it works

For each piece of logic, ask: "what is the one reason this code would change?"

  • Validation rules change when the business redefines what's valid.
  • Network logic changes when the API changes.
  • Display logic changes when the designer wants new wording.

When code with several reasons to change lives in one place, a change for one reason risks breaking the others. Split by concern, and each piece only changes for its own reason.

Why does it exist?

Tangled code is expensive in predictable ways:

  • Hard to test: you can't isolate one concern.
  • Hard to reuse: you'd copy the whole tangle to reuse one part.
  • Hard to change safely: touching one concern can break its neighbor.

Separating concerns costs a little organization up front and greatly lowers the cost of every later change.

When to use it

Apply it once code does more than one distinct job, especially the classic trio: getting data, validating or transforming it, and displaying it. It pays off most in code you'll test, reuse, or revisit.

When not to use it

For a tiny one-off, like a five-line script you'll run once, splitting it up can cost more than the code itself. If the code won't be read, tested, or changed again, some tangling is harmless.

Common mistakes

  • Splitting code into more functions without separating concerns, e.g., three functions that each still mix validation and network calls.

  • Over-separating trivial code into a maze of tiny functions that's harder to follow than the original.

  • Leaving business logic in the 'coordinator' function instead of having it only delegate to the separated pieces.

Practice exercises

  1. Easy:

    Split a function that validates a phone number and formats it for display into two separate functions.

  2. Medium:

    Refactor a function that fetches users from an API, filters out inactive ones, and renders them to the DOM into three separate pieces.

  3. Hard:

    A 40-line function reads a file, parses CSV rows, validates each row, and writes valid rows to a database. Redesign it into separate concerns and explain what you'd unit test in each piece.

Interview questions

What is separation of concerns?

Keeping a program's different responsibilities, such as data access, business rules, and presentation, in separate, independent parts of the code instead of mixed together.

Why does mixing concerns together make code harder to test?

You can't run one concern (like a validation rule) alone. Running it also triggers whatever it's tangled with, like a network call or a UI update.

How do you decide where to draw the line between two concerns?

Ask whether they'd change for different reasons. If validation rules and network error handling change independently, they're different concerns and belong in different places.