page_title: Cohesion (computer science)
section_title: High cohesion
hierarchical_section_title: Cohesion (computer science) / High cohesion
caption_attribution_description: English: Good, bad apps
context_page_description: In computer programming, cohesion refers to the degree to which the elements inside a module belong together. In one sense, it is a measure of the strength of relationship between the methods and data of a class and some unifying purpose or concept served by that class. In another sense, it is a measure of the strength of relationship between the class's methods and data themselves.
  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. In contrast, low cohesion is associated with undesirable traits such as being difficult to maintain, test, reuse, or even understand.
  Cohesion is often contrasted with coupling, a different concept. High cohesion often correlates with loose coupling, and vice versa. The software metrics of coupling and cohesion were invented by Larry Constantine in the late 1960s as part of Structured Design, based on characteristics of “good” programming practices that reduced maintenance and modification costs.
context_section_description: In object-oriented programming, if the methods that serve a 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 reusability 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.
  Related methods are in the same source file or otherwise grouped together; for example, in separate files but in the same sub-directory/folder.
  Advantages of high cohesion (or "strong cohesion") are:
  Reduced module complexity (they are simpler, having fewer operations).
  Increased system maintainability, because logical changes in the domain affect fewer modules, and because changes in one module require fewer changes in other modules.
  Increased module reusability, because application developers will find the component they need more easily among the cohesive set of operations provided by the module.
  While in principle a module can have perfect cohesion by only consisting of a single, atomic element – having a single function, for example – in practice complex tasks are not expressible by a single, simple element. Thus a single-element module has an element that either is too complicated, in order to accomplish a task, or is too narrow, and thus tightly coupled to other modules. Thus cohesion is balanced with both unit complexity and coupling.
caption: Cohesion
exif: {}