Lecture 17: Classification of Cohesion
Unit 3: Requirement Analysis and Design (4353202)
Lecture Agenda
- Recap of Analysis vs. Design
- What is Cohesion?
- Why is Cohesion Important?
- Levels of Cohesion (from weakest to strongest)
- Examples for Each Level of Cohesion
- Goal: High Cohesion
- Key Takeaways
Recap of Analysis vs. Design
Analysis focuses on what the system should do (problem domain), while Design focuses on how the system will do it (solution domain).
What is Cohesion?
Cohesion is a measure of how strongly related and focused the responsibilities of a single module or class are. It describes the degree to which the elements within a module belong together.
In simpler terms, it's about how well the internal parts of a module work together to achieve a single, well-defined purpose.
Why is Cohesion Important?
- Improved Readability: Highly cohesive modules are easier to understand because they do one thing well.
- Increased Maintainability: Changes are localized within a module, reducing ripple effects.
- Enhanced Reusability: Well-defined, single-purpose modules are easier to reuse in other parts of the system or in different projects.
- Reduced Complexity: Breaking down a system into cohesive units reduces overall complexity.
Levels of Cohesion (Weakest to Strongest)
- Coincidental Cohesion
- Logical Cohesion
- Temporal Cohesion
- Procedural Cohesion
- Communicational Cohesion
- Sequential Cohesion
- Functional Cohesion
1. Coincidental Cohesion (Weakest)
Elements are grouped together for no apparent reason other than being in the same module. They have no meaningful relationship to each other.
- Example: A module named `Utilities` that contains functions like `calculateTax()`, `printReport()`, and `sendEmail()`.
- Problem: Changes to one function have no relation to others, making the module hard to understand and maintain.
2. Logical Cohesion
Elements perform similar logical functions, but the specific function to be performed is selected by a control flag or parameter.
- Example: A module that performs all input/output operations, where a parameter determines if it's reading from a file, database, or network.
- Problem: The module has multiple responsibilities, and changes to one logical function might affect others.
3. Temporal Cohesion
Elements are grouped because they are all processed at the same time or during the same phase of program execution.
- Example: A `startup()` module that initializes all system variables, opens log files, and establishes database connections.
- Problem: Functions are related by time, not by purpose. If a new startup activity is added, it might not logically belong with existing ones.
4. Procedural Cohesion
Elements are grouped because they are part of a specific sequence of execution. The output of one element might be the input of another, but they don't necessarily operate on the same data.
- Example: A module that contains functions for `readInput()`, `processData()`, and `writeOutput()`, where the order of execution is important.
- Problem: Functions are related by control flow, not by data or purpose.
5. Communicational Cohesion
Elements are grouped because they operate on the same input data or produce the same output data.
- Example: A module that contains functions like `calculateEmployeeSalary()` and `updateEmployeeRecord()`, both operating on the `Employee` data structure.
- Benefit: Better than procedural, as functions are related by data.
6. Sequential Cohesion
Elements are grouped because the output of one element serves as the input for the next element. This is a strong form of cohesion, often found in data processing pipelines.
- Example: A module with `readSensorData()`, `filterNoise()`, and `storeProcessedData()`. The output of `readSensorData()` is input to `filterNoise()`, and its output is input to `storeProcessedData()`.
- Benefit: Clear flow of data and responsibility.
7. Functional Cohesion (Strongest)
Elements are grouped because they all contribute to a single, well-defined function or task. The module performs one and only one task, and all its elements are essential for that task.
- Example: A module named `CalculateSquareRoot` that contains all the logic and sub-functions required to compute the square root of a number.
- Benefit: Highly reusable, easy to understand, and simple to maintain. This is the ideal level of cohesion.
Goal: High Cohesion
In software design, the goal is to achieve **Functional Cohesion** as much as possible. If not, then **Sequential** or **Communicational Cohesion** are also considered good.
Avoid **Coincidental**, **Logical**, and **Temporal Cohesion** as they lead to poor design and maintenance issues.
Key Takeaways
- **Cohesion** measures how related elements within a module are.
- Higher cohesion leads to better **readability, maintainability, and reusability**.
- **Functional Cohesion** is the strongest and most desirable type.
- Understanding cohesion helps in designing **robust and flexible** software systems.
Next Lecture
Topic: Classification of Coupling
Q & A
Questions & Discussion

