Cohesion & Coupling in Software Engineering

Introduction

Cohesion and coupling techniques are important concepts in software engineering that describe the relationships between different components or modules of a software system.

What is Cohesion?

It refers to the degree to which the elements of a module or component are related to each other and work together to achieve a common purpose. A highly cohesive module or component contains elements that are closely related and have a single, well-defined responsibility.

What is Coupling?

It refers to the degree to which one module or component depends on another. A loosely coupled system has minimal dependencies between its components, while a tightly coupled system has many dependencies. A system with high coupling can be more difficult to modify and maintain, as changes in one component can have unexpected effects on other components.

In software engineering, the goal is to achieve high cohesion and low coupling in order to create software that is easy to understand, modify, and maintain. This can be achieved by carefully designing the structure of the software and the relationships between its components.

Types of Cohesion

Types of cohesion that are commonly used to describe the relationship between elements in a software module or component are:

  1. Functional cohesion: All elements within a module or component are related to a single, well-defined function or task. All elements within the module contribute to the completion of that function or task.
  2. Sequential cohesion: Elements within a module or component are related to a specific sequence of steps or operations that must be performed in order to complete a task. The elements are not necessarily related to a single function or task, but they are related to a specific order of operations.
  3. Communicational cohesion: Elements within a module or component are related to a specific type of data or message passing between them. The elements are not necessarily related to a single function or task, but they are related to a specific type of communication.
  4. Procedural Cohesion: Elements within a module or component are related to a specific procedure or algorithm that must be executed in order to complete a task. The elements are not necessarily related to a single function or task, but they are related to a specific set of steps that must be followed.
  5. Temporal Cohesion: Elements within a module or component are related to a specific time period or event. The elements are not necessarily related to a single function or task, but they are related to a specific time or event.
  6. Logical Cohesion: ​​Elements within a module or component are related to a specific logical condition or decision. The elements are not necessarily related to a single function or task, but they are related to a specific logical condition or decision.

It is generally desirable to have a high level of cohesion, as this can make the module or component easier to understand, modify, and maintain. However, in some cases, a lower level of cohesion may be appropriate if it helps to achieve other design goals or requirements.

Types of Coupling

Types of coupling that are commonly used to describe the relationship between different components or modules of a software system are:

  1. Content Coupling: one module or component accesses the internal contents or data of another module. This type of coupling can be difficult to maintain and can make the system more prone to errors.
  2. Common Coupling: two or more modules share a global variable or data structure. This can make it difficult to understand the relationships between modules and can lead to unexpected side effects.
  3. Control Coupling: one module controls the behaviour or flow of another module by passing it control information. This can make the system more difficult to maintain and modify.
  4. Stamp Coupling: one module passes a large data structure or record to another module, which uses only a small subset of the data. This can lead to inefficiencies in the system and can make it more difficult to modify.
  5. Data Coupling: two or more modules share data through arguments or parameters passed between them. This can be a relatively low-level form of coupling, but it can still make the system more difficult to maintain and modify.
  6. Message Coupling: two or more modules communicate through messages or signals. This can be a more loosely coupled approach, but it can also make the system more difficult to understand and debug.

In general, it is desirable to have a low level of coupling between modules or components, as this can make the system more flexible and easier to modify over time. However, in some cases, a higher level of coupling may be appropriate if it helps to achieve other design goals or requirements.

Applications

Cohesion and coupling are fundamental concepts in software engineering that are widely applied in various software development activities. Some specific applications of cohesion and coupling are:

  1. Software Design: they are used to define the architecture and structure of software systems. Designers use cohesion to ensure that elements of a module or component are related to a single, well-defined function or task.
  2. Software Testing: used in software testing to ensure that each module or component is tested in isolation and that changes to one module do not impact other modules in unexpected ways.
  3. Agile Development: used in agile development methodologies to ensure that software systems are designed and implemented in a way that is flexible and adaptable to changing requirements.

Conclusion

High cohesion and low coupling are also important principles in object-oriented programming, where classes should be designed to have a single, well-defined responsibility and communicate with other classes through well-defined interfaces. This can lead to software that is more flexible, reusable, and maintainable over time.