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

Ali Çehreli acehreli at yahoo.com
Tue Jan 25 15:47:12 UTC 2022

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 


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:


That report mentions "thousands of global variables", all of which I bet 
started as "what can go wrong?"


More information about the Digitalmars-d mailing list