Coupling and Cohesion

In software engineering, coupling and cohesion are two important concepts that help in assessing the quality of code. Both are metrics to evaluate how well different components of a software system interact with each other (coupling) and how well the internals of a single component are organized (cohesion). Below are detailed explanations of each:

Coupling

Coupling refers to the degree to which one module (class, function, etc.) relies on other modules. High coupling means that a change in one module may necessitate changes in the modules it is coupled with, making the system harder to maintain and understand. Low coupling, on the other hand, means that modules operate independently of each other as much as possible.

Types of Coupling:

  • Content Coupling: One module directly manipulates the content of another.

  • Common Coupling: Multiple modules share global data.

  • Control Coupling: One module controls the logic of another by passing it control information.

  • Stamp Coupling: Multiple modules share a composite data structure and use only parts of it.

  • Data Coupling: Modules share data through well-defined interfaces (parameters).

  • Message Coupling: The lowest form of coupling, modules interact only through message passing.

Best Practices:

  • Favor low coupling by reducing direct interactions between modules.

  • Use interfaces and abstractions to minimize direct dependencies.

Why is Low Coupling Important?

  1. Maintainability: Lower coupling makes it easier to understand and modify individual components without affecting others.

  2. Reusability: Less coupled modules can be more easily reused in other projects, as they have fewer dependencies on existing code.

  3. Testability: Loosely coupled systems are easier to test since individual modules can be isolated and tested independently.

  4. Flexibility: Low coupling allows for more flexibility to change or replace components without affecting the rest of the system.

Cohesion

Cohesion refers to the degree to which the elements within a single module (class, function, etc.) belong together. In a highly cohesive module, the functionalities are closely related, making it easier to understand, maintain, and reuse. Low cohesion usually means that a module has more than one responsibility, making it harder to understand and maintain.

Types of Cohesion:

  • Functional Cohesion: Elements are grouped together because they contribute to a single, well-defined task.

  • Sequential Cohesion: Elements are grouped together so that each one's output serves as input for the next.

  • Communicational Cohesion: Elements are grouped together because they operate on the same data.

  • Procedural Cohesion: Elements are grouped together because they need to be executed in a certain order, but don't necessarily relate to the same function or task.

  • Temporal Cohesion: Elements are grouped by when they are processed, usually related to program startup, shutdown, etc.

  • Logical Cohesion: Elements are grouped because they logically fall under a category, even if they perform different tasks.

  • Coincidental Cohesion: The lowest level of cohesion, where elements have no meaningful relationship and are grouped arbitrarily.

Best Practices:

  • Aim for high cohesion by ensuring that each module has a single, well-defined responsibility.

  • Divide modules further if they start to take on additional responsibilities.

Understanding coupling and cohesion can be particularly useful when you're designing or refactoring software systems. Keeping modules both lowly coupled and highly cohesive can result in software that is easier to maintain, understand, and extend.

COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark