COHESION

Definition In computer programming, cohesion is a measure of how strongly-related or focused the elements (or responsibilities) of a single module are. (The term element refers to any piece of code that performs some work or defines some data.) In other words, cohesion examines how the activities within a module are related to one another. Designing Highly Cohesive Modules The objective of designers is to create highly cohesive modules where all the elements of a module are closely related. However, the elements of one module should not be closely related to the elements of another module. Such a relationship leads to tight coupling between modules. Ensuring high cohesion within modules is one way of reducing tight coupling between modules. Together with coupling, cohesion is one of the best measures of the quality of a design. Levels of Cohesion The different levels of cohesion are described below:

FUNCTIONAL COHESION
Description of 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. A functionally cohesive module performs one and only one problem related task. Examples of functionally cohesive modules are:  Drag Drop – an event triggered when a dragged object is dropped on a window,  Sum Elements in Array,  Convert Kilometers to Miles,  Read Customer Record,  Calculate Net Pay,  Assign Account Number. Functionally cohesive modules may be simple and perform one task, such as Read Customer Record. However, a complex module with numerous sub modules may still be functionally cohesive if all of its subordinate modules are only performed to carry out the task of the parent module. For example, Calculate Net Pay is functionally cohesive although it contains many different functions (e.g., calculate taxable deductions, calculate tax, and calculate CPP deduction). Often, functionally cohesive modules are low-level modules on a Structure Chart. Strengths of Functional Cohesion

Weakness of Functional Cohesion One weakness of functional cohesion is: Designers should guard against designing over-simplified modules or methods. easier to maintain. . a sequentially cohesive module:  has good coupling. A sequentially cohesive module contains activities where output data from one activity serves as input data to the next activity. retrieve customer order. INFORMATIONAL COHESION Description A module has informational cohesion.  Get and edit input data. all performed on the same data structure.Strengths of functional cohesion are:  Functionally cohesive modules are good candidates for re-use.g.  Is easy to maintain. Ex: C++ class works at this level where well designed objects are modules with informational cohesion Strengths of Informational Cohesion  Reduces maintenance cost as it allows changes in a module implementation without changing its usage.  Systems built with functionally cohesive modules are easily understood and. and generate invoice. a function which reads data from a file and processes the data). with independent code for each action. If functional cohesion is taken too far in structured design. Examples of activities in a sequentially cohesive module are:  retrieve customer. the system design consists of hundreds of modules comprised of two or three lines of code. therefore. Strength of Sequential Cohesion In general.  It corresponds to the definition of an ADT (abstract data type) or object in anobjectoriented language. if it performs number of actions each with its own entry point. SEQUENTIAL COHESION Description of Sequential Cohesion Sequential cohesion is when parts of a module are grouped because the output from one part is the input to another part like an assembly line (e.

 Communicational cohesive modules are easy to maintain. or price of book from bibliographic record. The difference between the two types of modules is the order of execution of the activities within the modules. This may make it more difficult to make changes in one part of the code without affecting the functionality of another part of the code. But creating a second module causes duplication of code. A communicational cohesive module is one which performs several functions on the same input or output data. Maintainability is usually improved when you separate a communicational cohesive module into functionally cohesive modules. Activities within a sequentially cohesive module must be carried out in a specific order. a module which operates on the same record of information). based on a passed flag.) Strengths of Communicational Cohesion Strengths of communicational cohesion are:  Communicational cohesion is an acceptable level of cohesion although it is not as good as functional or sequential cohesion. .g. the order of the execution of activities is not important. (Note: The functions in this example could be performed independently of each other and should be separated for greater flexibility. Both are loosely coupled because their elements are not related to elements in other modules. For example. making the unnecessary data redundant. One activity cannot be completed until another activity is done. Comparison of Communicational and Sequentially Cohesive Modules Communicational cohesive modules and sequentially cohesive modules are similar in many respects. Both types of modules are designed around the data they use. The designer must decide whether to discard the redundant data or create a second module passing only the necessary data. COMMUNICATIONAL COHESION Description of Communicational Cohesion Communicational cohesion is when parts of a module are grouped because they operate on the same data (e. But in a communicational cohesive module. title. This is because the activities contained within the sequentially cohesive module generally only meet the requirements of that one module.  It is easier to share code within a communicational cohesive module.Weakness of Sequential Cohesion Sequentially cohesive modules are not as good candidates for re-use as are functionally cohesive modules. Weaknesses of Communicational Cohesion The drawbacks of communicational cohesive modules are:  Other modules may require only part of the input or output data of the communicational cohesive module. making maintenance more difficult. obtain author.

Like procedurally cohesive modules.g. and notifies the user).PROCEDURAL COHESION Description of Procedural Cohesion Procedural cohesion is when parts of a module are grouped because they always follow a certain sequence of execution (e. It is also common for such a module to pass around partially edited data. For example. Activities in a procedurally cohesive module are related by flow of execution rather than by one problem-related function. It is common in a procedurally cohesive module for the input and output data to be unrelated. the activities make up part of some function). a procedurally cohesive module may only initialize alphanumeric fields of an input record before returning the input record. A procedurally cohesive module performs several different and possibly unrelated activities in which control flows from each activity within the module to the next. Weaknesses of Temporal Cohesion ..  Format A for printing. For example.  Procedurally cohesive modules are not as easily maintained as the modules using the high and middle levels of cohesion. or flags. Weaknesses of Procedural Cohesion Weaknesses of procedural cohesion are:  Because a procedurally cohesive module performs no real function in itself. Procedural cohesion may result from an attempt to modularize some part of a flow chart. a function which checks file permissions and then opens the file). TEMPORAL COHESION Description of Temporal Cohesion Temporal cohesion is when parts of a module are grouped by when they are processed . a function which is called after catching an exception which closes open files. a procedurally cohesive module may:  write record B. A temporally cohesive module is one which performs several activities that are related in time.the parts are processed at a particular time in program execution (e. (i. it is generally not a black box and not re-usable.g. One of the most common examples of a temporally cohesive module is an initialization routine that initializes data used by many modules throughout a system.  read record A.e. but as a group perform no real function. temporally cohesive modules typically consist of partial activities whose only relationship to one another is that they are all carried out at a specific time. switches. They often contain a group of functions which make up part of a larger function. creates an error log.

The generalized module is passed a switch to indicate which activity to perform. A logically cohesive module performs several activities of the same general category in which the activity or activities to be executed are selected by the invoking module. Comparison of Temporally and Procedurally Cohesive Modules Temporally cohesive modules are similar to procedurally cohesive modules.A temporally cohesive module has several drawbacks:  Often the activities in a temporally cohesive module are more closely related to other modules than they are to each other. depending on the value of a control flag.  It is more difficult to re-use a temporally cohesive module. the developer has to decide whether to remove the initialization code for the data it wants initialized to create a separate initialization routine or to pass a parameter to the initialization routine indicating the piece of data that is to be initialized. if a module requires only one piece of data to be initialized. changes made to one activity may affect another activity. even if they are different by nature (e.  It makes maintenance of a system more difficult. As with procedurally cohesive modules. grouping all mouse and keyboard input handling routines).g. It is not important in temporally cohesive modules. The activities within the module are all different. This type of cohesion also results in tight coupling of data. A better approach would have been to separate the temporally cohesive initialization routine into functionally cohesive modules that initialize related data. Control flags are often an indication that a module is logically cohesive. They differ in the order of execution of activities. Weaknesses of Logical Cohesion Weaknesses of logical cohesion are: . or deletes various combinations of records. An example of a logically cohesive module is a general I/O routine which reads. Also. Parameters may be empty depending on the activity being executed. For example. In either case. Coupling in both types of modules tends to be poor. The order of execution is important in procedurally cohesive modules. Both make poor black boxes because their functionality cannot be stated simply without knowing the internals of the modules. it cannot call the global initialization routine because that would reset data for the entire system. They are only related because they share the same interface to the module. the existing code that invokes the initialization routine must be modified. writes. Instead.  Maintenance is made more difficult because often the developer is tempted to share code among activities within the module that are only related in time. temporally cohesive modules usually contain sequential statements while procedurally cohesive modules often share repetitions and conditions between activities. LOGICAL COHESION Description of Logical Cohesion Logical cohesion is when parts of a module are grouped because they logically are categorized to do the same thing.

It is even less cohesive than a logically cohesive module because its activities are not even in the same category.e the only relationship between the parts is that they have been grouped together (e. Its activities are not related by flow of data or by flow of control. calculating inventory reorder amount or generating an invoice depending on the value of the parameters passed to the module.g. a “Utilities” class). A coincidentally cohesive module is one whose activities have no meaningful relationship to one another. programmers end up sharing related code. COINCIDENTAL COHESION Description of Coincidental Cohesion Coincidental cohesion is when parts of a module are grouped arbitrarily i. An example of a module using coincidental cohesion is one which performs various functions such as computing net pay. Often. Both logical and coincidental cohesion require the programmer to know the internal details of the modules.  They are also poor candidates for re-use. .  Coincidental cohesion is very difficult to maintain. Weaknesses Coincidental Cohesion Weaknesses of coincidental cohesion are:  Coincidental cohesion is considered the worst level of cohesion. Logically cohesive modules are difficult to maintain.

Sign up to vote on this title
UsefulNot useful