My account

Coupling and Modularity

Coupling is not good or bad by itself; a system with no coupling does nothing. Look at three things for each connection: how much knowledge it shares (strength), how far apart the two sides are (distance), and how often they change (volatility). Keep strong coupling close, keep far parts on small contracts, and put things that change together in one place.

Not reviewedWritten with AI helpReading time: 15 minOnline shop exampleStrengthdistancevolatilityC# examples

Author: bezzad

Contents

The problem: “loosely coupled” services that change together

Our online shop has six services. They talk only by messages, so the team says: “We are loosely coupled.”

But last month a small change in the discount rule needed changes in four services, four pull requests from three teams, and a release on the same night. Something is wrong.

The slogan “make everything loosely coupled” did not help. We need a better way to see coupling.

What is coupling?

Two parts are coupled when a change in one can force a change in the other. Each connection exists because one part needs knowledge from the other: a method name, a data shape, a business rule, an order of steps.

A system with zero coupling does nothing, because the parts cannot work together. So the goal is not “no coupling”. The goal is the right coupling in the right place.

Two old words help here:

  • Cohesion. How much the things inside one module belong together. High cohesion: one module does one job, and its parts change together.
  • Modularity. Splitting a system into parts that can be understood and changed mostly alone.

A useful rule: things that change together should live together.

Three things to look at

For each connection between two parts, ask three questions:

  1. Strength. How much knowledge crosses the border?
  2. Distance. How far apart are the two sides?
  3. Volatility. How often will this knowledge change?

Strength: how much knowledge is shared

Strongest: shares the most knowledgeIntrusiveOrder code reads the Inventory tables directlyFunctionalBoth modules repeat the same discount ruleModelOrder uses the full Product class of InventoryContractOrder calls a small public contract: Reserve(items)Weakest: shares only what is needed
From top to bottom, each level shares less knowledge. Lower is easier to change.
  1. Intrusive. One side uses the other’s private parts: its tables, private fields, or internal classes. The owner cannot change anything safely.
  2. Functional. Both sides must know the same business rule. For example, both calculate the discount. If the rule changes, both must change, at the same time.
  3. Model. One side uses the other’s full domain model. It knows more fields and meanings than it needs.
  4. Contract. One side uses a small public contract made for this purpose. Internal details can change freely behind it.
Messages do not make coupling weak by magic: If an event carries the whole internal Order object, the consumers are coupled to that model. Async changes when the call happens, not how much is shared.

A smaller lens: connascence

Inside the code, you can name the kind of shared knowledge more exactly. This idea is called connascence (two things “born together”). Some common forms, from weaker to stronger:

Form Both sides must agree on Shop example A weaker way
Name A name Method name ReserveStock Fine; renaming tools help
Type A type Price is a decimal Fine inside one module
Meaning What a value means Status 3 means “shipped” A named enum value
Position The order of values (id, qty, price) in a list Named fields or a record
Algorithm The same calculation Same hash or same discount math One shared, owned function or service
Execution order Which call comes first Reserve before Pay One method that does both steps
Timing How long something takes “The cache is ready in 5 s” An explicit signal or event

A small example of connascence of meaning:

// Two modules must both "know" that 3 means shipped.
if (order.Status == 3) SendShippedEmail(order);

// Better: the meaning has a name in one place.
public enum OrderStatus { New = 1, Paid = 2, Shipped = 3 }

if (order.Status == OrderStatus.Shipped) SendShippedEmail(order);

Distance: how far apart

Same classSame moduleOther moduleOther team'sserviceOther companyone commitone pull requestone releasea plan with a teammonths, a contractThe farther apart, the more a shared change costs
The same shared change costs a commit when the code is close, and months when the other side is another company.

Distance is measured in coordination, not in lines of code:

  • Inside one class, a shared change is one commit.
  • Between two modules of one app, it is one release.
  • Between two teams, it needs a plan, a meeting, maybe a versioned contract.
  • With another company, it can take months.

Distance has three faces: the code structure (class, module, service), the runtime (same process or over the network), and the owners (same team or another team). Owners often matter most.

Volatility: how often it changes

Some knowledge almost never changes: the rules of a date, an ISO country code, a stable public library. Some changes every week: pricing and discount rules, the core business that makes the company special.

You can guess volatility from the business (what is the company still learning?) and measure it from git history: which files change together, and how often?

Putting them together

StrongWeakstrengthClose (same module)Far (other team or service)distanceCohesionThings that change togetherlive together. Good.Distributed monolithEvery change needs a meetingwith another team. Worst.Local complexityExtra layers and interfacesinside one small module.Loose couplingFar parts share only a small,stable contract. Good.
Strong coupling is fine when the parts are close. Far parts should share only a small contract.
  1. Strong and close: cohesion. Good. The parts that share a lot live in one module and one team.
  2. Weak and far: loose coupling. Good. Far parts share only a small, stable contract.
  3. Strong and far: distributed monolith. The worst place. Each change needs other teams and synchronized releases.
  4. Weak and close: local complexity. Extra interfaces, mappers and layers between parts that always change together. It adds work and no safety.

Volatility decides how much it hurts. Strong and far coupling to something that never changes (for example, a stable standard) is cheap. Strong and far coupling to the weekly-changing discount rule is very expensive. A simple way to remember: pain ≈ strength × distance × volatility. Make any one of the three small and the pain drops.

Back to the discount rule

Why did one discount change touch four services?

  1. The discount rule was copied into Cart, Order, Invoice and Promotion: functional coupling.
  2. These are four services of three teams: far.
  3. Discounts change every week: high volatility.

All three are high, so the pain is high. We can turn any knob:

  • Lower the strength. Only the Pricing module calculates discounts. The others ask for the final price, or receive it in a message.
  • Lower the distance. If Cart and Order always change together and one team owns them, maybe they should be one service.
  • Isolate the volatility. Hide the weekly-changing rules behind one stable contract: “give me the price for this cart”.

Finding good module borders

  1. Start from the business, not the database. List the main jobs (catalog, cart, ordering, payment, shipping). Each job is a candidate module.
  2. Walk through real use cases. “A customer places an order” touches which modules? If one use case touches five modules, the borders may be wrong.
  3. Check what changes together. Git history shows files that change in the same commits. If they are in different modules, consider moving them.
  4. Check the characteristics. Parts with very different needs (scale, availability, security) are candidates for separate deploy units.
  5. Repeat at every level. The same rules work for methods, classes, modules, services and whole systems.

Common mistakes

Mistake Result The right way
“Loose coupling everywhere” Many layers and mappers inside one small module. Strong coupling is fine when close.
Splitting by technical layer into services Every feature crosses all services. Split by business job.
Sharing database tables between services Intrusive coupling over a long distance. Each part owns its data; share a contract.
Copying a business rule into many places One rule change, many releases. One owner for each rule.
Thinking messages remove coupling Big events tie consumers to the internal model. Small events made for consumers.

Summary in six lines

  1. Coupling is shared knowledge; zero coupling means a useless system.
  2. Strength: intrusive, functional, model, contract. Lower is easier to change.
  3. Distance is about coordination: code structure, runtime and owners.
  4. Volatility is how often the knowledge changes; core business changes the most.
  5. Strong and close is cohesion; weak and far is loose coupling; strong and far is the worst.
  6. To reduce pain, lower strength, lower distance, or hide volatile rules behind a stable contract.

Done reading this lesson?Mark it as finished so it shows in your lessons list and report card.