28 Cohesion and Coupling

Software Architecture enables a software engineer to analyze the effectiveness of the design in meeting its stated requirements, it also consider the architectural alternatives at a stage when making design changes is still relatively easy. Finally the outcome is, it reduces the risks associated with the construction of the software. Focus is placed on the software component which can be:

  • A program module
  • An object-oriented class
  • A database
  • Middleware

Modularity can be defined as a concept closely tied to abstraction which supports the independence of models. It supports the abstraction in software which in turn supports hierarchical structuring of programs. Modularity enhances design clarity, and eases implementation which reduces cost of testing, debugging and maintenance.

The major characteristics of a good design includes:

  • Component or Functional independence
  • Exception identification and handling
  • Fault prevention and fault tolerance

Functional independence is a key to any good design due to Error isolation, Scope of reuse, and Understandability. Functional independence is measured using Cohesion and Coupling.

 

Cohesion can be defined as the degree to which all elements of a component are directed towards a single task. The degree to which all elements directed towards a task are contained in a single component. The degree to which all responsibilities of a single class are related.

Fig 1: Types of Cohesion

 

Coincidental cohesion are parts of the component that are unrelated (unrelated functions, processes, or data). In this parts of the component are only related by their location in source code. The elements needed to achieve some functionality are scattered throughout the system. This is the worst form of Cohesion.

 

Coincidental Cohesion Example

 

All of the following in one module:

  • Logic to produce student list
  • Logic to produce class grade report
  • Logic to check e-mail
  • Logic to change password

In Logical Cohesion Elements of component are related logically and not functionally. Several logically related elements are in the same component and one of the elements is selected by the caller. It include both high and low-level actions in the same class.

Example:

 

A component reads inputs from tape, disk, and network, All the code for these functions are in the same component, Operations are related, but the functions are significantly different.

 

In Temporal Cohesion, elements of a component are related by timing. Difficult to change because we may have to look at numerous components when a change in a data structure is made. Increases chances of regression fault.

 

Example:

 

An “Initialization” procedure which does the following:

  • Opens all files.
  • Initializes arrays and all variables. Sets all constants
  • Sets all flags
  • Reads the first transaction

A “Termination” procedure which does the following:

  • Writes array contents to disk files.
  • Closes all files.

In Procedural Cohesion, elements of a component are related only to ensure a particular order of execution. Actions are still weakly connected and unlikely to be reusable. The changes to the ordering of steps or purpose of steps requires changing the module abstraction.

 

Example

 

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 certain sequence of steps have to be carried out for achieving an objective.

 

Examples:

  1. A function which checks file permissions then opens the file.
  2. The algorithm for decoding a message.

In Communicational Cohesion, the Module performs a series of actions related by a sequence of steps to be followed by the product and all actions are performed on the same data. The action is based on the ordering of steps on all the same data. Actions are related but still not completely separated.

Example

  • Update record in database and send it to the printer
  • Update a record on a database
  • Print the record
  • Fetch unrelated data at the same time.
  • To minimize disk access

In sequential cohesion, methods are together in a class because the output from one part is the input to another part like an assembly line, in other words, the output of one component is the input to another. This occurs naturally in functional programming languages.

 

Example

 

Functions in one procedure:

  • Search array for a certain name.
  • Place the name in another array and sort the array.
  • Change the name from last-first to first-last.
  • Print first-last name.

In Functional Cohesion, every essential element to a single computation is contained in the component. Every element in the component is essential to the computation. All the statements in the procedure are related in the performance of a single function. Functional cohesion is the strongest cohesion. In a functionally bound module, all elements of the module are related to performing a single function. Function like “computer square root” and “sort the away” are clear examples of functionally cohesive modules. Coupling is the degree of dependence such as the amount of interactions among components. High coupling makes modifying parts of the system difficult, e.g., modifying a component affects all the components to which the component is connected.

Coupling addresses the attribute of “degree of interdependence” between software units, modules or components.

Fig 3: Coupling

In content coupling a module directly references the content of another module. In this the component directly modifies another’s data. That is the component modifies another’s code, e.g., jumps (goto) into the middle of a routine.

 

Example

 

Part of a program handles lookup for customer. When customer not found, component adds customer by directly modifying the contents of the data structure containing customer data.

 

In common coupling more than one component share data such as global data structures, usually a poor design choice because of the lack of clear responsibility for the data. This reduces the readability. It is difficult to determine all the components that affect a data element (reduces maintainability) and to reuse the components. In common coupling the side effects require looking at all the code in a function to see if there are any global effects. The changes in one module to the declaration requires changes in all other modules. The identical list of global variables must be declared for module to be reused.

 

Example

  •  Process control component maintains current data about state of operation. Gets data from multiple sources. Supplies data to multiple sinks.
  • Each source process writes directly to global data store. Each sink process reads directly from global data store.

In Control Coupling the component passes control parameters to coupled components. This may be either good or bad, depending on situation. It is bad if parameters indicate completely different behavior and good if parameters allow factoring and reuse of functionality. Control coupling occurs between modules when data are passed that influence the internal logic of a module (e.g., flags and switches). In an example of control coupling, a module that retrieves either a customer name or an address depending on the value of a flag is illustrated.

In Stamp Coupling the component passes a data structure to another component that does not have access to the entire structure. It requires a second component to know how to manipulate the data structure (e.g., needs to know about implementation). The second has access to more information that it needs. This may be necessary due to efficiency factors: this is a choice made by insightful designer, not lazy programmer.

 

Example

 

In Customer Billing System the print routine of the customer billing accepts customer data structure as an argument, parses it, and prints the name, address, and billing information.

 

Coupling in object oriented design can be classified into three different dimensions of coupling properties which are:

  • Interaction couplinG
  • Component coupling
  • Inheritance coupling

In interaction coupling methods are coupled by interaction in terms of invocation of each other or sharing of data. The object classes may be interaction coupled. The interaction coupled methods may belong to the same object class. In component coupling the component relationship between classes is defined by the use of a class as domain of some instance variable of another class. In interaction coupling, the focus is on how much information is exchanged between methods and classes. With component coupling it can be analyzed that how explicit the coupling between classes is. In Inheritance coupling it only concerns object classes. Two classes are inheritance coupled if one class is a direct or indirect subclass of the other. The inheritance coupling not only exhibit the coupling property between subclasses and super classes but also the coupling property between an interaction coupled object class and the inheritance hierarchy.

 

Summary

 

Software design is a process to transform user requirements into some suitable form, which helps the programmer in software coding and implementation. Functional independence is a key to any good design which can be measured using Cohesion and Coupling. A design which exhibits the property of high cohesion and low coupling is considered to be ideal. In object oriented design three different dimensions of coupling namely Interaction coupling, Component coupling, Inheritance coupling are incorporated.