View Full Version : Globals
This is kinda off topic, but I thought I'd ask the gurus here anyways...
Having just finished my degree where I was told that global variables are a BAD thing I have noticed them being used in NeHe's tutorials.
Being that his tutorials are the best thing since sliced bread - are globals good or bad?? Should I use them or avoid them??
12-15-2001, 08:25 AM
If you are an OO fanatic you should never use global variables, but many times they are useful, it's up to you to decide, if you are making a stand alone application I don't see anything wrong with globals, if you make a library it might be worth the trouble to get rid of them, you do run into the problemthat you kight create a global variable with the same name as an already existing variable, but with C++ this is not a big problem as you get unique names depending on type. but this can be solved with name spaces also.
So if you want to be 100% OO then you should not use them.
Thx, it'd be good to have some other opinions on this - even if they are all the same
12-15-2001, 12:00 PM
Globals should be avoided wherever possible simply because their access is not controlled and are modifiable by any section of your code unless made const. Encapsulation can be used to control access to a variable. In most cases, a resource needed by all parts of an application can be encapsulated in a class, struct, or namespace and delivered as needed without any additional performance cost.
If everyone (all code sections) has write access to a resource and a problem develops with respect to that resource, how do you track down the culprit? In a large scale application that can be incredibly difficult to do. If only one code section has write access to it and a problem develops, the source of the problem can quickly isolated.
Another benefit of encapsulation is that you can quickly change the manner in which a resource is produced and completely shield the clients of the code section from the changes you made ... that means those using the resource do not have to alter their code at all.... this helps to build higly maintainable code.
There are many other reasons why encapsulation is a good thing. I recommend that you read up on it if you are not familiar with it.
The downside to heavy encapsulation is that it takes a little more time to write the associated code. Many times, that the reason that programmers decide to break encapsulation and go global. I suppose that is why Jeff Moleffe used them in his tutorials. They are not large scale applications, they are intended as tutorials, and he write a tremendous amount of code for the community. If he put in the extra time to encapsulate everything, we would have a LOT less material coming from him. Since these are small scale applications, the lack of encapsulation does not pose much of a problem.
His tutorials are by far the most complete reference to OpenGL programming that I have found. I took his base code as a starting point and encapsulated all of it, added a great deal to it, and now I have a game engine that I can use to quickly build applications. It is relatively easy to debug too.
Just my $0.02
[This message has been edited by Iceman (edited 12-15-2001).]
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.