What is Coupling? What is Cohesion? Explain different types of Cohesion and Coupling with proper example.
Understanding Coupling and Cohesion in Software Engineering
What is Coupling?
- Coupling refers to the degree of interdependence between modules or classes in a software system.
- High coupling indicates strong dependencies, making changes difficult and increasing the risk of unintended consequences.
- Low coupling promotes modularity, reusability, and maintainability.
Types of Coupling
- Content Coupling: One module directly modifies data within another. Example: Module A directly changing variables inside Module B. This is extremely undesirable.
- Common Coupling: Modules share global data. Example: Multiple modules accessing and modifying the same global variable. Difficult to manage and understand.
- Control Coupling: One module controls the flow of execution in another using flags or parameters. Example: Module A passing a flag to Module B to dictate its behavior. Often avoidable through better design.
- Stamp Coupling: Modules share a composite data structure, but only use a portion of it. Example: Passing a large object when only a small subset of its data is needed. Inefficient and leads to tight coupling.
- Data Coupling: Modules communicate through passing data parameters. Example: Module A passing specific data values to Module B. The most desirable form of coupling.
What is Cohesion?
- Cohesion refers to how closely related the functions within a single module or class are.
- High cohesion implies that a module performs a single, well-defined task.
- Low cohesion suggests that a module performs unrelated tasks, making it difficult to understand, maintain, and reuse.
Types of Cohesion
- Coincidental Cohesion: Parts of a module have no logical relationship. Example: A module containing unrelated functions such as logging and database access. Weakest form of cohesion.
- Logical Cohesion: A module performs multiple related functions, but each is invoked independently. Example: A module performing several validation operations. Slightly improved over coincidental, but still weak.
- Temporal Cohesion: Functions within a module are grouped because they are executed at the same time. Example: A module containing functions that are all executed during system initialization. Better than logical, but still not ideal.
- Procedural Cohesion: Functions are grouped because they are executed in a specific order. Example: A module with functions that perform steps in a particular process. Improves upon temporal.
- Communicational Cohesion: Functions operate on the same data. Example: A module containing functions that process data from a single input file. Strong cohesion, good design.
- Sequential Cohesion: Output of one function serves as input for another. Example: A module with functions where the output of one is the direct input of the next. Very strong cohesion.
- Functional Cohesion: A module performs a single, well-defined function. Example: A module calculating the area of a circle. The strongest form of cohesion and ideal.