2.2.9Design Principles

Separation of concerns

2,018 words9 min readdifficulty · medium

WHY does this principle exist?

A concern is any distinct aspect of a problem you care about — e.g. "how data is stored", "how prices are calculated", "how the screen looks". The opposite of separation is tangling (one module doing many concerns) and scattering (one concern smeared across many modules).


WHAT exactly are we separating?

Two measurable forces tell you whether you've succeeded:

Why these two together? Because you can cheat each one alone:

  • Put everything in one giant function → coupling between modules is zero (there's one module!) but cohesion is terrible.
  • Split into 50 tiny modules that all call each other → cohesion looks high but coupling explodes. SoC asks you to win both by cutting along the natural seams of concerns.

Figure — Separation of concerns

HOW do you actually separate concerns?

A practical 3-step recipe:

  1. List the reasons to change. Ask: "Who would ask me to edit this, and why?" Each distinct answer is a concern. (This is literally the Single Responsibility Principle in disguise.)
  2. Group by reason, not by data. Code that changes together lives together; code that changes for different reasons gets split apart.
  3. Insert a boundary (a function, class, module, or layer) so the parts talk through a narrow interface, hiding their internals.

Common mistakes (steel-manned)


Active recall

Recall What is a "concern", and what's the cleanest test for it?

A concern = one reason to change. Test: "Who asks me to edit this, and why?" Each distinct answer is a separate concern.

Recall Why minimize coupling AND maximize cohesion, not just one?

Optimizing either alone is gameable: one giant module = zero module-coupling but no cohesion; many tiny chatty modules = "high cohesion" but exploding coupling. SoC wins both by cutting along natural concern boundaries.

Recall What is a cross-cutting concern and how is it separated?

A concern (logging, auth, error handling) that touches many modules. Separated via decorators/middleware/aspects so business logic stays clean.

Recall (Feynman, age 12) Explain it simply.

Imagine a kitchen where one person chops, cooks, plates, and washes dishes all at once — when the recipe changes, they get confused and burn the food. Separation of concerns is giving each job to a different person: the chopper just chops, the cook just cooks. Now if you change the dish, only the cook learns something new — everyone else keeps doing their own simple job. Software is the same: give each piece of code one job so changing one thing doesn't break everything.


Flashcards

Separation of Concerns means each module handles
one concern (one reason to change), with minimal overlap with others.
A "concern" is best defined as
a single reason to change / one cohesive responsibility.
Two metrics that indicate good SoC
low coupling and high cohesion.
Coupling measures
how many other modules a module depends on (want it LOW).
Cohesion measures
how focused a module's elements are on one concern (want it HIGH).
Why must we optimize coupling AND cohesion together
each is gameable alone; SoC achieves both by cutting along natural concern boundaries.
Tangling (anti-pattern) means
one module mixing several concerns.
Scattering (anti-pattern) means
one concern spread across many modules.
A cross-cutting concern is
a concern (logging, security, error handling) that spans many modules.
Cross-cutting concerns are separated using
decorators, middleware, or aspect-oriented programming.
In layered architecture, dependencies should point
in one direction (presentation → domain → data).
Quickest practical test for good separation
a typical change should require editing ideally one place.
SoC relates to SRP because
SRP applies SoC at the class level (one class = one reason to change).
"One thing" in SoC means
one reason to change, NOT one line/one tiny function.

Connections

  • Single Responsibility Principle — SoC applied to a single class/module.
  • Coupling and Cohesion — the measurable outcome of good SoC.
  • Layered Architecture — SoC realized as horizontal layers.
  • Cross-cutting concerns — concerns that resist simple layering.
  • Modularity — the broader goal SoC serves.
  • Don't Repeat Yourself (DRY) — pairs with SoC; each concern has one home.
  • Information Hiding — the boundary that makes separation hold.

Concept Map

motivates

splits program by

fights

fights

aims for

aims for

measured as

measured as

equivalent to

applied via

inserts

enables

Human limited memory

Separation of Concerns

Concern one reason to change

Tangling one module many concerns

Scattering one concern many modules

Low coupling

High cohesion

Measured result

Single Responsibility Principle

3-step recipe

Narrow interface boundary

Change stays local

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Separation of Concerns ka matlab simple hai: program ko aise tukdo me todo ki har tukda sirf ek kaam kare. "Concern" yaani ek alag wajah jiski wajah se code badalna pad sakta hai — jaise data store karna, business calculation karna, aur screen pe dikhana. Agar yeh teeno cheezein ek hi function me ghusa di, to ek bhi change karne pe sab kuch tootne ka risk rehta hai. Isliye humein inhe alag-alag "rooms" (modules) me rakhna chahiye.

Iska asli faayda yeh hai ki change local ho jaata hai. Maan lo tax rate badal gaya — agar cart_total() alag function hai, to bas wahi edit karoge, baaki kuch chhune ki zaroorat nahi. Database badalna ho to sirf fetch_cart(). UI web pe le jaana ho to sirf render_total(). Humans ka dimaag ek time pe thodi cheezein hi pakad sakta hai, isliye chhote focused tukde banana zaroori hai — yahi to programming me sabse bada headache solve karta hai.

Do cheezein yaad rakho jo batati hain ki separation sahi hua ya nahi: coupling kam (ek module dusre pe kam depend kare) aur cohesion zyada (ek module ke andar sab cheezein ek hi kaam ke liye ho). Sirf files badha dene se separation nahi hoti — test yeh hai: "ek typical change ke liye kitni jagah edit karni padti hai?" Agar ek hi jagah, to badhiya separation hai.

Aur ek cheez — cross-cutting concerns jaise logging, security, error handling har jagah aate hain. Inhe decorators ya middleware se alag rakhte hain taaki business logic saaf rahe. Mantra simple hai: "One reason, one room."

Go deeper — visual, from zero

Test yourself — Design Principles

Connections