Advertisement

Global Variables

Started by November 22, 2018 03:41 AM
11 comments, last by Gnollrunner 6 years ago
On 11/22/2018 at 5:49 AM, Gnollrunner said:

I usually try to figure out which option makes the most people angry and go that direction ?

It does remind me to a certain extent of a religious war. When I first started programming I used globals. When I started programming seriously I stopped using them, because they were 'mmm bad, okay'. Then someone older and wiser pointed out that no, in fact in some circumstances they make perfect sense, and may be a really good way of solving a problem, and now I tend to use them again (in somewhat convoluted fashion).

My view is I will use what I regard as the best tool for the job in hand. For instance making a couple of small games in unity and godot, and not knowing their languages very well, globals come to the rescue and gets the game finished quickly.

If I'm writing a document view app which can handle several documents open at a time, I'll make greater use of things like contexts, and objects to be operated on passed by reference (although you can still use a somewhat global mechanism within document / view by relying on the fact you can only have one current document at a time).

Games in particular are often crying out for some kind of global access, more so than many other apps, which is why purists have more difficulty justifying not using it. For instance, you want to play a sound, are you going to pass around a sound object / context through every function just in case you want to play a sound, or simply use a global function? 

To me there are a lot of 'it depends' involved in how you access things. Are you working in a team (where people are more likely to screw things up when given global access), are several threads likely to be trying to change globals at the same time, how easy is it to keep track of dependencies etc etc. Another issue which often needs explicit attention is order of construction / destruction, especially in c++.

There are of course lots of ways of dressing up global access to make it more palatable. Personally I won't use singletons (unless forced to by a language that does not properly support globals), because I regard singletons as a 'solution in search of a problem', and I've seen some really bad implementations, and they are often used by people trying to convince themselves they are not using globals.

On the other side of the coin you do get people who will go to the ends of the earth to avoid using any sort of global access, and end up often using hugely verbose chains of function calls passing around 'stuff', which can make minor changes to one of these parameters a major undertaking, and often are in search of engineering perfection but never finish their projects. However I will say that the bigger the project / team there can be a bigger payoff from being really careful with access and the extra housekeeping quickly becomes worth it.

16 minutes ago, lawnjelly said:

My view is I will use what I regard as the best tool for the job in hand.

Yup. I think that's a fair statement.  Everyone should use what they think is best for the given job. I do on occasion use singletons in C++ because they sometimes solve the initialization sequence problem.  The down side is, if you use them that way you typically need an if every time you get your instance. The problem is in C++ the order of global initialization between different translation units is undefined so if you need to start up various systems in a certain order you have to do something.  There is always the hack of putting all the relevant globals in one file.  I've used that too.  I personalty think if there is some fairly commonly used thing that someone is telling you "never use no matter what!", it might not be the best advice. I even use gotos from time to time.

This topic is closed to new replies.

Advertisement