Coupling & Cohesion
How much pieces of code depend on each other (coupling) versus how well one piece focuses on one clear job (cohesion).
What is it?
Two different questions help you judge whether code is well organized.
First: if you change this module, how many other modules break or need changes? If many do, they're tightly coupled. They know too much about each other's internals, so a ripple in one becomes a wave everywhere. If changes rarely spread, they're loosely coupled. That's what you want: you can change one part and know what won't break.
Second: does this module do one related job, or is it a grab-bag of logic stuffed together for convenience? If everything inside serves one purpose, it has high cohesion. A "utils.js" that formats dates, sends emails, and calculates taxes has low cohesion.
You want low coupling between modules (so they're independent) and high cohesion within each module (so each is focused and easy to understand). Loose on the outside, tight on the inside.
Explain like I'm 10
Think of company departments. High cohesion means accounting only does accounting, not IT support and marketing too. Low coupling means accounting can change its internal process without a meeting with every other department.
Examples
Tight coupling: two modules reaching into each other's internals
// orderService.js
export function createOrder(cart) {
const order = { items: cart.items, total: cart.items.reduce((s, i) => s + i.price, 0) };
db.orders.push(order); // reaches directly into a shared global array
return order;
}
// emailService.js
export function notifyCustomer(customer) {
const lastOrder = db.orders[db.orders.length - 1]; // assumes it was just created
send(customer.email, `Your order total is ${lastOrder.total}`);
}notifyCustomer silently depends on the order createOrder pushed to a shared array. Change how orders are stored, or call notifyCustomer before an order exists, and it breaks. The two modules are tightly and invisibly coupled.
Loose coupling: passing what's needed explicitly
// orderService.js
export function createOrder(cart) {
return { items: cart.items, total: cart.items.reduce((s, i) => s + i.price, 0) };
}
// emailService.js
export function notifyCustomer(customer, order) {
send(customer.email, `Your order total is ${order.total}`);
}
// calling code
const order = createOrder(cart);
notifyCustomer(customer, order);notifyCustomer no longer assumes anything about how or where orders are stored. It just needs an order passed in. Each function can now be changed, tested, or reused on its own.
How it works
Ask two questions of any module:
- Coupling check: "If I rewrote this module's internals but kept its inputs and outputs, would anything else need to change?" If yes, something is leaking: shared global state, a hidden assumption about call order, or a dependency on another module's private details. - Cohesion check: "Can I describe this module's job in one short sentence without 'and'?" If you need "and" ("validates users and sends emails and logs analytics"), it does too many unrelated things.
Why does it exist?
These are the two biggest predictors of how expensive a codebase is to change. High coupling means small changes cause wide, unpredictable breakage. Low cohesion means you can't find where a behavior lives, because related logic is scattered and unrelated logic is jammed together.
Naming them gives engineers a shared vocabulary to critique a design before it's built, not only after it causes pain.
When to use it
Use them as a lens when splitting code into modules, reviewing a pull request, or explaining why code feels hard to work with. If a small change keeps needing edits in unrelated files, that's almost always a coupling problem.
When not to use it
Zero coupling is neither possible nor useful: modules must call each other to form a working system. The goal is coupling through clear, stable, minimal interfaces, not through shared internals and hidden assumptions.
Common mistakes
Believing all coupling is bad. The real problem is coupling to internals instead of through a stable, well-defined interface.
Letting a 'utils' or 'helpers' file become a low-cohesion dumping ground for anything without an obvious home.
Reducing coupling by passing one giant shared object around 'just in case'. That just moves the tight coupling into the object's shape.
Practice exercises
- Easy:
Open a 'utils.js' or 'helpers.js' file from one of your projects. List its functions and note which are unrelated to each other. That's a cohesion problem.
- Medium:
Rewrite the tightly coupled order/email example above so notifyCustomer knows nothing about how orders are stored internally, beyond the fields it uses.
- Hard:
Describe a module you've seen with low cohesion (many unrelated jobs). Propose how you'd split it into two or more high-cohesion modules, and what interface each would expose.
Interview questions
What's the difference between coupling and cohesion?
Coupling measures how much modules depend on each other's internals; you want it low. Cohesion measures how focused one module's responsibilities are; you want it high.
Why is 'low coupling, high cohesion' considered good design?
Low coupling lets you change one module knowing unrelated modules won't break. High cohesion makes each module easy to understand, test, and find, because it does one related job.
Give an example of tight coupling that isn't obvious from reading a single function.
Two modules that communicate through a shared global or shared mutable state, where one assumes the other already ran and set it up. The dependency is invisible until something breaks.