Coupling:
Coupling refers to how much two software modules depend on each other. High coupling means they are tightly
connected, and changes in one can affect the other. Low coupling is preferred because it makes modules more
independent and easier to manage. Here are the types of coupling:
1. Data Coupling: Modules share data through parameters. They are independent and only exchange data.
Example: a customer billing system.
2. Stamp Coupling: Whole data structures are passed between modules, sometimes for efficiency. It can lead
to unnecessary data being shared.
3. Control Coupling: Modules share control information, like one module deciding how another behaves.
Example: a sort function using a comparison function.
4. External Coupling: Modules depend on external factors like hardware or protocols, affecting how they
interact.
5. Common Coupling: Modules share global data. Changes to this data can affect all modules using it, making
it harder to maintain.
6. Content Coupling: One module directly changes or controls another module's data. This is the worst type of
coupling and should be avoided.
7. Temporal Coupling: Modules depend on the timing or order of events, making them harder to maintain and
test.
8. Sequential Coupling: One module's output becomes the input for another, creating a sequence of
dependencies.
9. Communicational Coupling: Modules share communication channels, like message queues or databases,
which can lead to performance problems.
10. Functional Coupling: Modules depend on each other's functions, making the code tightly connected and
harder to modify.
11. Data-Structured Coupling: Modules share a data structure, like a database table, which can cause issues
with data integrity and performance.
12. Interaction Coupling: One class calls methods of another class, which can lead to high dependency if
internal details are accessed.
13. Component Coupling: A class depends on another class by using its variables or methods, creating a link
between the two classes.
Cohesion:
Cohesion measures how closely the elements within a module or component work together to achieve a single
purpose. High cohesion means that everything in the module is focused on one task, making the code more
organized and easier to maintain. Low cohesion means the elements serve different purposes, making the code
harder to manage. Here are the different types of cohesion:
1. Functional Cohesion: The best type of cohesion where everything in the module works towards a single,
clear function. Example: a function that calculates and returns a value.
2. Layer Cohesion: Found in layered systems, where higher layers use lower layers, but not the other way
around.
3. Communicational Cohesion: All operations in the module deal with the same data. Example: a class that
handles data storage and access.
4. Sequential Cohesion: Data flows from one part of the module to another. Example: an operation that
processes data step by step.
5. Temporal Cohesion: Elements are grouped because they happen around the same time, like system startup
tasks.
6. Logical Cohesion: Elements are related logically, but not by function. Example: handling inputs from different
devices in one component.
7. Coincidental Cohesion: The worst type, where elements are grouped together randomly and are not related
at all.
8. Procedural Cohesion: Tasks are grouped by their execution order, such as a sequence of related actions.
9. Informational Cohesion: Elements work together because they operate on the same data structure or object,
common in object-oriented programming.