Global Scale Variables?

Hey All, why is having variables on a global scale bad, as ben mentioned in lecture 26? I’m not sure why, and I’ve been putting almost all if not all of my variables on a global scale this whole time. Also, I can’t find the code from this specific section so that I can review where different variables are going to go when I switch them to Local Scale.

Edit: Nevermind, I found all the locations in the video.

The problem with global variables is that since every function has access to these, it becomes increasingly hard to figure out which functions actually read and write these variables.

To understand how the application works, you pretty much have to take into account every function which modifies the global state. That can be done, but as the application grows it will get harder to the point of being virtually impossible (or at least a complete waste of time).

If you don’t rely on global variables, you can pass state around between different functions as needed. That way you stand a much better chance of understanding what each function does, as you don’t need to take the global state into account.

are-global-variables-bad

Privacy & Terms