Cohesion is a measure of how well the parts of a module fit together. A module is considered cohesive when all its components are closely related and packaged together. Breaking the module into smaller parts would lead to increased coupling between modules, as they would need to communicate with each other in order to achieve the desired results.
Attempting to divide a cohesive module would only result in increased coupling and decreased readability.
Every part of the module is related to the other, and the module contains everything essential to function.
Every part of the module is related to the other, and the module contains every thing essential to function.
Two modules form a communication chain, where each operates on information and/or contributes to some output. For example, add a record to the database and generate an email based on that information.
Two modules must execute code in particular order.
Modules are related based on timing dependencies. For example, many systems have a list of seemingly unrelated things that must be initialized at system startup; these different tasks are temporally cohesive.
Elements in a module are not related other than being in the same source file; this represents the most negative form of cohesion.
Lack of Cohesion of methods is a metric that measures the structural cohesion of a module, typically a component. In other words it’s the sum of sets of methods not shared via sharing fields.
Consider a class with private fields a and b. Many of the methods only access a, and many other methods only access b. The sum of the sets of methods not shared via sharing fields (a and b) is high; therefore, this class reports a high LCOM score, indicating that it scores high in lack of cohesion in methods. Consider the three classes shown below.
Fields appear as single letters and methods appear as blocks.
In Class X, the LCOM score is low, indicating good structural cohesion.
Class Y, however, lacks cohesion; each of the field/method pairs in Class Y could appear in its own class without affecting behavior.
Class Z shows mixed cohesion, where developers could refactor the last field/method combination into its own class.