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, a good example is program initialization and termination.
module init timer_A=0; couter_B=0; ... end
Procedual Cohesion
A procedual cohesion module is one that its elements are involved in different activities but executed in a specific order. Example,
module tableupdate read_A(); wirte_B(); ... end
Communicational Cohesion
A communicationally module is one whose elements are not related but use the same input or output data.
module data_A task1_read_A(); task2_read_A(); task1_write_A(); ... end
Sequential Cohesion
A sequentially cohesive module is that one of its elements's output data happens to be input data to another one.
module data_update A.update(); B.update(A); ... end
Functional Cohesion
A sequentially cohesive module is one whose elements contribute to one task, or one problem.
module task task.init; task.compute; task.io; ... end
Informational Cohension (best)
A informationally cohensive module is a "object-oriented" module. It contains complex data structure and all its elements contribute to the object.
module class_A A.init; A.read; A.write; ... end
Cohesion Metrics
A way to decide the type of cohension.
Coupling
Coupling is the degree of interaction between two modules. The levels of couplingis listed below.
Types of Coupling
From worst to best:
- 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
Two modules are said to be control coupled when one module passes a control element to the other module.
example: There are two modules m1 and m2. When m1 passes a string "mission accomplished" to m2, without commanding m2 to do anything, then m1 simply passes data to m1. However, if m1 pass "mission accomplished" followed by commanding m2 to signal correspondingly, then they are control coupled. Control coupling destroys the independence of modules and therefore suffers the disadvantage of decreased reusability.
Stamp Coupling
If a data structure (e.g. a struct in C,) is passed as an argument from one module to the other, but instead of using all data contained in the data structure, the receiver of the argument only uses a part of it, then these two modules are said to be stampl coupled.
example: calculate_witholding( employee_record )
In this example, calculate_witholding will probably need employee salary and id to carry out the computation, however, other data such as address, contact of employee_record are not likely to be needed.
Because a module receives more information than it really needs, the difficulty of data access control suffered by common coupled modules also troubles stamp coupled modules. If arguments passed to a module can be reduced to those necessary ones, it not only increases the readability of the code but also improves the likelyhood of it being reused.
Data Coupling
When two modules pass arguments that are either simple arguments or a data structure in which all elements are used by the called module, then they are said to be data coupled.
example:
- display_time_of arrival( flight_number )
- compute_product( a, b, result )
Data coupling makes the maintainance easier and do not suffer problems introduced in other types of coupling.
Reference
- E. Yourdon, Modern Structured Analysis, Yourdon Press, Englewood Cliffs, NJ, 1989.
- Stephen R. Schach, Object-oriented & classical Software Engineering, McGraw-Hill, NY, NY, 2005