Professional Documents
Culture Documents
Week 6 4coupling Final
Week 6 4coupling Final
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.
# 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
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.
# Module B
def print_x(x):
print(x) # no common coupling
# 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
• 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
• 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
• Abstract coupling: coupling to abstract classes. Can be both is-a and has-a
coupling (polymorphism).