High Cohesion

Problem definition

High Cohesion is an evaluative pattern that attempts to keep objects appropriately focused, manageable and understandable. High cohesion is generally used in support of Low Coupling. High cohesion means that the responsibilities of a given element are strongly related and highly focused.For example object Big with 100 methods and 2000 code lines for sure does more than object Small with 10 methods and 200 code lines. What is more of 100 methods of object Big is responsible for such a different things as access to database and generating random numbers than it functional cohesion is lower then cohesion of object Small. Criterion of cohesion is not only size of the object and number of it’s code lines but also functional similarity of it’s responsibilities. Breaking programs into classes and subsystems is an example of activities that increase the cohesive properties of a system. Alternatively, low cohesion is a situation in which a given element has too many unrelated responsibilities. Elements with low cohesion often suffer from being hard to comprehend, hard to reuse, hard to maintain and adverse to change.

Analysis

Similarly to Low Coupling pattern, High Cohesion is a principle which should be always taken into consideration while taking any project decisions. It’s an evaluation principle, used for evaluation of quality of different possible solutions. Several levels of cohesion are distinguished:

  • Very low cohesion – class is fully responsible for many task in different and difficult functionality areas
  • Low cohesion – class is fully responsible for complex tasks in one functionality area
  • Moderate cohesion – class is full responsible for many small tasks in different areas but are logicically connected to the class concept.
  • High cohesion – class is responsible for several tasks in one functionality area and cooperates with different classes for their realisation.

How to easily evaluate cohesion of class? Class with high cohesion contains relatively small number of methods which tasks are connected to each other and if any task is difficult it cooperate with other classes. Such classes are easy to maintain and are very reusable. High Cohesion principle is, like many other patterns, analogy to the real world – it is known that if somebody takes to much different responsibilities he has no chances for high efficiency.

Advantages

Thanks to this principle we get the project that is readable , easy to maintain and future development. It has low coupling and code is very reusable .

Follow

Get every new post delivered to your Inbox.