Cohesion (computer science)

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In computer programming, cohesion refers to the degree to which the elements of a module belong together.[1] Thus, it is a measure of how strongly related each piece of functionality expressed by the source code of a software module is.

Cohesion is an ordinal type of measurement and is usually described as “high cohesion” or “low cohesion”. Modules with high cohesion tend to be preferable because high cohesion is associated with several desirable traits of software including robustness, reliability, reusability, and understandability whereas low cohesion is associated with undesirable traits such as being difficult to maintain, difficult to test, difficult to reuse, and even difficult to understand.[citation needed]

Cohesion is often contrasted with coupling, a different concept. High cohesion often correlates with loose coupling, and vice versa.[citation needed] The software quality metrics of coupling and cohesion were invented by Larry Constantine [2] based on characteristics of “good” programming practices that reduced maintenance and modification costs.

High cohesion[edit]

In computer programming, cohesion is a measure of how strongly related the responsibilities of a single module are. As applied to object-oriented programming, if the methods that serve the given class tend to be similar in many aspects, then the class is said to have high cohesion. In a highly cohesive system, code readability and the likelihood of reuse is increased, while complexity is kept manageable.

Cohesion is increased if:

  • The functionalities embedded in a class, accessed through its methods, have much in common.
  • Methods carry out a small number of related activities, by avoiding coarsely grained or unrelated sets of data.

Advantages of high cohesion (or “strong cohesion”) are:

  • Increased understanding of modules (they are simpler, having fewer operations).
  • Increased ease in maintaining a system, because logical changes in the domain affect fewer modules, and because changes in one module require fewer changes in other modules.
  • Increased ease in reusing a module, because application developers will find the component they need more easily among the cohesive set of operations provided by the module.

Types of cohesion[edit]

Cohesion is a qualitative measure meaning that the source code text to be measured is examined using a rubric to determine a cohesion classification. The types of cohesion, in order of the worst to the best type, are as follows:

Coincidental cohesion (worst)
Coincidental cohesion is when parts of a module are grouped arbitrarily; the only relationship between the parts is that they have been grouped together (e.g. a “Utilities” class).
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 mouse and keyboard input 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/informational 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 (e.g. tokenizing a string of XML).

Although cohesion is a ranking type of scale, the ranks do not indicate a steady progression of improved cohesion. Studies by various people including Larry Constantine, Edward Yourdon, and Steve McConnell [3] indicate that the first two types of cohesion are inferior; communicational and sequential cohesion are very good; and functional cohesion is superior.

While functional cohesion is considered the most desirable type of cohesion for a software module, it may not be achievable. There are cases where communicational cohesion is the highest level of cohesion that can be attained under the circumstances.[citation needed]

See also[edit]

References[edit]

  1. ^ Yourdon, E.; Constantine, L L. Structured Design: Fundamentals of a Discipline of Computer Program and Systems Design. Yourdon Press, 1979
  2. ^ W. Stevens, G. Myers, L. Constantine, “Structured Design”, IBM Systems Journal, 13 (2), 115-139, 1974.
  3. ^ Code Complete 2nd Ed., p168-171

External links[edit]