Jump to content
  • Advertisement
Sign in to follow this  
Mercury Filter

Globals usage in tutorials and elsewhere.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been reading the OpenGL Programming Guide, and in it they justify usage of global variables in tutorials on the grounds that they are just small programs in the examples. The question I have is if there is some piece of information about global variables that I don't understand that would justify that? I am familiar with scope, but I am trying to understand why you would risk the possibility of teaching something that is considered to be bad practice (as far as I have ever heard; also, I realize that the book I am referring to is not to teach programming, but OpenGL), when you could just avoid using the globals?

So in essence, is there a difference that I simply am not understanding that makes their use appealing for such tutorials?

 

When I took a course in computational physics, the professor seemed to love global variables, and it took me a bit to get past that, as I had read often that one should avoid them. Every C program that we looked at in class had almost every piece of information related to the values of the elements of the simulation placed in variables that were at the top of the source file, right below the includes and defines. I believe that I read that C variables are not actually strictly globals in that context, so my usage of the term may be incorrect.

Share this post


Link to post
Share on other sites
Advertisement

I have been reading the OpenGL Programming Guide, and in it they justify usage of global variables in tutorials on the grounds that they are just small programs in the examples. The question I have is if there is some piece of information about global variables that I don't understand that would justify that? I am familiar with scope, but I am trying to understand why you would risk the possibility of teaching something that is considered to be bad practice (as far as I have ever heard; also, I realize that the book I am referring to is not to teach programming, but OpenGL), when you could just avoid using the globals?

So in essence, is there a difference that I simply am not understanding that makes their use appealing for such tutorials?

 

When I took a course in computational physics, the professor seemed to love global variables, and it took me a bit to get past that, as I had read often that one should avoid them. Every C program that we looked at in class had almost every piece of information related to the values of the elements of the simulation placed in variables that were at the top of the source file, right below the includes and defines. I believe that I read that C variables are not actually strictly globals in that context, so my usage of the term may be incorrect.

 

globals aren't problematic for small code samples so for a tutorial that focuses on something other than programming there is nothing wrong with using them.

Share this post


Link to post
Share on other sites

Also, in games you often use the equivalent of Globals, except you keep them encapsulated in an enumeration or namespace. They have real uses in game development, so I wouldn't fret too much.

 

Cheers :)!

Share this post


Link to post
Share on other sites

Globals are problematic when you may have issues of naming, multiple simultaneous accesses and manipulation, variables with a high load of usage in general (globals aren't as fast as a local variable, depending on your compiler), harder to keep track (need a namespace). If those problems with the global variables don't affect you for some reason, they may be useful sometimes.

 

Still, you can use globals for simple code samples, classes and tutorials since they give a huge edge on readability. It would be bad if you had to keep track of lots of references, pointers values, if you added 3-4 parameters to each function, just to avoid a global on a code snippet; where you, btw, wanted to teach something that had nothing to do with globals.

 

For an example, if I want to teach you how to implement a concatenate strings function in C++, it could be useful to use the namespace std globally (string is more readable than std::string), and you could pass less references as parameters to functions, also adding to the readability on a short code.

 

About your question of them being or not global due to their position, someone please correct me if I'm wrong here, it depends on the file type and usage of the code.

If you have a global variable inside a .h header, it'll be available at any place where that header was included. Differently, if you include it inside an implementation file such as .cpp, they'll be "global in that file".

Share this post


Link to post
Share on other sites

OMG - This was just a simple opinion on the use of Global Variables.  I have come across some languages that the only way you could use arrays was to make them global.

This was by the design of the language, not by choice.

  Besides,  to globalize or localize is a personal programming choice in most cases.  Normally dictated by the size of the program and their use.

  But I guess my original comment on this became a game of hangman's Noose

Edited by Poigahn

Share this post


Link to post
Share on other sites

For Example :  int x,y;  // Loop Counters  These I declare up front instead of declaring them in Each function I would use a Counter.

 
I used to do this, thinking it was a clever way to save space, so believe me when I say don't!!! You cannot conceive of every way in which you might call a function, so don't expose its inner workings to the global scope! Good practice in software design is to reduce the affect a piece of code can have on other pieces of code.
 
As an aside I actually ran into this exact problem today. Javascript will put a variable in the global scope if you don't explicitly put 'var', so all of my loops used the same variable "i" unintentionally. Not fun to debug. =)
 

 

I believe that I read that C variables are not actually strictly globals in that context, so my usage of the term may be incorrect.

Variables in C declared outside of any functions are global in scope.

 

I would like to point out to all those who blindly discourage the use of globals, that cout is a global. Globals have their place, but can be very easily misused.

Edited by Segmented

Share this post


Link to post
Share on other sites

Ah yes. Global loop counters are great. If you want slow code that accesses memory rather than keeping the counter in a register and want to see everything go up in flames the moment you add multiple threads into the mix.

 

Simple rule: keep the scope and lifetime of variables as small as possible.

 

-Don't declare stuff at the beginning of functions, especially if they are a) only used way further down or especially b) within a single block or branch.

-Don't make variables members that are only used in a single function or could easily be passed as parameter

-Do not make anything global, unless the alternative would be even worse

-To avoid name conflicts, don't use overly short names on variables with large scope (a global should never be a single letter)

Share this post


Link to post
Share on other sites

I tend to use both Globals and Local Variables.  I use Globals for mundane task which may be used in several Functions through out the coding.
For Example :  int x,y;  // Loop Counters  These I declare up front instead of declaring them in Each function I would use a Counter.


Pretty pretty please tell me it is April 1st..... Or some other joke day, I simply don't believe this was a serious comment. The only way I can read it is as satire.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!