Global variables, yes or no?
Global variables, yes or no?
If they didn’t exist in a language would you be upset? Any obvious things that can’t be done without global variables? I don’t think there is, but I could have missed something. I know that Haskell doesn’t have them and I think some newer ones like V also don’t have them.
global variables are fine for things like config where you only change them from specific parts of the code but read them from many parts. there's really nothing specific that you can't do without them just that passing some information can get tedious. i feel like the whole "NO GLOBAL VARIABLES WHATSOEVER" comes from a pure object oriented design and i just never vibed with that. rigid rules are usually not a good idea.
Global constants make a lot of sense, those could be easily copied to where they need to go at compile time. The issue I’m having is that, if I allow global dynamic variables then it means having to allocate some sort of table or block for the global variables and if they can allocate more at runtime or even worse they try allocating strings or arrays at runtime memory will fragment and become a mess quickly
I will sometimes create a global caching object, but I typically program in Python, so having the cache allocated ahead of time isn't a big deal.
Even so I usually put a limit on the global cache unless I know exactly how big it's gonna be. I have a pretty large automation suite that requires tool scripts to be instantiated, and that initialization for each tool object can take up to 10 seconds. The system that runs the scripts is constantly creating and destroying the tool objects, so I had to cache them and hijack the init method to pull from a global cache or it would take up to a minute for the toolbox to load.
Since the initialized information (database connections and schema maps) is unlikely to change during a session, it was a good tradeoff.
As others said, global constants are totally fine. Especially for anything that requires authentication. I usually assign them on startup by pulling from either a config file or system environment variables.
Python also heavily relies on globals for logging, which is unavoidable if you want to use standard lib and also do logging.