madhavrao… » 2007 » December

Cohesion: Cohesion is a measure of how strongly related and focused the responsibilities of an element are. An element with highly related responsibilities, and which does not do a tremendous amount of work, has high cohesion. These elements includes classes, subsystem and so on. A class with low cohesion do many unrelated things, or does too much of work. Such classes suffer from the following problems:

  • hard to comprehend
  • hard to reuse
  • hard to maintain
  • delicate; constantly effected by change

Types of Cohesion

The types of cohesion, in order of worst to best are as following:

  • Coincidental Cohesion
  • Logical Cohesion
  • Temporal Cohesion
  • Procedural Cohesion
  • Communicational Cohesion
  • Sequential Cohesion
  • Functional Cohesion

Coincidental Cohesion (worst): Coincidental cohesion is when parts of a module are grouped arbitrarily (at random); the parts have no significant relationship (e.g. a module of frequently used mathematical functions).

Logical Cohesion: Logical cohesion is when parts of a module are grouped because they logically are categorized to do the same thing, even if they are different by nature (e.g. grouping all I/O handling routines).

Temporal Cohesion: Temporal cohesion is when parts of a module are grouped by when they are processed - the parts are processed at a particular time in program execution (e.g. a function which is called after catching an exception which closes open files, creates an error log, and notifies the user).

Procedural cohesion: Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e.g. a function which checks file permissions and then opens the file).

Communicational cohesion: Communicational cohesion is when parts of a module are grouped because they operate on the same data (e.g. a module which operates on the same record of information).

Sequential cohesion: Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.g. a function which reads data from a file and processes the data).

Functional cohesion (best): Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module

Coupling: Its measure of how strongly one element is connected to, has knowledge of, or relies on other elements. An element with low (or weak) coupling is not dependent on too many other elements. A class with with high coupling relies on many other classes. Such classes may be undesirable; some suffer from the following problems:

  • Changes  in related classes forces changes
  • Harder to understand in isolation
  • Harder to reuse because its use requires the additional presence of the classes on which it depends

Types of Coupling

Types of coupling in order of highest to lowest are as following:
(This also includes procedural coupling)

  • Content Coupling
  • Common Coupling
  • External Coupling
  • Control Coupling
  • Stamp Coupling
  • Data Coupling
  • Message Coupling
  • Subclass Coupling
  • Temporal Coupling

Content Coupling (high): Content coupling is when one module modifies or relies on the internal workings of another module. Therefore changing the way the second module produces data will lead to changing the dependent module. In OOP improper implementation of AOP can creep in this type of coupling.

Common Coupling: Common coupling is when two modules share the same global data. Changing the shared resource implies changing all the modules using it. Enumerations, Constants are few constructs which should be looked into for this type of coupling in OOP

External Coupling: External coupling occurs when two modules share an externally imposed data format, communication protocol, or device interface. Interfacing technology should be decoupled from main application by using Adapter pattern to overcome this type of coupling.

Control Coupling: Control coupling is one module controlling logic of another by passing it information on what to do. Class members should be made private, they should be exposed using public accesses. If any variable is used for controlling logic of the class, then it should be modified by methods of the class and avoid exposing them with public accesses.

Stamp Coupling: Stamp coupling exists when module shares a composite data structure and uses only part of it. E.g. passing a whole record to a function which only needs one field of it. This may lead to changing the way a module reads a record because a field which the module doesn’t need has been modified.

Data Coupling: Data Coupling is when modules share data for example; parameters. Chang in data structure all related modules.

Message Coupling (low): This is loosest type of coupling. Modules are not dependent on each other, instead they use public interface to exchange parameter less messages (or events)

Subclass Coupling: Describes relationship between a class and its parent. The class is connected to its parent, but the parent isn’t connected to child.

Temporal Coupling: When two actions are bundled together into one module just because they happen to  occur at same time then it leads to temporal coupling.