An easy example of temporal cohesion is an Initializer or System Startup module.
Temporal cohesion is a type of cohesion where the elements (like functions or procedures) within a module are grouped together because they are all processed at the same time or within the same timeframe. The actions might not have any logical relationship other than their simultaneous execution. It's often considered a weaker form of cohesion compared to functional or sequential cohesion.
Understanding Temporal Cohesion Through Examples
Based on the provided reference, here are some clear instances demonstrating temporal cohesion:
- System Startup Modules: A classic example is a module or function designed to execute tasks required when a program or system begins. This could include initializing variables, setting up configurations, opening initial files, or connecting to databases. All these actions are grouped because they occur during the startup time.
- Error Handling or Cleanup Functions: Another common scenario involves actions that need to happen immediately after a specific event, such as catching an exception or initiating a shutdown.
- The reference highlights a function invoked after catching an exception. This function might simultaneously perform multiple, otherwise unrelated, tasks such as:
- Closing all open files to prevent data loss or resource leaks.
- Creating an error log entry detailing the exception.
- Notifying the user about the error.
These three actions are grouped within the same function purely because they must all be executed at the moment an exception is caught. Their logical purpose isn't tightly coupled (one handles files, one handles logging, one handles user interface), but their timing requirement links them.
- The reference highlights a function invoked after catching an exception. This function might simultaneously perform multiple, otherwise unrelated, tasks such as:
- Modules with Temporal Names: As noted in the reference, modules often named with temporal indicators like "init" frequently exhibit temporal cohesion, signifying tasks performed during an initialization phase.
In summary, temporal cohesion groups operations not by what logical task they collectively perform, but by when they are executed relative to the program's timeline or specific events.