Skip to content

Coupling vs Cohesion

Two measures of code quality in OOP design:

  • Coupling → how much classes depend on each other
  • Cohesion → how focused a class is on one clear purpose

Golden Rule: Aim for LOW coupling + HIGH cohesion

Coupling

High Coupling — Bad

cpp
class Car {
public:
    Engine engine;
    void drive() {
        if (engine.rpm > 2000 && engine.temp < 100 && engine.turbo == true)
            cout << "Car driving fast" << endl; // knows too much about Engine internals!
    }
};
// Car knows TOO MUCH about Engine internals
// Change Engine → must change Car too

Low Coupling — Good

cpp
class Engine {
private:
    int rpm=3000; int temp=90; bool turbo=true;
public:
    bool isPerformanceReady() { return rpm>2000 && temp<100 && turbo; }
    string getDiagnostics() { return "RPM:" + to_string(rpm); }
};

class Car {
    Engine engine;
public:
    void drive() {
        if (engine.isPerformanceReady()) // only knows behavior
            cout << "Car driving fast" << endl;
    }
};
// Car only knows WHAT Engine can do — not HOW it works internally
// Change Engine internals → Car is unaffected

Cohesion

Low Cohesion — Bad

cpp
class Utility { // everything dumped in one class — no clear purpose!
public:
    void createUser(string name) { /* ... */ }   // user stuff
    void sendEmail(string to) { /* ... */ }        // email stuff
    void readFile(string path) { /* ... */ }       // file stuff
    int calculateTax(int amount) { /* ... */ }     // math stuff
    void generateReport() { /* ... */ }             // report stuff
};

High Cohesion — Good

cpp
class UserService { void createUser(string n) {/*...*/} void deleteUser(string n) {/*...*/} };
class EmailService { void sendEmail(string to) {/*...*/} void sendBulkEmail(/*...*/) {/*...*/} };
class FileService { void readFile(string path) {/*...*/} void writeFile(/*...*/) {/*...*/} };
class TaxCalculator { int calculate(int amount) {/*...*/} };
// Each class does ONE thing well!

Types of Coupling (Worst to Best)

  1. Content Coupling → directly modifying another class's data — WORST
  2. Common Coupling → sharing global variables
  3. Control Coupling → passing flags to control another class
  4. Stamp Coupling → passing whole object when only part needed
  5. Data Coupling → passing only needed data — GOOD
  6. Message Coupling → communicating via interfaces/abstractions — BEST

Types of Cohesion (Worst to Best)

  1. Coincidental → random unrelated methods grouped together — WORST
  2. Logical → grouped by type but unrelated (all math funcs)
  3. Temporal → grouped because called at same time
  4. Procedural → grouped because of execution order
  5. Communicational → operate on same data
  6. Sequential → output of one feeds into next
  7. Functional → everything contributes to ONE clear task — BEST

Quick Checklist

Low coupling checklist:

  • [ ] depend on interfaces not concrete classes
  • [ ] use dependency injection
  • [ ] hide internals behind methods
  • [ ] minimal knowledge of other classes

High cohesion checklist:

  • [ ] class has one clear purpose
  • [ ] all methods relate to that purpose
  • [ ] class name clearly describes what it does
  • [ ] small and focused

Comparison Table

CouplingCohesion
MeasuresDependency between classesFocus within a class
GoalLowHigh
Bad signClasses know too much about each otherClass does unrelated things
FixInterfaces, abstractionSplit into focused classes
Relates toBetween classesWithin a class

One-liner: "Coupling measures dependency between classes — keep it low via abstractions. Cohesion measures how focused a class is — keep it high by giving each class one clear responsibility."