Globals usage in tutorials and elsewhere.

Started by
20 comments, last by Hodgman 10 years, 5 months ago

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.

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.

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

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 :)!

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

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".

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

Your Brain contains the Best Program Ever Written : Manage Your Data Wisely !!

Global loop conters? Yikes-a-mundo, yikes on a bike, doing wheelies.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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

That's not good, that's not good at all!

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

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.

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)

f@dzhttp://festini.device-zero.de

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.

This topic is closed to new replies.

Advertisement