Archived

This topic is now archived and is closed to further replies.

BSXrider

Globals

Recommended Posts

Are they really as bad as everyone makes out? Do you go any lengths to avoid every possible global? I have all that in one of my project files:
  
GL_Window*	g_window;
Keys*		g_keys;



/////////////////////////// CAMERA POSITION

GLfloat eyeX=0;
GLfloat eyeY=0;
GLfloat eyeZ=32;

/////////////////////////// CONSTANTS FOR TIME FUNCTIONS

LARGE_INTEGER tickspersecond;
LARGE_INTEGER lasttime;
LARGE_INTEGER timenow;
      __int64 diftime;
          int FPS = 1;

/////////////////////////// BALLS & PLAYERS

const int numballs   = 100;
const int numplayers = 2;
Ball	balls	[numballs];
Player	players	[numplayers];
const float speed=0.01f;

////////////////////////// TEXTURE STUFF

GLuint       texture[1];
AUX_RGBImageRec *Texs[1];
GLFont       fNehe;
  

Share this post


Link to post
Share on other sites
In my experience, it''s pretty much unavoidable. (and please don''t tell me I just don''t know how to write good OO code)

For example, just yesterday I wanted to return an exit value from an MFC app back to the shell. In order to do this you have to override CWinApp::ExitInstance() and return your value. How does this function know what the error level should be? It has to be in some globally accessible place...

It is often helpful, however, to wrap related global information in a nice class. Then use something like g_App.GetWindow() and g_App.GetErrorLevel(). That makes it easier to manage and safer.

Share this post


Link to post
Share on other sites
Well, when doing some engineering subjects at uni and computing in high school, and according to probably every book published, global variables are a bad thing.

The basic reason is that you may get confused.

as said above, if you have a global called "int a", but later on you accidently make a function which has "int a" in it, the global one is ignored. If you don''t know what you are doing (or the person who is reading it has no idea what you are doing) then it may lead to problems.

Meanwhile global variables only need to make stack space once so it makes if much faster in frequently called functions, and in general they perform faster aswell.

Just make sure you make the names obvious so you know what the hell it is.



Beer - the love catalyst
good ol'' homepage

Share this post


Link to post
Share on other sites
If your project is only 3 or 4 cpp files and their headers, well, I don''t worry about globals. But if its larger, or I''m working as part of a team, or someone else is going to have to use my code, I always try to minimize my use of globals.

The reason that globals are bad is that you start having namespace conflicts when the project starts getting large and many people are working on it at the same time. They are also bad, because they''re really hard to debug. If the global variable has the wrong value and you want to find out why, and you have to search through 100 cpp files to find all the places the global gets access and modified ... yuck!

When I do use globals (because I don''t think they fit in a singleton or because of performance issues), I try to give them unique names by prefixing them with an abbreviation, like MD2_SCALE_FACTOR, MD2_NUM_ANIMS, MD2_FORWARD_VEL, MD2_CROUCH_VEL all constants in my MD2Model source file. You can also use namespaces.

So, no globals aren''t all bad. They can be faster in some cases than class members. But I think its a good idea that you learn to program with as few globals as possible. Especially if you plan to have a job later that involved programming. Learning how to program without globals isn''t easy





Share this post


Link to post
Share on other sites
quote:
Original post by Pyabo
In my experience, it''s pretty much unavoidable. (and please don''t tell me I just don''t know how to write good OO code)



You don''t know how to write good OO code. Using the Singleton Pattern, the need for globals can be totally irradicated.

Share this post


Link to post
Share on other sites
You should work to avoid globals to keep your code looking clean but it's not essential unless working in large groups. If you're just starting it's best to focus on getting things done and then work on finding ways to neaten up your code.

You can't build a mansion before building a few shacks so don't worry about it. You'll just get better the more you code.

Ben

[The Rabbit Hole | The Labyrinth | Programming | Gang Wars | The Wall]

Edited by - KalvinB on February 6, 2002 2:48:51 AM

Share this post


Link to post
Share on other sites