You are on page 1of 28

Cohension

and Coupling
Type of coupling

High Coupling
Content

Common

Control

Loose
Stamp

Data

Uncoupled
Low
Coupling

• Data and control are two types of information flow across modules.

• If only some data is exchanged, the amount of coupling is probably acceptable.

• Exchanging lots of data and control results in a high degree of coupling.


Stamp Coupling: Two modules are stamp
coupled if they communicate using
composite data items such as structure,
Control Coupling: Control Coupling exists
Data Coupling: When data of one module objects, etc. When the module passes
among two modules if data from one
is passed to another module, this is called non-global data structure or entire
module is used to direct the structure of
data coupling. structure to another module, they are
instruction execution in another.
said to be stamp coupled. For example,
passing structure variable in C or object in
C++ language to a module.

External Coupling: External Coupling


arises when two modules share an
Common Coupling: Two modules are Content Coupling: Content Coupling
externally imposed data format,
common coupled if they share exists among two modules if they share
communication protocols, or device
information through some global data code, e.g., a branch from one module into
interface. This is related to
items. another module.
communication to external tools and
devices.
Content coupling: # Content Coupling Example
class ModuleA:
def method1(self):
Cases where classes are dependent on
each other’s internal structures & their moduleB.method2('specific_argument')
implementations, happen because of
wrong abstractions. It creates the class ModuleB:
problem of updating / changing together. def method2(self, arg):
A change in one causes a change in the # Do something with the specific argument
other one. As the program grows, this
problem becomes inextricable.
Content coupling occurs when one
module makes direct references to the
internals of another module, such as
calling a function in the second module In this example, ModuleA directly calls ModuleB's method2
and passing in specific arguments. This and passes in a specific argument. This creates a tight coupling
can make it difficult to modify the second between the two modules.
module without affecting the first.
Content coupling: # Module A
def square(x):
return x ** 2

# Module B
def print_square(x):
In this example, print_square() function result = square(x) # content coupling
depends on the implementation details print(f"The square of {x} is {result}.")
of square() function. To fix this, we can
modify print_square() function to
perform the square operation itself # Module A
instead of calling square() function. # No change needed

# Module B
def print_square(x):
result = x ** 2 # no content coupling
print(f"The square of {x} is {result}.")
Content coupling:
# Module A
# No change needed
In the fixed code, print_square() function
performs the square operation itself # Module B
using the ** operator, so it does not def print_square(x):
depend on the implementation details of result = x ** 2 # no content coupling
square() function. print(f"The square of {x} is {result}.")
# Module A
x=0

Common Coupling: # Module B


from module_a import x

def print_x():
Common coupling occurs when two or print(x) # common coupling
more modules depend on a shared global
variable or resource. This can lead to
issues when multiple modules try to
modify the same resource at the same
time.
In this example, Module A defines a
global variable x, and Module B reads
and prints the value of x. This creates
common coupling between the two
modules.
Common Coupling:
To fix this, we can modify Module B to
pass the value of x as a parameter
instead of using a global variable.

Fixed code: # Module A


x=0

# Module B
def print_x(x):
print(x) # no common coupling

# Call the function with the value of x from Module A


print_x(x)
Control Coupling:

Control coupling occurs when one


module controls the flow of execution of
another module. This can lead to tight
coupling between the two modules and
make it difficult to modify either one
without affecting the other
# Module A
def get_data_from_api():
# code to call external API and retrieve data
External Coupling: pass

# Module B
from module_a import get_data_from_api
In this example, Module B depends on an
external API to retrieve data, creating
external coupling between the two def main():
modules. If the API changes, Module B will data = get_data_from_api()
also need to change to adapt to the new
API. # code to do something with data
pass
To fix this, we can introduce an additional
layer of abstraction to separate the API
dependency from Module B. For example,
we can modify Module A to provide a more
generic function that can retrieve data
from different sources (not just the API),
and modify Module B to call this new
function instead of get_data_from_api()
directly.
# Module A
def get_data(data_source):
# code to retrieve data from a given source
External Coupling: pass

# Module B
from module_a import get_data

In the fixed code, we have introduced a def main():


new get_data() function in Module A that data = get_data('api')
abstracts away the data source. Module # code to do something with data
B can now call get_data() with the data pass
source as an argument, and Module A
will retrieve the data from the
appropriate source. This reduces the
external coupling between the two
modules, making the code more
maintainable and less prone to breaking
if the API changes.
Types of coupling
• Content coupling is a type of coupling where two modules share a common piece of
data, or where one module directly modifies the internal state of another module.
This is typically considered to be the most tightly coupled type of coupling, as any
change to one module can have a direct impact on the other module.

• On the other hand, data coupling is a type of coupling where two modules
exchange data through parameters or return values, but do not share any internal
data structures or have any direct access to each other's internal state. This is
considered to be a looser type of coupling, as any changes to the data exchanged
between modules can be isolated to just those modules and do not have a direct
impact on other modules.
Types of coupling

• Content coupling: One module modifies local variables of another module or


branches execution into another module. Violates the principle of information hiding.
• Control coupling: Using control flags, one module controls the execution path in
another module. Makes code hard to understand. A dependency in which structures
control each other’s flows by passing flags. It is a special case of data coupling.
• Common coupling: Modules access global variables. Modules are bound together via
shared data structures. Leads to unexpected side effects and error propagation.
Dependencies on global data and variables. If you change the global variable,
everything related will be affected.
Type of coupling

• Stamp coupling: A module passes an entire data structure to another when only
some data members are needed.
• External coupling: When interfacing with external tools, devices or libraries, module
is coupled to the external device interface, communication protocol or data format. If
the component or system they use in common along the structures imposes format,
interface, data structure, etc, this will cause external coupling.
• Data coupling: Use of parameter lists to pass data from one module to another. It is
the dependency that structures create by passing primitive data to each other, for
example, integers. It is a simpler version of data-structured coupling. Passing too
many parameters to a method is a common symptom of this coupling.
OOP further coupling types

• Inheritance coupling: is-a coupling.

• Abstract coupling: coupling to abstract classes. Can be both is-a and has-a
coupling (polymorphism).

You might also like