Monday, 13 April 2020

Cohesion

Cohesion:Cohesion is a measure of the degree to which the elements of the module are functionally related.

High Cohesion makes it easier to:

  • Understand what a class or method does
  • Use descriptive names
  • Reuse classes or methods
Example:
//Example for Low Cohesion
class Utils {
                public static Time getSystemTime(){
                                return Time;
                }
               
                public static String captilizeName(String useName) {
                                return userName;
                }
               
                public static boolean validateEmail(String email) {
                                return isValidEmail;
                }
}

//Example for High Cohesion

class EmailUtils {
                public static boolean validateEmail(String email) {
                                return isValidEmail;
                }
}

class TimeUtils {
                public static Time getSystemTime(){
                                return Time;
                }
}

class NameUtils {
                public static String captilizeName(String useName) {
                                return userName;
                }
}

Categories of Cohesion:


1) Functional Cohesion: Functional cohesion is when parts of a module are grouped because they all contribute to a single well-defined task of the module.
Functional Cohesion is said to exist if the different elements of a module, cooperate to achieve a single function.

2) Sequential Cohesion: if the element of a module form the components of the sequence, where the output from one component of the sequence is input to the next.
Example: A function which reads data from a file and processes the data.

3) Communicational Cohesion: Tow elements operate on the same input data or contribute towards the same output data.
Example: Module determines customer details like the use customer account no to find and return customer name and loan balance

4) Procedural Cohesion: A module is said to possess procedural cohesion, if the set of functions of the module are all part of a procedure (algorithm) in which a certain sequence of steps have to be carried out for achieving an objective
Example: A function which checks file permissions then opens the file

5) Temporal Cohesion: When a module includes functions that are associated by the fact that all the methods must be executed in the same time, the module is said to exhibit temporal cohesion
Example: a function which is called after catching an exception which closes open files, creates an error log, and notifies the user.

6) Logical Cohesion: Elements are logically related not functionally
Logical cohesion is when parts of a module are grouped because they are logically categorized to do the same thing, even if they are different by nature (e.g. grouping all mouse and keyboard input handling routines).
Example: Print Functions: An example of logical cohesion is the case where a set of print functions generating different output reports are arranged into a single module.

class Calculate {
int a, b;
void Calculate(int a, int b){
this.a  = a;
this.b = b;
}
void printAddition(){
print(a+b);
}
void printSub(){
print(a-b);
}

void printMul(){
print(a*b);
}
}

7) Coincidental Cohesion: Coincidental cohesion is when parts of a module are grouped arbitrarily; the only relationship between the parts is that they have been grouped together.
A module is said to have coincidental cohesion if it performs a set of tasks that are associated with each other very loosely, if at all.
Ex: Utils example.

No comments:

Post a Comment