Whether a design is good depends mostly on two measures: how strongly the parts of a module belong together, and how much the modules depend on each other.

Cohesion

Cohesion measures the strength of the relation among the elements within a module. The higher the cohesion, the better. The types, from the weakest to the strongest, are as follows. Coincidental cohesion has unrelated elements put together by chance. Logical cohesion joins elements that do similar kinds of things, chosen by a flag, such as all input routines. Temporal cohesion joins elements that are run at the same time, such as initialisation. Procedural cohesion joins elements that must follow a sequence of control. Communicational cohesion joins elements that work on the same data. Sequential cohesion has the output of one element as the input to the next. Functional cohesion has all elements contributing to a single task, which is the best.

Definition: Cohesion. The degree to which the elements within a module belong together and contribute to a single, well-defined task.

Coupling

Coupling measures the degree of interdependence among modules. The lower the coupling, the better. The types from the weakest to the strongest are as follows. Data coupling passes only simple data items as parameters, and it is the best. Stamp coupling passes a data structure of which only a part is used. Control coupling passes a flag that controls the logic of the other module. External coupling shares an external device or format. Common coupling shares global data. Content coupling lets one module change the inside of another, and it is the worst. Low coupling means that a change in one module is unlikely to need a change in another.

Measuring and Improving

A design is judged to be good when modules are functionally cohesive and are data coupled. To improve a design, a module with weak cohesion is split into modules that have one task each, global data are replaced by parameters, and control flags are removed by redesigning the interface. Complex relations are reduced by keeping the hierarchy shallow and the fan-out small.

Table 3.3: Types of cohesion and coupling

Rank Cohesion (weak to strong) Coupling (strong to weak)
Worst Coincidental Content
Logical, temporal Common
Procedural External, control
Communicational Stamp
Best Sequential, functional Data

Example 3.3: Classifying coupling and cohesion

Problem: Classify (a) a module that does printing, tax and date checking, (b) two modules that pass only an integer parameter, (c) two modules that read and write the same global variable.

Solution:

  1. Unrelated tasks in one module show coincidental cohesion.
  2. Passing only simple parameters is data coupling.
  3. Sharing a global variable is common coupling.

Result: (a) coincidental cohesion, the weakest. (b) data coupling, the best. (c) common coupling, which is poor.

Common mistakes

  • Confusing cohesion, which is inside a module, with coupling, which is between modules.
  • Using global data for convenience, which raises coupling.
  • Creating a module that has several unrelated tasks, such as a Utility module.

Key takeaways

  • Cohesion is within a module and should be high.
  • Coupling is between modules and should be low.
  • Functional cohesion and data coupling are the best.
  • Coincidental cohesion and content coupling are the worst.

Practice questions

  1. What is the best type of cohesion?
  2. What is the best type of coupling?
  3. What is common coupling?

Answers to the practice questions

  1. Functional cohesion.
  2. Data coupling.
  3. Modules sharing global data.

Sources

  • Official BCA syllabus Advanced Software Engineering, course code 25BCA405DS01, Unit III