What is logical cohesion, and why is it bad or undesirable?

Posted by Matt Fenwick on Programmers See other posts from Programmers or by Matt Fenwick
Published on 2012-10-02T18:43:54Z Indexed on 2012/10/02 21:50 UTC
Read the original article Hit count: 230

Filed under:
|
|

From the c2wiki page on coupling & cohesion:

Cohesion (interdependency within module) strength/level names : (from worse to better, high cohesion is good)

  • Coincidental Cohesion : (Worst) Module elements are unrelated
  • Logical Cohesion : Elements perform similar activities as selected from outside module, i.e. by a flag that selects operation to perform (see also CommandObject). i.e. body of function is one huge if-else/switch on operation flag
  • Temporal Cohesion : operations related only by general time performed (i.e. initialization() or FatalErrorShutdown?())
  • Procedural Cohesion : Elements involved in different but sequential activities, each on different data (usually could be trivially split into multiple modules along linear sequence boundaries)
  • Communicational Cohesion : unrelated operations except need same data or input
  • Sequential Cohesion : operations on same data in significant order; output from one function is input to next (pipeline)
  • Informational Cohesion: a module performs a number of actions, each with its own entry point, with independent code for each action, all performed on the same data structure. Essentially an implementation of an abstract data type. i.e. define structure of sales_region_table and its operators: init_table(), update_table(), print_table()
  • Functional Cohesion : all elements contribute to a single, well-defined task, i.e. a function that performs exactly one operation get_engine_temperature(), add_sales_tax()

(emphasis mine).

I don't fully understand the definition of logical cohesion. My questions are:

  • what is logical cohesion?
  • Why does it get such a bad rap (2nd worst kind of cohesion)?

© Programmers or respective owner

Related posts about code-quality

Related posts about metrics