Course contentsShow
← Software Engineering
Lesson 180 of 1,9335. Software Design PrinciplesFree lesson

Understanding Cohesion: What Belongs Together

Define cohesion as how closely the responsibilities within a module relate to each other and why high cohesion is desirable.

Understanding Cohesion: What Belongs Together

What you'll learn: You'll discover what cohesion means in software design and why keeping related responsibilities together makes your code stronger.

What Is Cohesion?

Cohesion measures how closely the responsibilities within a single module (like a class, function, or file) relate to each other. Think of it as asking: "Do all the pieces inside this container truly belong together?"

High cohesion means everything in a module works toward a single, well-defined purpose. Low cohesion means a module is doing multiple unrelated things—it's scattered and unfocused.

The Kitchen Drawer Analogy

Imagine two kitchen drawers:

Drawer A (High Cohesion): Contains only silverware—forks, knives, spoons. Everything relates to eating utensils.

Drawer B (Low Cohesion): Contains silverware, batteries, a screwdriver, rubber bands, and takeout menus. It's a junk drawer—nothing really belongs together.

When you need a fork, which drawer makes your life easier? The focused one. The same applies to code.

Why High Cohesion Is Desirable

When a module has high cohesion:

  • It's easier to understand — You grasp its purpose quickly because it does one thing well
  • It's easier to test — Fewer unrelated concerns means simpler, focused tests
  • It's easier to change — Modifications affect one clear area of responsibility
  • It's more reusable — A focused module can be used elsewhere without dragging along unrelated baggage

High cohesion works hand-in-hand with separation of concerns and encapsulation—principles you've already learned. It also naturally reduces coupling because well-focused modules depend on fewer external pieces.

Key Takeaway: Cohesion measures how well responsibilities within a module belong together; aim for high cohesion where each module has a single, clear purpose.