Lecture 2: Module Coupling and Cohesion
Overview
This lecture provides a comprehensive guide to Module Coupling and Cohesion in Software Design. Understanding these concepts is fundamental to building maintainable, reusable, and testable software systems. We'll explore different types of coupling (from worst to best) and cohesion levels, along with practical examples.
Lecture Material (PDF)
Types of Coupling (Worst to Best)
Coupling refers to the degree of interdependence between software modules. Lower coupling is generally better as it makes modules more independent and easier to maintain.
Content Coupling (Worst)
One module directly modifies data or instructions within another module. This should be avoided as it creates tight dependencies and makes code fragile.
Common Coupling
Modules share global data. Like a shared document with everyone's passwords - changes affect all modules. Minimize this approach.
Control Coupling
One module passes flags or parameters to control another's flow. Use with caution - modules should have autonomy to make their own decisions.
Stamp Coupling
Modules share a composite data structure but only use parts of it. Sending entire User objects when only purchase history is needed creates unnecessary dependencies.
Data Coupling (Best)
Modules communicate only through parameters, passing only necessary data. Like a sword swing sending damage value to dragon health - clean and minimal dependencies.
Types of Cohesion (Worst to Best)
Cohesion measures how closely the elements within a module belong together. Higher cohesion means a module has a clear, focused purpose.
Coincidental (Worst)
Elements have no clear relationship. Like a toolbox with a hammer, toothbrush, spoon, lightbulb, and sock - random items thrown together.
Logical Cohesion
Elements perform similar activities but are grouped arbitrarily. Utility modules with string manipulation, date formatting, and math operations.
Temporal Cohesion
Elements grouped by when they're processed. Like a morning routine - brushing teeth, showering, dressing - related by timing, not function.
Procedural Cohesion
Elements grouped by execution order. Like following a recipe - steps are sequential but may use different tools and techniques.
Communicational Cohesion
Elements operate on the same data. Like different tradespeople working on the same house - different tasks, shared goal.
Functional Cohesion (Best)
All elements contribute to a single, well-defined task. Like an orchestra where every musician contributes to performing one piece of music.
Design Principles & Benefits
Understanding coupling and cohesion leads to better software design through several key principles and measurable benefits.
Modularization
Breaking down systems into smaller, independent modules. Separate modules for calculations and display improve organization and reusability.
Abstraction
Hiding complex implementation details behind simple interfaces. Users call get_file_contents() without worrying about file handling internals.
Encapsulation
Bundling data and methods within a class. A Dog class with name and bark() method keeps related functionality together.
Information Hiding
Restricting access to internal details. Private variables like __balance in BankAccount prevent unauthorized direct access.
Interface Design
Creating clear and consistent ways to interact. Functions with clear parameters like calculate_area(length, width) are easy to understand and use.
Design Patterns
Proven solutions to common problems. Factory pattern for creating different shapes provides flexibility and maintainability.
Labs & Practical Exercises
Apply your knowledge of coupling and cohesion through hands-on lab exercises. These labs will help you identify and improve module design in real code.
Lab Answers
Check your solutions and compare with provided answers.
