Sign in to follow this  
NogginBoink

Use of Global Variables

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by pragma Fury
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.

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 this post


Link to post
Share on other sites
Quote:
Original post by NogginBoink
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.


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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this