Public Group

# Use of Global Variables

This topic is 4892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

As a newcomer to the 3d programming world, I'm curious to know how many of you use global variables (and how many, and for what) in your programming. I'm trying to minimize the number of globals in my program as a matter of good programming practice, yet even at the very outset it's clear that there would be many advantages to using globals. I'd appreciate feedback from y'all on the challenges you've had trying to minimize the number of global variables in your apps, and the cases in which you've found that the use of globals is a practical necessity. Thanks!

##### Share on other sites
Globals, or at least 'globalizing techniques' like the Singleton pattern, are inevitable. As a rule of thumb, try avoiding them until they become necessary. If too much globals are necessary, review your design and look for a way to refactor the code.

The "don't use globals" mindset is overrated and often leads to nightmarish code that is much, much, much worse than an innocent global. "Don't use globals unless they are necessary" is what you are after.

##### Share on other sites
Hi,

I use a global variable if it is needed to be used in almost all of my code. You could also use defines for certain things, but I prefer the global. In my current project I use a global bRenderCollisionData which renders all the collision data with the models. This is the only global variable in the whole project, but it makes sense to me to make it global rather than have it in some kind of singleton or monostate class which would only complicate the issue. However each case is different and in your case it may vary. You would have to evaluate if you should be using it or not and how it will affect the program if used incorrectly.

This is my opinion.

##### Share on other sites
Personally, The only time I consider the use of globals as acceptable is when they are constants. Requiring a modifiable global for program execution suggests bad design to me. (though there are times when your hands are tied)

Things to consider about globals are:
1. globals are inialized in the order they are declared within a file. That is:
int x = 5;int y = 2;int z = x*y;

since x and y are already initialized, z will = 10.
however, there is no guaranteed initialization order of globals in different files, so if x and y were declared in one file, and z in another, you may get an exception. And to make it worse, it's impossible to catch that exception.
And there are many more issues when dealing with dlls.. in short, avoid compliated global dependancies.

2. Due to the nature of globals, working with them in a multithreaded environment may lead to problems

As xMcBaiNx mentioned, avoid them if you can.

##### Share on other sites
Quote:
 Original post by pragma FuryPersonally, The only time I consider the use of globals as acceptable is when they are constants. Requiring a modifiable global for program execution suggests bad design to me.

The same goes from a performance point of view. Using non-constant globals means they have to be updated in memory all the time, because you never know when another function is going to need it. non-globals or global constants can just be kept in registers, which shaves off a good chunk of clock cycles :)

But I agree, the main thing is the design issues :P

##### Share on other sites
Quote:
 Original post by NogginBoinkI'm trying to minimize the number of globals in my program as a matter of good programming practice, yet even at the very outset it's clear that there would be many advantages to using globals.

What are the advantages? I can think of only one - speed of implementation. It's far quicker for me to create a global than to do the so-called right thing. For that reason, I almost always start to implement a new feature with globals. But when the work is done, I move the globals to better places, like inside a function, or a class.

It's a bit like any sort of work really - you make a mess in the process of doing the work but when you're finished, it's worth clearing up after yourself to make life easier in the future.

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5

• 13
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632949
• Total Posts
3009412
• ### Who's Online (See full list)

There are no registered users currently online

×