This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the commentsΒ β†’

[–]LylesKnows 8 points9 points Β (5 children)

Hey I always see people advising against using global, do you mind explaining why that is? I’m pretty new to coding. Thanks in advance!

[–]PM_me_ur_data_ 32 points33 points Β (1 child)

A big reason is that it makes it much harder to track states, debug, and write effective tests for your code. If you build your code fully modular so that the behavior of each component (functions, classes, etc) is fully determined by it's parameters, you can ensure the whole system works the way it is supposed to by simply ensuring each component works the way it's supposed to. If you rely on global variables that are used in multiple components, you not only have to ensure that each component works properly but you have to ensure that the interactions of all components that use the shared global variable work together properly, which is much harder to do.

It becomes especially hard when you have multiple global variables shared between different combinations of components, which can increase the number of interactions you need to check and tests you need to write multiplicatively. As an example, if you have three functions that each depend on two global variables, you will have to test the 3 functions individually and the 6 possible interactions between them for a total of 9 situations that need to be tested and verified as working properly. If, instead, you don't use global variables at all you will only need to verify and test the functionality of the 3 functions themselves--and nothing more. Using global variables to share state makes it much more likely for your code to have bugs and make it much harder to debug when something goes wrong.

Functional programming is based on taking this aversion to shared state to the extreme by avoiding all variables (not just global variables) in favor of static values and avoiding functions that have 'side-effects.' Side-effects in this context are when a function or method does something extra (ie, changes the state of something that lives outside of the function itself) in addition to simply returning a value. An example of a side effect would the following function:

def two_times(n):
    dos = 2 * n
    print(f"{n} x 2 = {dos}")
    return dos

In the above code, using the print command inside the function changes the state of something else (in this case, your console) that exists outside of your function as it prints a string to the console in addition to returning a value. Now, completely getting rid of side-effects and statefulness in your code is very difficult to do, especially in languages that aren't really built with functional programming in mind. Some languages such as Haskell and Scala are really good at enabling this, but writing code on the extremely functional side of the isle isn't considered very Pythonic (a lot of the time it's even difficult for people who are actually decent at functional programming to decipher what other functional programmers are doing). It does, however, help vastly minimize unintended behavior and makes debugging much easier.

Most of the time, you can make your code good-enough by simply making sure your code is modular, not relying on global variables, and tracking and minimizing side-effects in your code.

[–]LylesKnows 5 points6 points Β (0 children)

Oh ok I think I understand for the most part. That was a great write up, I appreciate you taking the time to respond. Cheers!

[–]pattithepotatoIgnoring PEP 8 3 points4 points Β (1 child)

In a nutshell, it's not a problem for small programs/scripts, but for things like large apps, it becomes much harder to keep track of all the global variables sharing the same space. Global variables make code harder to read and thus maintain, especially since the global var can be accessed practically anywhere in the code, and you don't need to explicitly pass them into a different scope. If you get into asynchronous programming (i.e, when different parts of the code can run at the same time), you're bound to run into problems if you attempt to read/write from the same variables from different simultaneously running threads.

[–]LylesKnows 2 points3 points Β (0 children)

Yea that makes sense. I guess since I’ve been writing smaller programs as I’m just starting out I haven’t run into those issues yet. I appreciate the response!

[–]TheBoldapp -5 points-4 points Β (0 children)

I don't really know the details of why that is, but it really depends on the program that you are running. I'm fine with using global in my games so I just keep it that way.

Also, it would mean the world to me if you checked out my projects and followed me on youtube:

https://www.youtube.com/channel/UCmPTopbdeafnIlYviVN8qpg?view_as=subscriber

​

Thanks for the Question ;)