Temporal Cohesion — Types Of Cohesion
Ahmed Samir Ahmed
Senior Android Software Engineer @IDH | Ex E-Finance fintech | Android | IOS | Dotnet
??Temporal Cohesion Definition:
Temporal cohesion refers to the characteristic of a software module where its components are grouped together based on the time frame in which they are processed within the program. In simpler words, if a component within the module carries out multiple tasks that are interconnected by their timing, it exhibits temporal cohesion.
??Overview of Temporal Cohesion:
Temporal cohesion is a specific type of cohesion that refers to how closely connected different parts of a software module are in terms of time. When programmers create software, they use various functions that are related to each other. Even though these functions are related, each one is designed to do a specific job. A good module in a software program should have a clear purpose, and every part of the module should be closely connected to one task. This means that in a well-designed module, all the parts should work together smoothly. Cohesion is an important concept that helps us measure the strength of a software program. Programs with high cohesion have a lot of interdependencies between their parts. On the other hand, a module is considered to have low cohesion if it doesn’t fully perform its intended task.
When the different parts of a software program module are grouped together based on when they are processed, we say that the module has temporal cohesion. This cohesion lasts until the processing of those parts is complete and until a new process begins. The relationship between the elements is based on their timing requirements. A module is linked together with temporal cohesion, meaning that all tasks within it must be performed within a specific timeframe. This cohesion includes the code necessary to initialize all components of the system. Numerous diverse activities take place simultaneously, all within the same time frame.?(they are all done at the same time)
Therefore, a module shows temporal coherence when its parts are put together because they are related in time but ??please be aware??that the elements within a temporally coherent cohesive have a weak connection with each other.
An easy example of a temporally cohesive module is an Initializer or System Startup module, (e.g., a function which is called after catching an exception which closes open files, creates an error log, and notifies the user), (good old modules containing some sort of temporal indication in their names, like “init”, “first”, “next”, “when”, “startup”, “termination”, or “cleanup”), (Initialization of the module, setting the counter to zero, opening the student file, clearing the variables of error messages, initializing the array, etc), and etc.
Lets take an example of Initializer or System Startup module. This module encapsulates and carries out the tasks required to initialize or start up for the?entire system. lets take an example in the following figure.
In the design shown as (ii) in the above figure, there is an enhanced and easier-to-manage approach. In this design, the module takes charge of coordinating the startup process but delegates the responsibility of starting up individual modules to their respective tasks.
??Please know that:?? The functions within a module have weak relationships with each other, but they are strongly connected to functions in other modules. This can result in the need to make changes to multiple modules when performing maintenance.
??Summary of Properties for Temporal Cohesion:
领英推荐
??Let’s take our first example (Tricky Point):
Example that illustrate temporal cohesion include startup(), completeNewEmployee(), and shutdown(). Some programmers view temporal cohesion as undesirable because it is often linked to poor programming practices, such as having a disorganized mixture of code within a startup() function.
To avoid this problem, think of temporal functions as organizers of different events. For example, the startup() function could be responsible for tasks like reading a configuration file, setting up a temporary file, initializing a memory manager, and showing an initial screen. To make it more effective, have the temporally cohesive function call other functions to perform specific activities instead of directly performing those operations itself. This approach ensures that the purpose of the function is clearly focused on orchestrating activities rather than directly executing them.
function completeNewEmployee() {
// startup logic here
// Read configuration file
// Init scratch file
// etc
// shutdown logic here
}
function startup() {
// Read configuration file
// Init scratch file
// etc
}
function shutdown() {
}
function completeNewEmployee() {
startup();
// completeNewEmployee logic here
shutdown();
}
This example brings up the importance of selecting a suitable name that accurately describes the function at the appropriate level of abstraction. Choosing a name like readConfigFileInitScratchFileEtc() would suggest coincidental cohesion, implying that the function encompasses unrelated tasks. However, naming it startup() would make it clear that the function serves a specific purpose and exhibits functional cohesion.
??Let’s take an easy example:
When we create Window Services, we need to initialize some objects to be used by application at runtime.
public class Tempora
{
ILogger logger = null;
public readonly string ApplicationName;
public Temporal()
{
logger = new Logger();
ApplicationName = "Temporal";
//other initialization Logic
}
~Temporal()
{
logger = null;
}
}
Instead of initializing code in constructor, we should initialize code using properties. So that object will be initialized when property is called.
public class Tempora
{
ILogger logger = null
public readonly string ApplicationName;
public Temporal()
{
}
public ILogger Logger
{
get
{
if (logger == null)
logger == new Logger();
return logger;
}
}
~Temporal()
{
if(logger != null)
logger = null;
}
}
??Let’s take a deep example that describes more to understand the concept further:
The grouping of functions or procedures is not based on whether they are pure functions or have side effects. Instead, they are grouped based on their execution time in the system or application’s lifecycle. For instance, if there is a set of functions or procedures that are consistently executed during the application’s initialization,?it is advisable to group them together within the same class or package.
public class AndroidApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ApplicationInitializer initializer = new ApplicationInitializer();
initializer.initializeDatabase();
Date startDate = initializer.detectApplicationStartDate();
initializer.writeStartDateToDatabase(startDate);
initializer.initializeExternalLibraries();
}
}
// the steps that are invoked at the initialization of the application
// will be grouped in the same package or class :
class ApplicationInitializer {
public void initializeDatabase(){
// ...
}
public Date detectApplicationStartDate(){
return new Date();
}
public void writeStartDateToDatabase(Date date){
// ...
}
public void initializeExternalLibraries(){
// ...
}
}
? If there are questions you can connect me on?LinkedIn.
?If you are interested in more articles on my Medium account, you can use this?Link. I hope that you’re having a great day.???
??Don’t forget to give a star or leave a comment if you found it useful.??
PMO Manager | X - EFinance Investment Group ●Project Management●Software Development ●Product Management ●ITIL●PMP●Fintech● Mobile/POS Development●Digital Solutions
1 年Great job ??