Why people dislike global variables so much while I find them so convenient?

rempas rempas at tutanota.com
Wed Jan 26 11:08:21 UTC 2022

On Tuesday, 25 January 2022 at 15:47:12 UTC, Ali Çehreli wrote:
> On 1/25/22 01:53, rempas wrote:
> > people dislike global variables
> The situation is much better in D because I suspect what you 
> call global is D's module-scope and thread-local.
> (Note: I don't want to argue whether thread-local by default 
> was a good decision or not but I certainly take full advantage 
> of the ease of thread-local variables in D.)
> So, module-scope variables are just fine: std.parallelism uses 
> a default ThreadPool object, std.stdio functions use stdout, in 
> this case a truly global object, etc.
> > When I have a variable that I must pass down to 5-6
> > functions, I find it much easier to make it global rather
> than having it
> > been passed in all the functions that need it.
> I've used that method once, which I mentioned at this point 
> during a presentation:
>   https://youtu.be/dRORNQIB2wA?t=2946
> Both you and I realize that a module is an object and in our 
> case there is a single object of it. That works.
> When you need more than one object (more than one context), 
> then you move all those variables to a user-defined type and 
> they become proper member variables.
> > global variables can mess me up
> Otherwise, as everybody else told, global variables can be very 
> dangerous:
> https://www.safetyresearch.net/toyota-unintended-acceleration-and-the-big-bowl-of-spaghetti-code/
> That report mentions "thousands of global variables", all of 
> which I bet started as "what can go wrong?"
> Ali

As always, thank for the great info Ali! Wish you an amazing day!

More information about the Digitalmars-d mailing list