Becoming the Best Software Engineer: Pythonic Common Coupling

Spread the love

In this blog post, you will learn about what common coupling is, and why it is important to be aware of. Also, you will see a Python script that exemplifies common coupling, and you will learn how to avoid it so that your code is amazing.

This is the fifth blog post in a series on coupling in Python. You can read my previous blog post in the series Becoming the Best Software Engineer: Pythonic External Coupling.

What Is Common Coupling and Why Is It Important?

Common coupling, also knowns as global coupling, occurs when modules share global data, such as a global variable (Ingeno, 2018). Common coupling is a very high form of coupling, which should be avoided at all costs, and if unavoidable, minimized. This is because it can make code very difficult to test and maintain over time.

An Example of Common Coupling

The following Python script demonstrates common coupling:

# Global Variable
GRAVITY : float = 9.8

# Calculate weight in Newtons
def earth_weight(mass : float) -> float:
    return GRAVITY * mass

def main():
    mass : float = 10.0
    weight : float = earth_weight(mass)

    print(f"Earth Weight: { weight } N")

if __name__ == "__main__":
    main()

In this case, “GRAVITY” is a global variable that is shared between the “earth_weight” “function” and “main” “function”.

You might be thinking “what’s the big deal?” The following is a Python script with a slight modification to the script, which will shed light on why sharing global data can lead to unexpected consequences:

# Global Variable
GRAVITY : float = 9.8

# Calculate weight in Newtons
def earth_weight(mass : float) -> float:
    return GRAVITY * mass

def main():
    global GRAVITY
    GRAVITY = 3.71
    mass : float = 10.0
    weight : float = earth_weight(mass)

    print(f"Earth Weight: { weight } N")

if __name__ == "__main__":
    main()

Now, the main method sets the “GRAVITY” global variable to 3.71, which is the gravity on mars, so the return value of the “earth_weight” function is wrong. In this small-scale example, it’s easy to see that the “earth_weight” function will return the wrong value, but imagine if there were 10,000 lines of code, which would make it much more difficult.

How Do You Avoid Common Coupling?

In this example, common coupling can be completely avoided by moving the “GRAVITY” global variable inside the “earth_weight” function, so the code becomes the following:

# Calculate weight in Newtons
def earth_weight(mass : float) -> float:
    GRAVITY = 9.8
    return GRAVITY * mass

def main():
    mass : float = 10.0
    weight : float = earth_weight(mass)

    print(f"Earth Weight: { weight } N")

if __name__ == "__main__":
    main()

Now, the “earth_weight” function cannot be manipulated by external code, and it will not return unexpected and incorrect values.

In this blog post, you learned what common coupling is, why it is important, and how to avoid it in your code.


If you found this blog post to be valuable, then you should consider doing the following:

Subscribe

* indicates required

Intuit Mailchimp


References

Ingeno, J. (2018). Software architect’s handbook. Packt Publishing.


Posted

in

by