Coupling and Cohesion
Measuring module dependencies and focus
Overview
Coupling and Cohesion are two fundamental metrics for evaluating software design quality.
Cohesion: How strongly related the elements within a module are. HIGH cohesion is good. Coupling: How dependent modules are on each other. LOW coupling is good.
The goal is HIGH cohesion and LOW coupling.
Key Concepts
Cohesion: Relatedness within a module (want HIGH)
Coupling: Dependencies between modules (want LOW)
High cohesion = focused, single-purpose modules
Low coupling = independent, interchangeable modules
These metrics often move together when improved
Code Example
1// ❌ LOW Cohesion: Class does unrelated things
2class Utils {
3 public void sendEmail(String to, String body) { }
4 public int calculateTax(double amount) { }
5 public void compressFile(String path) { }
6 public User parseUserJson(String json) { }
7 public void resizeImage(String path, int width) { }
8}
9
10// ✅ HIGH Cohesion: Each class is focused
11class EmailService {
12 public void send(String to, String body) { }
13 public void sendBulk(List<String> recipients, String body) { }
14 public void sendWithAttachment(String to, String body, File attachment) { }
15}
16
17class TaxCalculator {
18 public double calculate(double amount) { }
19 public double calculateWithDeductions(double amount, List<Deduction> deductions) { }
20}
21
22// ❌ HIGH Coupling: Direct dependencies everywhere
23class OrderService {
24 public void process(Order order) {
25 MySQLDatabase db = new MySQLDatabase(); // Direct dependency
26 StripePayment payment = new StripePayment(); // Direct dependency
27 SendGridEmail email = new SendGridEmail(); // Direct dependency
28
29 db.save(order);
30 payment.charge(order.getTotal());
31 email.send(order.getCustomerEmail(), "Order confirmed");
32 }
33}
34
35// ✅ LOW Coupling: Depend on abstractions
36class OrderService {
37 private final Database database;
38 private final PaymentGateway payment;
39 private final EmailService email;
40
41 // Dependencies injected - can be any implementation
42 public OrderService(Database db, PaymentGateway pay, EmailService mail) {
43 this.database = db;
44 this.payment = pay;
45 this.email = mail;
46 }
47
48 public void process(Order order) {
49 database.save(order);
50 payment.charge(order.getTotal());
51 email.send(order.getCustomerEmail(), "Order confirmed");
52 }
53}High cohesion means each class has a clear purpose. Low coupling means classes depend on interfaces, not implementations.
Advantages
- ✓
Easier maintenance and modification
- ✓
Better testability (mock dependencies)
- ✓
Improved reusability
- ✓
Easier to understand individual modules
- ✓
Changes have limited impact
Best Practices
- 1.
Group related functionality together (high cohesion)
- 2.
Use interfaces to reduce coupling
- 3.
Apply Dependency Injection
- 4.
Avoid "Utility" or "Manager" classes (low cohesion smell)
- 5.
Minimize the public interface of classes
💡 Interview Tips
Define both terms clearly
Remember: HIGH cohesion, LOW coupling
Give examples of both good and bad
Discuss how they relate to SOLID principles
Show how DI helps reduce coupling