Measuring Coupling and Cohesion in Practice
You'll learn practical techniques to spot coupling and cohesion problems during code reviews and design discussions.
Making the Abstract Concrete
You understand what coupling and cohesion are conceptually, but how do you actually identify issues in real code? Think of it like a health checkup: you need specific signs to look for, not just a vague sense that something's wrong.
Practical Heuristics for Code Review
Spotting High Coupling
During code reviews, watch for these red flags:
- Change ripple effect: If modifying one class forces changes in 3+ other classes, coupling is too tight
- Import/dependency lists: Long lists of imports suggest a class knows too much about others
- Constructor bloat: Classes requiring many dependencies signal potential coupling issues
- Test brittleness: If tests break whenever unrelated code changes, coupling is high
Identifying Low Cohesion
Look for these warning signs:
- The "and" test: If you can't describe a class without using "and" (e.g., "This class handles users and sends emails"), cohesion is weak
- Unused parameters: Methods ignoring half their class's fields suggest parts don't belong together
- Distant methods: If methods in a class never call each other or share data, they probably shouldn't coexist
- Change patterns: If only 1-2 methods change together repeatedly while others never do, the class lacks cohesion
Using Conversation as a Tool
In design sessions, ask: "If I change requirement X, what else must change?" The fewer the answers, the better your coupling. Ask: "What is this class's one responsibility?" Struggle to answer means cohesion needs work.
Key Takeaway: Measure coupling by tracking change ripples and dependency counts; measure cohesion by checking if a component has one clear, describable purpose that all its parts support.