ArticleslgStudy

computer science

Cohesion (computer science)

Cohesion (computer science) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Cohesion (computer science) rather than just read about it. In short: 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.

Cohesion (computer science) — main illustration
Cohesion (computer science) — illustration

Key takeaways

  • Cohesion (computer science) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Cohesion (computer science) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Cohesion (computer science) from memory before moving on to harder problems.

Reference excerpt

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. 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 software traits including robustness, reliability, reusability, and understandability. In contrast, low cohesion is associated with undesirable traits such as being difficult to maintain, test, reuse, or understand. Cohesion is often contrasted with coupling. 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. Structured Design, cohesion and coupling were published in the article Stevens, Myers & Constantine (1974) and the book Yourdon & Constantine (1979). The latter two subsequently became standard terms in software engineering.

High cohesion

In object-oriented programming, a class is said to have high cohesion if the methods that serve the class are similar in many aspects. 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, with fewer operations. Increased system maintainability, because logical changes in the domain affect fewer modules, and 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 is either too complicated to accomplish a task, or too narrow and thus tightly coupled to other modules. Thus cohesion is balanced with both unit complexity and coupling.

Types of cohesion Cohesion is a qualitative measure, meaning that the source code is examined using a rubric to determine a classification. Cohesion types, from the worst to the best, 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). Example:

Logical cohesion Logical cohesion is when parts of a module are grouped because they are logically categorized to do the same thing even though they are different by nature (e.g., grouping all mouse and keyboard input handling routines or bundling all models, views, and controllers in separate folders in an MVC pattern). Temporal cohesion Temporal cohesion is when parts of a module are grouped according to the time in which they are processed. The parts are processed at a particular time in program execution (e.g., a function that is called after catching an exception that 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 that 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 that 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 that 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., Lexical analysis of an XML string). Example:

Perfect cohesion (atomic) Example.

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

See also Coupling (computer science) List of object-oriented programming terms Static code analysis

References

External links Definitions of Cohesion metrics Cohesion metrics Measuring Cohesion in Python

Worked examples

Example 1 — a first encounter with Cohesion (computer science)

Start with the simplest possible case. Write down what Cohesion (computer science) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Cohesion (computer science) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Cohesion (computer science) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Cohesion (computer science)

In research
Cohesion (computer science) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Cohesion (computer science) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Cohesion (computer science) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Programming principles, Software architecture, Software metrics, so understanding it makes those chapters shorter.
In everyday life
Look for Cohesion (computer science) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Cohesion (computer science)” →

How to study Cohesion (computer science) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Cohesion (computer science) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Cohesion (computer science) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Cohesion (computer science) in simple terms?

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.

Why does Cohesion (computer science) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Cohesion (computer science)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Cohesion (computer science).

Tags

  • Programming principles
  • Software architecture
  • Software metrics

Keep exploring