Function Call
def secondary(data):
# Do stuff.
def main():
...
secondary(data)
...
Simplicity:
As simple as possible. The function call is the connection. The only coupling is a direct connection to an explicit module.
Replaceability:
A new function alternative() must be written. Then changing the coupling simply requires changing the function call in main():
# def secondary(data):
# # Do stuff.
def alternative(data):
# Do different stuff.
def main():
...
# secondary(data)
alternative(data)
...
- Write new replacement function.
- One-line change in
main(), to call new function.
Method Call On Class
class SomeStupidClass:
@staticmethod
def secondary(data):
# Do stuff.
def main():
...
SomeStupidClass.secondary(data)
...
Simplicity:
Almost as simple as possible. The class simply acts as a namespace for the secondary() function. In some situations, having such a namespace may tidy up the code and make it more readable.
The method call is the connection. The only coupling is a direct connection to the explicit method of a class.
Replaceability:
This depends on whether the new alternative function lives in the same namespace class, or needs its own new class.
Like this:
class SomeStupidClass:
# @staticmethod
# def secondary(data):
# # Do stuff.
@staticmethod
def alternative(data):
# Do different stuff.
def main():
...
# SomeStupidClass.secondary(data)
SomeStupidClass.alternative(data)
...
Or this:
#class SomeStupidClass:
#
# @staticmethod
# def secondary(data):
# # Do stuff.
class LessStupidClass:
@staticmethod
def secondary(data):
# Do stuff better.
def main():
...
# SomeStupidClass.secondary(data)
LessStupidClass.secondary(data)
...
- Write new replacement method or class.
- One-line change in
main(), to call different class or method.
Object Instantiation, Then Method Call On Object
class SomeStupidClass:
def secondary(self, data):
# Do stuff.
def main():
...
stupid_object = SomeStupidClass()
stupid_object.secondary(data)
...
Simplicity:
No longer as simple as possible, but still straightforward. There is the extra overhead of having to instantiate the container class.
The coupling now has two elements: instantiating the object, and calling the method on the object. If both are done in the same place, then the coupling is trivial to understand. However, if instantiation is done in a very different place to the method call, then the reader has to mentally keep track of both parts of the coupling to properly understand how Module A depends on Module B.
Replaceability:
Swapping functionality requires either a new method on the existing class, or a new class.
Like this:
class SomeStupidClass:
# def secondary(self, data):
# # Do stuff.
def alternative(self, data):
# Do stuff better.
def main():
...
stupid_object = SomeStupidClass()
# stupid_object.secondary(data)
stupid_object.alternative(data)
...
Or this:
# class SomeStupidClass:
#
# def secondary(self, data):
# # Do stuff.
class LessStupidClass:
def secondary(self, data):
# Do stuff better.
def main():
...
# stupid_object = SomeStupidClass()
stupid_object = LessStupidClass()
stupid_object.secondary(data)
...
- Write new replacement method or class.
- One-line change in
main(), to call new method or instantiate new class.
🙠