What is cohesion in programming?
Cohesion in software design is a term used to describe the degree to which the elements of a module or component are related to each other and serve a common purpose. In simple words is keeping related things together and unrelated things out.
Usually in programming you want to increase cohesion. And what does that mean? It means when designing a program if you organize the code in a high cohesive way it's much better to manage it, finding the source of bugs and locate what you want based on the functionalities organization you defined.
For example, let's say you create two different modules or components and each of them is responsible for a different functionality in your program. So increasing the cohesion of your software would be to concentrate everything related to a specific functionality within the module or component responsible for that functionality.
In small programs the cohesion might not seem so relevant but as the scale changes and the code becomes larger, so does the importance of having a cohesive code. Keeping the cohesion in your code is also an important habit even when writing small pieces of code such as naming a function or a variable that is related to what the function actually does or to what the variable actually stores.
Once cohesion can exist in many different levels that can be related based on distinct criteria, following are some examples of different types of cohesion.
Types of cohesion
The types are ordered by strength or weakness of cohesion forms, with the strongest at the top of the list and the weakest at the bottom of the list.
Functional: this one is related specifically to functions and it comes to writing functions with no side effects. An example of that is writing functions respecting the function scope and not using global keywords, such as global variables. This is the highest level of cohesion and is generally considered to be the most desirable.
Layer: this type of cohesion refers to the situation where elements are organized into layers or tiers. It's often used in software design to organize complex systems into manageable parts, where each layer is responsible for a specific aspect of the system's functionality and it has its related services. For example, the concept of having models, views and controllers in a program can be considered layer cohesion, once we have a strong hierarchy between each layer where high level features access low level features, never the opposite - it's a one way flow.
Communicational: what acts on the same data should be kept together, so the elements are related by the data they share. An example of that are classes or instances of a class, and they all have methods acting on the same data.
Sequential: this type of cohesion occurs when elements are arranged together in a specific order and depend on each other to complete a task. The output of one element is typically the input to the next element. An example of that could be processing some data before using it (such as a big text block or a spreadsheet of numbers), and let's say that requires many steps of data treatment and formatting. So the result of each step is an input for the next.
Procedural: this case is similar to sequential once the elements are related by the sequence in which they are executed, but they do not necessarily perform a single, well-defined computation. They're not necessarily working on the same data (as in the sequential example) yet the procedures order is still relevant and the reason why we should group them all together.
Temporal: this is considered a weaker form of cohesion (in comparison to the previous ones) and the elements are related by the time at which they are executed. For example,
Utility: finally when you have some unrelated code and it's hard to find some relation to group them, you can try to group them by utility, which means if they don't have anything in common and they're not directly related to anything or to each other, so this is already something they have in common. So you can group them, such as helper functions you group in a
utils
file.
Conclusion
Cohesion in software design can exist on many different levels (such as modules, classes, files, functions, etc) and have different types depending on the criteria that was chosen to relate them.
A high cohesion is an important aspect of creating maintainable, reusable, and understandable code.
Top comments (0)