System Design Academy

Saving…

Lesson 1 of 7 in Module 20 of 45 lessons complete

Coupling & Cohesion

Module 2, lesson 1Concept12 min

Definition

Coupling

In plain English
Coupling is how much one part of the code depends on the details of another part.
Think of it like
It is like two train carriages chained together, where a tight chain means one can't move without the other.

Cohesion

In plain English
Cohesion is how closely the things inside one type or package belong together.
Think of it like
It is like a toolbox holding only plumbing tools instead of a drawer of random items.

Why it exists

You change one line about shelf storage. Five checkout files stop working. Checkout knew too much about shelves.

Ripple effect

In plain English
A ripple effect is when changing one piece of code forces changes in many other places.
Think of it like
Like a stone dropped in a pond sending rings outward.

You want changes to stay local. Low coupling limits dependencies on details. High cohesion keeps related work together.

How it works

Law of Demeter

In plain English
The Law of Demeter says code should talk only to its direct collaborators, not reach through them to their insides.
Think of it like
Like asking a shop assistant for milk instead of walking into their stockroom.
Coupling / cohesionLow cohesionHigh cohesion
Low couplingIndependent parts with mixed purposesTarget: focused parts with few dependencies
High couplingMixed purposes and spreading changesFocused parts that still depend on details
  • Reaching into another type's fields can tie you to its storage choices.
  • Long chains such as a.b.c.d expose several layers of details.
  • Depending on concrete types everywhere can make replacements harder.
  • A util package with unrelated helpers has low cohesion.
  • Needing 'and' to describe unrelated jobs suggests a type needs review.

One checkout follows Store, Shelf and Item links. The other checkout asks Store directly through PriceOf.

Ask the store for a price instead of reaching through shelves.

One checkout follows Store, Shelf and Item links. The other checkout asks Store directly through PriceOf.

Examples

Separate unrelated baggage jobs

BagService scans tags, charges excess-weight fees and sends SMS text messages.

Scan reads a bag tag. ChargeFee calculates the fee in this small example; it does not collect payment. SendSMS prints instead of sending a text.

Before: scan, price and notify for the same bag.
Go
package main

import "fmt"

type BagService struct{}

func (BagService) Scan(tag string)      { fmt.Println("scanned", tag) }
func (BagService) ChargeFee(kg int) int { return kg * 5 }
func (BagService) SendSMS(msg string)   { fmt.Println(msg) }
func main() {
	b := BagService{}
	b.Scan("B42")
	fmt.Println(b.ChargeFee(3))
	b.SendSMS("bag loaded")
}
After: scan, price and notify for the same bag.
Go
package main

import "fmt"

type Scanner struct{}

func (Scanner) Scan(tag string) { fmt.Println("scanned", tag) }

type FeeCalculator struct{}

func (FeeCalculator) ChargeFee(kg int) int { return kg * 5 }

type Notifier struct{}

func (Notifier) SendSMS(msg string) { fmt.Println(msg) }
func main() {
	Scanner{}.Scan("B42")
	fmt.Println(FeeCalculator{}.ChargeFee(3))
	Notifier{}.SendSMS("bag loaded")
}

The insight

If you need 'and' to describe unrelated jobs, the type's cohesion may be low.

Trade-offs

Pros

  • Changes stay local.
  • Focused parts are easier to test.
  • People can work in parallel.

Cons

  • More small types and files.
  • Over-splitting scatters simple logic.

Use when

  • Use these goals throughout a growing app.
  • Review code when one change spreads widely.

Avoid when

  • Do not split a 50-line script into ten packages.
  • Do not separate work that always changes together.

Quick check

Reorganising the shelves breaks the checkout code. What is the problem?
Which type has the highest cohesion?

Takeaways

  • Coupling = how much parts depend on each other's details; keep it low.
  • Cohesion = how well a type's contents belong together; keep it high.
  • Every rule in this module serves those two goals.

Try the quick check above whenever you feel ready.

Previous: UML Sequence Diagrams