Types of Coupling and Cohesion
Types of Coupling and Cohesion
In software engineering, coupling and cohesion are key concepts that relate to the design of
systems. Understanding these concepts can help in evaluating and improving software design.
Types of Coupling
Coupling refers to the degree of interdependence between software modules. It indicates how
closely connected different components are. Lower coupling is generally preferred because it
leads to more modular and maintainable code. Here are the main types of coupling:
1. Content Coupling: One module accesses the internal data of another module. This is the
highest degree of coupling and is usually avoided.
2. Common Coupling: Multiple modules share a common global variable. Changes to this
variable can affect all modules, making maintenance difficult.
3. External Coupling: Modules depend on externally defined data formats or protocols.
This can create issues if the external definitions change.
4. Control Coupling: One module controls the behavior of another by passing it
information on what to do. This can lead to tightly coupled code.
5. Stamp Coupling: Modules share a composite data structure, but only some of the data is
used by each module. This can lead to unnecessary dependencies.
6. Data Coupling: Modules share data through parameters. This is the lowest level of
coupling and is generally considered acceptable.
Cohesion
Cohesion refers to how closely related and focused the responsibilities of a single module are.
Higher cohesion within a module means that its functions are closely related, making the module
easier to maintain and understand. The types of cohesion, from lowest to highest, are:
Design Evaluation
Evaluating design in software engineering typically involves assessing the coupling and cohesion
of modules. Here are some common methods for evaluation:
1. Code Review: Peers review code to identify areas of high coupling and low cohesion,
suggesting refactoring where necessary.
2. Static Analysis Tools: Tools can analyze code for metrics related to coupling and
cohesion, providing quantitative data.
3. Design Patterns: Using established design patterns can promote low coupling and high
cohesion by providing proven architectural solutions.
4. Refactoring: Regular refactoring sessions help improve code structure by reducing
coupling and increasing cohesion.
5. Unit Testing: Well-structured, cohesive modules are easier to unit test. If testing reveals
difficulties, it may indicate a need for better design.
6. Metrics Analysis: Employing metrics such as LCOM (Lack of Cohesion of Methods) for
cohesion and coupling metrics can help quantify design quality.