Appearance
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 tooLow 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 unaffectedCohesion
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)
- Content Coupling → directly modifying another class's data — WORST
- Common Coupling → sharing global variables
- Control Coupling → passing flags to control another class
- Stamp Coupling → passing whole object when only part needed
- Data Coupling → passing only needed data — GOOD
- Message Coupling → communicating via interfaces/abstractions — BEST
Types of Cohesion (Worst to Best)
- Coincidental → random unrelated methods grouped together — WORST
- Logical → grouped by type but unrelated (all math funcs)
- Temporal → grouped because called at same time
- Procedural → grouped because of execution order
- Communicational → operate on same data
- Sequential → output of one feeds into next
- 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
| Coupling | Cohesion | |
|---|---|---|
| Measures | Dependency between classes | Focus within a class |
| Goal | Low | High |
| Bad sign | Classes know too much about each other | Class does unrelated things |
| Fix | Interfaces, abstraction | Split into focused classes |
| Relates to | Between classes | Within 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."