Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 50 ยท Software Design

Design Principles: Abstraction, Cohesion and Coupling

Learn the core software design principles: abstraction, modularity, information hiding, cohesion, coupling and separation of concerns, with a refactoring example.

Requirements say what a system must do. Design decides how it is organized so that it can do it, and, just as important, so that people can keep changing it for years. Two programs can behave identically today and still differ enormously in how painful the next change will be. The difference is design.

Good design is not about cleverness or fashionable patterns. It rests on a handful of old, stable ideas: hide complexity behind simple interfaces, split a system into parts that each do one job, and keep the connections between parts few and narrow. This lesson covers those ideas. The runnable examples are plain JavaScript; they show the principles at the size of a single file, but the same reasoning applies from functions up to whole services.

Abstraction

Abstraction means focusing on what something does and ignoring how it does it. When you call sort(list), you rely on the promise "the list comes back ordered" and do not care whether it uses quicksort or merge sort. An abstraction is a simplification you can trust.

Software is built in layers of abstraction. A function hides a few statements; a class hides data and functions; a module hides classes; a service hides modules. Each layer lets the person above it think with fewer details. The art is choosing abstractions that match the problem: chargeCustomer(order) is a good abstraction because it is how the business talks, while doStep3(x, y, flag) is not, because it reveals nothing about intent.

A useful test: can you describe what the unit does in one sentence without using the word "and" and without mentioning its internals? If not, the abstraction is probably muddled.

Modularity and information hiding

A module is a unit of a system with a defined purpose and a defined interface: a function, a class, a file, a package, a service. Modularity is dividing a system into such units so that each can be understood, built, tested and replaced on its own.

Just dividing is not enough; where you draw the lines matters. David Parnas, in a famous 1972 paper, proposed a criterion that is still the best one: information hiding. Each module should hide a design decision that is likely to change. If the way you store users might change (array today, database tomorrow), put that decision inside one module and let everyone else talk to it through a small interface. When the decision changes, only that module changes.

This is the same idea as private attributes in class diagrams: a minus sign in front of a field says "this is a secret of this class." The interface is the public promise; everything behind it is free to change.

Cohesion

Cohesion measures how strongly the things inside one module belong together. High cohesion means everything in the module serves one clear purpose. Low cohesion means the module is a grab bag.

From weak to strong cohesion

Coincidental Unrelated things thrown together, like a utils file.
Temporal Grouped only because they run at the same time.
Sequential Output of one step feeds the next.
Functional Every part works toward one task.

Aim for functional cohesion. The weak kinds are warning signs.

1 / 2

A UserValidator that only validates users has high cohesion. A Helpers class with formatDate, sendEmail and calculateTax has low cohesion: there is no single reason to open that file, so every change risks touching something unrelated. A practical hint is the single responsibility principle: a module should have one reason to change. If a tax-law change and an email-provider change both force edits to the same file, it has two responsibilities.

Coupling

Coupling measures how much one module depends on the internals of another. Low (loose) coupling means modules interact through small, stable interfaces and know little about each other. High (tight) coupling means a change in one module ripples into many others.

Forms of coupling, from loosest to tightest:

  • Data coupling: modules exchange only the plain values they need (total(price, quantity)).
  • Stamp coupling: modules share a whole data structure but use only part of it.
  • Control coupling: one module passes a flag that tells another what to do (render(page, true)), so the caller must know the callee's internals.
  • Common coupling: modules share global data, so any of them can break the others.
  • Content coupling: one module reaches into another's internals directly. This is the worst kind.

The slogan to remember is high cohesion, low coupling. The two are linked: a module with one clear job tends to need few outside connections, and a module with muddled jobs ends up entangled with everything. Together they predict how easy a system will be to change. A related practical rule is the Law of Demeter: talk to your direct collaborators, not to the collaborators of your collaborators. A call chain like order.customer.address.country.code is a symptom of tight coupling to a structure you do not own.

Separation of concerns

A concern is a distinct aspect of what the program must handle: displaying data, validating input, applying business rules, saving to storage, logging. Separation of concerns means keeping each concern in its own part of the code, so you can reason about one thing at a time.

A classic split is presentation, business logic and data access. If a function both computes a discount and writes HTML and queries the database, you cannot test the discount rule without a database, and you cannot reuse it in a mobile app. If the discount rule is a pure function, it needs only numbers to test. The architecture styles in the next lesson are, at heart, ways to separate concerns at the scale of the whole system.

Refactoring a tangled module

Here is a small order-processing module that works, but mixes concerns. Run it first and see the output.

JS Before: one function does everything

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The function validates input, looks up prices, applies a discount rule, changes stock and prints, all in one block. It is cohesive in a weak sense (everything is about orders) but has at least five reasons to change. The price table and discount rule are buried and cannot be tested without printing. Now the same behavior, separated:

JS After: small modules behind small interfaces

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The output is identical for the three orders, and the last line shows something new: pricing is testable with no stock and no console. Notice what changed. Each function has one job (cohesion). processOrder talks to stock through has and take only, so switching stock to a database means changing createStock alone (information hiding and low coupling). Results are returned as data, not printed, so a web page, a command-line tool and a test can all reuse the logic (separation of concerns).

This is not free: the second version has more code and more names. The trade is deliberate. For a throwaway script, the first version is fine. Design effort pays off in proportion to how long the code will live and how often it will change.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which pairing describes the design goal most software engineers aim for?

    A module should do one clear job (high cohesion) and depend on other modules through small interfaces (low coupling).
  2. 2. According to information hiding, what should a module hide?

    Parnas's criterion: hide the decisions most likely to change behind a stable interface so that change stays local.
  3. 3. A function takes a boolean flag that switches between two completely different behaviors. Which kind of coupling is this?

    Passing a flag that tells the callee what to do means the caller must know how the callee works inside: control coupling.
  4. 4. Why is it easier to test the refactored pricing code than the original function?

    Separating concerns lets the pricing rule run with plain numbers, without inventory or printing.
  5. 5. A file called Helpers holds date formatting, email sending and tax calculation. What is the main problem?

    A grab bag has several unrelated reasons to change, which is coincidental cohesion, the weakest kind.

Summary

  • Abstraction lets you use a unit by what it promises, not how it works.
  • Modules are best divided by information hiding: each hides a decision likely to change.
  • Cohesion is how well a module's parts belong together; aim for one clear job.
  • Coupling is how much modules depend on each other's internals; aim for narrow, stable interfaces.
  • Separation of concerns keeps display, rules and storage apart, which makes each testable and reusable.
  • Refactoring toward these principles costs code and names, and pays back as code lives longer and changes more often.

Next: Architecture Styles.