Global Variables

Started by
13 comments, last by szecs 12 years, 9 months ago
If I have some variables, that are used often, like pointer to graphic context, Display Window, resources reference, is ok to put all these inside a header file and include it in a precompiled header ?

In general, global variables could/should be used in these situations:

1. Constant valuesstatic int const GAME_VERSION = 0x00000000;
2. Values that are initialized once and don't change while the program is runningstatic HINSTANCE inst; // initialized once at the beginning of the program
Variables that may be read/accessed in several different sections of the code but only changed/updated in one (like the screen resolution) could be declared global, but if you do so make sure your program architecture ensures that these sections are not executed simultaneously.
Advertisement
There's only a few reasons to use a global:

1) You're lazy and can't figure out a better design yet.

2) Constants.

3) You're in a situation where you absolutely have no alternative but to use a global. Which is exactly almost never.

You currently fall under case #1. Laziness is ok sometimes if you don't care about people looking over your shoulder and chastising your lack of perfection. Just roll with it. You're not going to be holding this code up on a pedestal 2 years from now, marveling at it's perfection, so just go with your gut and learn from it later.

There's only a few reasons to use a global:

1) You're lazy and can't figure out a better design yet.
2) Constants.
3) You're in a situation where you absolutely have no alternative but to use a global. Which is exactly almost never.

You currently fall under case #1. Laziness is ok sometimes if you don't care about people looking over your shoulder and chastising your lack of perfection. Just roll with it. You're not going to be holding this code up on a pedestal 2 years from now, marveling at it's perfection, so just go with your gut and learn from it later.


That is an amazingly elitist statement.


Many things are global for good reason.

There is a global memory allocator, provided by the system. You can provide more narrowly-scoped versions, but there is a global one that is created for you by the system. Would you remove it from the language? How would you replace it?


There are global IO streams, provided by the system. You do not need to use them, but they are present for all takers. There is a globally used locale object. Would you remove them, and require every program that performs even the most simple I/O to construct locale objects, and have every stream get imbuded with this custom object?

Calls to kernel control, hardware control, iocontrol, and other system settings are global settings. Any thing with scope bigger than just your program is effectively global to your program. Good luck removing them.

Many programs will create a general purpose logger and create a global instance for general use. Many programs will create per-frame performance data and update information, and make it available globally for general use to any system that needs to know. Many programs will have a global object with pointers to various systems and managers since those systems truly need to be known and used across the program.

Many times a class will have need of a general purpose object that specifies behavior that can be shared but can also be replaced if necessary. Such a shared object can often be best implemented as a global object.




Yes, when used incorrectly global objects will increase code complexity and module interdependence. Don't do that.

However, used properly, global variables can dramatically reduce code complexity. That's when you use them.


A hammer is a tool that when used incorrectly can cause serious problems. But used correctly it is a great tool.
Fire is a tool that when used incorrectly can cause serious problems. But used correctly it is a great tool.
Surgery is a tool that when used incorrectly can cause serious problems. But used correctly it is a great tool.
Dynamite is a tool that when used incorrectly can cause serious problems. But used correctly it is a great tool.

Similarly, so it is with global objects.



Yes, there are many reasons to not use globals. Globals lose locality controls, and they can have issues with access control and constraint checking (although they don't have to if designed properly). Globals can have concurrency issues, and they can introduce some issues for testing. Used improperly they suffer from side effects. Indiscriminate access to a global variable can seem to show very odd functionality, but this is always true: if you start mucking around with the stdout stream and changing its properties you can expect problems from anyone using the stdout. If you start mucking around with the global locale you can expect all your streams changed based on the locale settings. The problem isn't the existence of the global object, the problem is the misuse of the object. The answer is simple: Don't do that.

However.... Globals are great when they represent things that truly are available everywhere in the program.


If that is the case, and a global variable is the answer to your problem, don't force your code into a solution that doesn't fit.

If the right solution is a global variable then use it without guilt.
That is an amazingly elitist statement.
I'd prefer "facetious" (I wasn't using the mathematical definition of almost never), but feel free to get up in arms and write a page-long retort if you like.

There is a global memory allocator, provided by the system.[/quote]And using it is a bad idea born out of laziness laugh.gif
I agree with Dragonion. If a variable is initialized once at startup then never-to-be-changed again (these are so obvious, only idiots would try to alter them, and you can't make a code idiot-proof anyway) and a lot of stuff uses (reads!) them, I'd use globals instead of passing around all the shit just to avoid using globals. The hInstance and hWnd of the main window can be examples in most cases. Of course, you can dump everything in a struct and pass it around, but I really see no point in passing it around.

Of course, use strict rules, good naming, etc.

Disclaimer: I'm a tinker and I work alone. And my knowledge is so poor, that I was never able to produce sane code without globals. Especially when making tools/editors with lots of overlapping states.

This topic is closed to new replies.

Advertisement