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

rempas rempas at tutanota.com
Tue Jan 25 09:53:25 UTC 2022


It is known that people dislike global variables and the reason 
is that they make the code harder to debug. In my experience tho, 
it is the exact opposite. 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. This practice also makes my function signatures looking much 
cleaner. Yeah, one variable will not make a difference but in my 
project, I have about 2-3 variables that need to be passed down 
to a lot of functions so I only think that it makes sense to use 
them as globals. Another problem is the case that I'll introduce 
a new variable that needs to also be passed in most of my 
functions. What happens then? Let's say I will have 20 functions 
at the time. I have to change both the function signature and all 
the other places in code that call this function. The latter can 
be easily done with a quick "search and replace" in my text 
editor but still, it's a tedious thing to do.

So can someone make examples about how global variables can mess 
me up. I know that probably everyone here has more personal 
experience than me so I really want to learn why global variables 
are considered so harmful.


More information about the Digitalmars-d mailing list