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

Patrick Schluter Patrick.Schluter at bbox.fr
Tue Jan 25 17:10:40 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/
Toyota, lol. I also had once to interact with software coming 
from them. It was surpzising to say it politely. It was a order 
system for suppliers of parts. The orders were tickets the 
suppliers had to scrape from a website. The very uncommon feature 
of the site was that it checked credentials (username/password) 
for the connection but didn't check if the client managed to read 
the ticket or not. Any read attempt of the tickets url would 
delete it on the server. The problem was that there was no 
"developer" server, only the real production server and I had to 
write the client software without losing orders as these were 
real orders of parts the supplier. It was impossible to negotiate 
with T. that there system was stupid. I managed to only lose 4 
orders which amounted to several thousands of euros.

More information about the Digitalmars-d mailing list