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

Bienlein jeti789 at web.de
Wed Jan 26 15:47:06 UTC 2022


On Tuesday, 25 January 2022 at 09:53:25 UTC, rempas wrote:
> 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.

I once worked on a system (written in Java) that was a huge 
commercial success. The company has a revenue of about €100 
million from it. What later turned out should always have been 
passed on to methods as a parameter was always a global function. 
Now many person years have been spent on development and 
countless lines have been written and there is absolutely no way 
to fix this as the effort in time and money is way to large. For 
some little programs you might get away with some globals, but 
not in case of lage systems.


More information about the Digitalmars-d mailing list