Global variables are not the problem
codestyleandtaste.com
Global variables are not the problem
1–10 of 165 posts
Re: Global variables are not the problem
#2Singletons if you must. At least you can wrap a mutex around access if you're trying to make it thread safe.
Re: Global variables are not the problem
#3Please no. Singletons if you must. At least you can wrap a mutex around access if you're trying to make it thread safe.
Re: Global variables are not the problem
#4Re: Global variables are not the problem
#5Re: Global variables are not the problem
#6The real underlying problem is 'Spooky action at a distance'; it's not an issue that is specific to global variables. If you pass an instance between components by-reference and its properties get modified by multiple components, it can become difficult to track where the state changes originated and that can create very nasty, difficult-to-reproduce bugs. So this can happen even if your code is fully modularized; the issue is that passing instances by reference means that the properties of that instance behave similarly to global variables as they can be modified by multiple different components/files (without a single component being responsible for it).
That's partly where the motivation for functional programming comes from; it forces pass-by-value all the time to avoid all possibility of mutations. The core value is not unique to FP though; it comes from designing components such that they have a simple interface which requires mostly primitive types as parameters. Passing objects is OK too, so long as these objects only represent structured information and their references aren't being held onto for future transformation.
So for example, you can let components fully encapsulate all the 'instances' which they manage and only give those parent components INFORMATION about what they have to do (without trying to micromanage their child instances); I avoid passing instances or modules to each other as it generally indicates a leaky abstraction.
Sometimes it takes some creativity to find a solution which doesn't require instance-passing but when you find such solution, the benefits are usually significant and lasting. The focus should be on message-passing. Like when logging, the code will be easier to follow if all the errors from all the components bubble up to the main file (e.g. via events, streams, callbacks...) and are logged inside the main file because then any developer debugging the code can find the log inside the main file and then trade it down to its originating component.
Methods should be given information about what to do, they should not be given the tools to do their job... Like if you catch a taxi in real life, you don't bring a jerrycan of petrol and a steering wheel with you to give to the taxi driver. You just provide them with information; the address of your desired destination. You trust that the Taxi driver has all the tools they need to do the job.
If you do really want to pass an instance to another instance to manage, then the single-responsibility principle helps limit the complexity and possibility for spooky action. It should only be passed once to initialize and then the receiving component needs to have full control/responsibility for that child. I try to avoid as much as possible though.
Re: Global variables are not the problem
#7I find the concept of a context structure passed as the first parameter to all your functions with all your "globals" to be very compelling for this sort of stuff.
Re: Global variables are not the problem
#8Please no. Singletons if you must. At least you can wrap a mutex around access if you're trying to make it thread safe.
> wrap a mutex
What if my program has one thread? Or the threads have clearly defined responsibilities?
Re: Global variables are not the problem
#9Re: Global variables are not the problem
#10Please no. Singletons if you must. At least you can wrap a mutex around access if you're trying to make it thread safe.
Why are singletons better? That's just a global object. > wrap a mutex What if my program has one thread? Or the threads have clearly defined responsibilities?