CSC/ECE 517 Fall 2007/wiki2 7 b2
Assignment 2 - Topic 7 - Cohesion and Coupling
Cohesion and coupling are concepts that are reasonably easy to understand, but nonetheless, it is challenging to find good examples that are succinct. Browse the hundreds of Web pages that attempt to explain these concepts, picking your favorite examples. Many of these pages mention related concepts; list some of them and explain how they relate to cohesion and coupling. Be sure to mention the metrics that can be used to measure how well a program conforms to the principles of high cohesion and low coupling.
The Concepts
The object-oriented paradigm, the virtuals of readability, maintainability, extensibility, reusability are realized through modularity, which aims at decomposing a complex problem into smaller pieces called modules (e.g. class). A module can be understood as a contiguous program statements, bounded together, having a set of its own variable names and an aggretate identifer.
Well designed object-oriented programs defines proper logic and operational boundaries among different modules, through which many aforementioned desiable properties can be achieved. The degree of interaction within one module was quantified and called module cohesion by Myers in 1978 and the degree of interaction between two different modules was called module coupling. It is considered as good traits when a program has high level cohesion while a low level coupling.
Cohesion
Types of Cohesion
From worst to best, the types of cohesion are listed below:
- Coincidental Cohesion (worst)
- Logical Cohesion
- Temporal Cohesion
- Procedual Cohesion
- Communicational Cohesion
- Sequential Cohesion
- Functional Cohesion
- Informational Cohension (best)
Coincidental Cohesion (worst)
Coincidental cohesion means the module are grouped randomly, without significant relationshiop. Example,
module miscellaneous sin(); read(); sendsignal(); ... end
Logical Cohesion
A logical cohesion module is one whose elements are somewhat in the same category. Example,
module IOfunction printf(); read_A; write_B; end
Temporal Cohesion
The elements of the module are related in time
Examples
Cohesion Metrics
Coupling
Types of Coupling
From worst to best, the types of coupling are listed below:
- Content Coupling (worst)
- Common Coupling
- Control Coupling
- Stamp Coupling
- Data Coupling (best)
Content Coupling (worst)
Two modules are said to be content coupled when one module have direct access or reference to the contents of the other one. Content coupling suffers a great disadvantage. It is simply impossible to reuse content coupled modules because any change to one of the modules would require change to another one. one example would be
Common Coupling
Two modules are said to be common coupled if both have access to the same global data. Common coupling is undesirable for several reasons. First, if the commonly referred global variale is changed, then every common coupled module has to be changed as well. This greatlly decreases the code maintainability. Second, because more than one module has the potential to modify the value of a global variable, when an error occurs, it is difficult to identify the cause due to very minimal readability. Third, data access control becomes very difficult with common coupled modules. However, common coupling could be a solution when a number of modules have to access a set of attributes defined as global variables.
Control Coupling
Examples
Coupling Metrics
Reference
- E. Yourdon, Modern Structured Analysis, Yourdon Press, Englewood Cliffs, NJ, 1989.