Detailed Classification of Cohesion (from Best to Worst)
Interactive Audio Lesson
Listen to a student-teacher conversation explaining the topic in a relatable way.
Introduction to Cohesion
π Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Cohesion measures how closely related and focused the responsibilities of a single module are. It reflects the 'single purpose' of a module, which is crucial for maintainability and understandability.
So, is high cohesion good for a module?
Exactly! High cohesion indicates that all parts of the module work together for a single purpose. This makes it easier to maintain.
What happens with low cohesion?
Low cohesion can lead to confusion, making a module harder to understand and maintain. It's like a cluttered workspace!
So how many types of cohesion are there?
Great question! There are seven categories of cohesion we need to explore, starting from functional cohesion to coincidental cohesion. Let's dive into the definitions next.
Functional Cohesion
π Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Functional cohesion is the ideal type. It means all elements of a module contribute to a single, well-defined function.
Can you give me an example?
Sure! Think of a module named `CalculateSquareRoot()`. Everything in that module is focused on calculating the square root.
What makes it so desirable?
Modules with functional cohesion are easier to understand, test, and reuse. They minimize side effects from changes, making maintenance straightforward.
Sounds like the perfect module!
It is! Let's discuss sequential cohesion next.
Types of Cohesion
π Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Now, letβs categorize the rest of the types. Starting with sequential cohesion, which has output from one operation serving as input to another.
Could you provide a real-world analogy for this?
Think of streaming a movie: you must buffer first before watching. Similarly, in a `ProcessOrder()` module, each step relies on the previous step's output.
Whatβs next after that?
Next is communicational cohesion, where different operations share data but aren't necessarily dependent on each other. For example, a `CustomerOperations()` module can update, retrieve, and delete customer records using the same customer data.
I see! So each type builds on clarity and purpose?
That's right! Clarity and independence in modules promote better software design.
Low Cohesion Types
π Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Now we get to low cohesion types, starting with procedural cohesion.
Is this why functionally different processes are grouped together?
Exactly! While the procedures are ordered, they may lack functional relevance, such as in `PerformStartup()`.
What about temporal and logical cohesion?
Temporal cohesion groups actions happening at the same time without real function overlap. In contrast, logical cohesion uses a parameter to determine which action to take, like a `ProcessFile(fileType)` method.
And the worst type?
That would be coincidental cohesion. Random groupings, leading to modules that do not cooperate or communicate effectively.
That sounds like a nightmare to manage!
Certainly! Cohesion quality directly affects maintainability. The goal should always be high cohesion.
Achieving High Cohesion
π Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
How do we achieve high cohesion? First, by combining closely related responsibilities into focused modules.
And what else?
Avoid grouping irrelevant operations. For example, keep initialization tasks separate and structured.
So logical and coincidental types should be avoided?
Yes! Focusing on functional, sequential, or communicational types keeps our modules strong and manageable.
Will high cohesion impact coupling too?
Definitely! There's a strong relationship between high cohesion and low coupling, promoting better system design overall.
Introduction & Overview
Read summaries of the section's main ideas at different levels of detail.
Quick Overview
Standard
The section discusses the significance of cohesion in software design by classifying it into various types ranging from functional cohesion, which represents an ideal case, to coincidental cohesion, which is detrimental. Each type is explained in terms of its definition, characteristics, and specific examples.
Detailed
Detailed Classification of Cohesion (from Best to Worst)
Cohesion is a critical concept in software design, reflecting the degree to which the elements of a module belong together. This section categorizes cohesion from the ideal to the undesirable forms, detailing their definitions, characteristics, and implications for software design.
1. Functional Cohesion - Ideal (Very High Cohesion)
Definition: All elements contribute to a single well-defined function.
Characteristics: Functionality can be expressed in one concise statement; example modules include CalculateSquareRoot() and ValidateCreditCardNumber().
Emphasis: Most desirable type of cohesion.
2. Sequential Cohesion - High Cohesion
Definition: Output from one part serves as input to the next, creating a sequence of operations relevant to a single purpose.
Characteristic Example: ProcessOrder() involves validating data, calculating cost, and generating an invoice.
3. Communicational Cohesion - High Cohesion
Definition: Elements operate on the same data, performing distinct functions.
Example: A module CustomerOperations() retrieves, updates, and deletes customer records, all using common customer data.
4. Procedural Cohesion - Medium Cohesion
Definition: Elements grouped by the sequence of execution rather than function.
Example: PerformStartup() executes database initialization followed by user authentication without direct relation.
5. Temporal Cohesion - Low Cohesion
Definition: Elements executed at the same time but unrelated in functionality.
Example: StartupTasks() might open log files and initialize variables, executed together but unrelated actions.
6. Logical Cohesion - Very Low Cohesion
Definition: Elements perform similar but distinct actions grouped by a parameter.
Example: ProcessFile(fileType) acts based on the file type but lacks a cohesive function.
7. Coincidental Cohesion - Worst (Very Low Cohesion)
Definition: Elements grouped randomly without meaningful relationships.
Example: MiscellaneousFunctions() containing various unrelated functions.
Conclusion
Cohesion plays a pivotal role in module design, where striving for high cohesion and avoiding low cohesion is fundamental in building maintenance-friendly software.
Audio Book
Dive deep into the subject with an immersive audiobook experience.
Functional Cohesion (Ideal - Very High Cohesion)
Chapter 1 of 7
π Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
1. Functional Cohesion (Ideal - Very High Cohesion):
- Definition: All elements of the module contribute to the execution of a single, well-defined function. The module performs exactly one task, and all its parts are essential to that task.
- Characteristics: The module's entire functionality can be described by a single, concise statement. Input is received, processed, and a single result is produced.
- Example: A module named CalculateSquareRoot(), ComputePayrollTax(), ValidateCreditCardNumber(), SaveCustomerRecord().
- NPTEL Emphasis: This is the most desirable type of cohesion.
Detailed Explanation
Functional cohesion refers to a situation where all components of a software module are highly related and perform a specific function. In this case, a module is dedicated to a single task - for example, calculating the square root of a number. Each part of this module contributes directly to this specific task, making the module highly focused and effective. The output of the module can be easily articulated, and every component is essential to perform its job, which provides clarity and reduces maintenance challenges.
Examples & Analogies
Imagine a well-organized library, where each section is dedicated to a specific genre of books. Just like a module with functional cohesion focuses on one task, this library section only deals with science fiction books, making it easy for visitors to find what they need quickly.
Sequential Cohesion (High Cohesion)
Chapter 2 of 7
π Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
2. Sequential Cohesion (High Cohesion):
- Definition: The output from one part of the module serves as input to the next part, like a sequence of operations. The elements are involved in a sequence of activities such that the output from one activity is the input to the next.
- Characteristics: The module performs a sequence of operations, but they are all related to a larger, single purpose.
- Example: A module ProcessOrder() that first ValidatesOrderData(), then CalculatesTotalCost(), and finally GeneratesInvoice(). The output of validation feeds into calculation, and the output of calculation feeds into invoice generation.
Detailed Explanation
In sequential cohesion, components of a module interact sequentially. This means that each function or component's output is directly required by the next component. For instance, in a module that processes an online order, the flow would be: first validating the data, then calculating costs, and finally generating an invoice. This type of cohesion reinforces efficiency as each step logically follows from the previous one, improving the module's clarity and reducing potential errors.
Examples & Analogies
Think of baking a cake where you follow a specific sequence of steps: first mix the ingredients, then pour the mixture into a pan, and finally bake it in the oven. Each step relies on the successful completion of the previous one, just as sequential cohesion relies on the output of one function being needed for the next.
Communicational Cohesion (High Cohesion)
Chapter 3 of 7
π Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
3. Communicational Cohesion (High Cohesion):
- Definition: All elements of the module operate on the same input data or produce the same output data. They perform different functions but use common data.
- Characteristics: The activities within the module are related by sharing common input or output data.
- Example: A module CustomerOperations() that GetsCustomerDetails(), UpdatesCustomerAddress(), and DeletesCustomerRecord() β all acting on the customer data object.
Detailed Explanation
Communicational cohesion occurs when elements within a module work together based on shared data, even if they perform different functions. An example could be a customer operations module that handles customer details, updates addresses, and deletes records - all these actions utilize the same data structure concerning customer data. This sharing means that the module generally has better consistency and graduates towards high cohesion since alltasks are relevant to the same topic.
Examples & Analogies
Consider a restaurant where a waiter takes orders, serves food, and delivers the bill. While these tasks are distinctly different, they all revolve around the same customers and dining experience, which keeps them organized and makes the waiter efficient in managing the dining process.
Procedural Cohesion (Medium Cohesion)
Chapter 4 of 7
π Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
4. Procedural Cohesion (Medium Cohesion):
- Definition: The elements of the module are grouped together because they follow a specific sequence of execution or control flow, irrespective of the data they operate on. The elements are part of a specific procedure.
- Characteristics: The module contains a set of activities that are performed in a specific order, even if they are not functionally or communicatively related.
- Example: A module PerformStartup() that first InitializesDatabase(), then LoadsConfigurationSettings(), and finally AuthenticatesUser(). These actions are ordered, but might not directly share data or contribute to a single, tightly defined function beyond 'startup.'
Detailed Explanation
This type of cohesion groups elements based on the procedure used. A procedural module may perform steps in a required order (e.g., initializing a system, loading settings, and authenticating a user). However, while the steps are ordered, they may not directly relate to one another beyond the sequence, leading to moderate cohesion. While this is better than random grouping, it lacks a tighter conceptual connection between functions, which may introduce complexities in understanding.
Examples & Analogies
Think of setting up a new computer: first you connect the cables, then install the operating system, and finally set up user accounts. These steps must occur in that order for the setup to work, but they donβt interact directly with one another - each step is a standalone procedure that contributes to the overall task.
Temporal Cohesion (Low Cohesion)
Chapter 5 of 7
π Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
5. Temporal Cohesion (Low Cohesion):
- Definition: The elements of the module are grouped together because they are all executed at the same time or within the same time window. They are related by time, not by function.
- Characteristics: The module performs a set of unrelated activities that logically should happen together at a particular point in time. Often associated with 'initialization' or 'cleanup' routines.
- Example: A module StartupTasks() that performs OpenLogFile(), InitializeAllVariables(), SetSystemClock(), DisplayWelcomeMessage(). These are all performed at startup but are otherwise unrelated.
- Consequences: Hard to reuse a specific part, changes in one part might affect unrelated parts, harder to understand.
Detailed Explanation
Temporal cohesion occurs when module components are executed together because they happen around the same time, yet they serve different functions. For instance, a startup tasks module might handle opening a log file, initializing variables, and setting the system clock. Although these tasks execute in the same time frame, they do not influence each other, which dilutes the module's cohesion. Consequently, understanding and maintaining such modules can become difficult as they are not fundamentally linked.
Examples & Analogies
Imagine a person preparing breakfast, where they boil the kettle, toast the bread, and fry eggs. Although all these activities happen simultaneously, they are not interdependent. You can change one task without affecting the others, which makes it harder to understand their purpose as a single coordinated effort.
Logical Cohesion (Very Low Cohesion)
Chapter 6 of 7
π Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
6. Logical Cohesion (Very Low Cohesion):
- Definition: The elements of the module perform a set of logically similar but functionally distinct activities. A parameter passed to the module determines which of these activities is executed.
- Characteristics: Uses flags or parameters to control which of several loosely related functions is performed.
- Example: A module ProcessFile(fileType) that takes fileType as a parameter and then ProcessTextFile(), ProcessImageFile(), or ProcessAudioFile(). The module handles different file types, but the logic for each type is distinct.
- Consequences: High coupling, complex control flow, hard to test, difficult to modify.
Detailed Explanation
Logical cohesion arises when a module handles various functions that are similar but not closely linked. This often requires using parameters to determine which function to execute. For instance, a process file module might perform various operations based on a file type, but each of those operations operates independently. This can lead to increased complexity in the code and challenges in maintaining or modifying the module since changes to one function could require changes in handling for others.
Examples & Analogies
Consider a Swiss army knife, which combines different tools (scissors, screwdriver, can opener) into one device. While all tools are logically related as they serve specific functions, using one does not relate to the operation of another. If you needed a new tool, you might have to manage incompatible features, leading to a complex setup that may not work well together.
Coincidental Cohesion (Worst - Very Low Cohesion)
Chapter 7 of 7
π Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
7. Coincidental Cohesion (Worst - Very Low Cohesion):
- Definition: The elements of the module are grouped together randomly, with no meaningful relationship between them. They are in the same module simply by coincidence.
- Characteristics: The module performs completely unrelated functions. It's often a 'dumping ground' for miscellaneous code.
- Example: A module MiscellaneousFunctions() that contains PrintReport(), CalculateCRC(), SortArray(), EncryptPassword().
- Consequences: Extremely difficult to understand, test, reuse, or maintain. Any change in one part of the module is likely to affect others, and a minor change can have widespread, unpredictable side effects.
Detailed Explanation
Coincidental cohesion means that the functions within a module are grouped arbitrarily, without any significant relation to one another. Such modules often end up cluttered with mismatched functions, making it extremely difficult to understand how they work together. Changes to one part can inadvertently affect another, leading to bugs and complicating testing and reuse efforts. This type of cohesion is considered the least desirable in software design.
Examples & Analogies
Think of a junk drawer in a kitchen where you throw random items - batteries, a tape measure, scissors, and old receipts. While all items are in the same location, they are unrelated and finding something specific becomes tiring. Like this drawer, a module with coincidental cohesion lacks clarity and purpose, making it frustrating and inefficient to use.
Key Concepts
-
High Cohesion: Favorable for maintainability and reusability.
-
Cohesion Types: Seven forms from functional to coincidental.
-
Design Principles: Strive for functional, sequential, or communicational cohesion.
Examples & Applications
A module CalculateArea() which calculates the area of a rectangle is functionally cohesive.
A CommunicateWithAPI() module that shares the same data set across multiple calls demonstrates communicational cohesion.
Memory Aids
Interactive tools to help you remember key concepts
Rhymes
High cohesion is key, like birds in a tree, together they thrive, functionality alive!
Stories
Imagine a chef who only prepares desserts. That's functional cohesion, as everything supports one delicious goal.
Memory Tools
F-S-C-P-T-L-C: Funny Sequential Cats Purr Together Loningly, Coinciding β a memory tool for identifying cohesion types.
Acronyms
Cohesion can be remembered as F-S-C-P-T-L-C, standing for Functional, Sequential, Communicational, Procedural, Temporal, Logical, Coincidental.
Flash Cards
Glossary
- Cohesion
A qualitative measure of how well the elements of a module work together toward a single purpose.
- Functional Cohesion
Occurs when all elements in a module contribute to a single, well-defined function.
- Sequential Cohesion
Refers to a situation where the output of one part of the module serves as input to another part.
- Communicational Cohesion
When elements of a module operate on the same data or produce the same output data.
- Procedural Cohesion
Grouping of elements in a module that follow a specific sequence of execution.
- Temporal Cohesion
Grouping of elements that are executed at the same time but are not functionally related.
- Logical Cohesion
Grouping of logically related but functionally distinct activities in a module.
- Coincidental Cohesion
The weakest form of cohesion where elements are grouped arbitrarily without any meaningful relationship.
Reference links
Supplementary resources to enhance your learning experience.