Skip navigation

It is my opinion that globals are necessary __sometimes__.

You just can’t get around the fact that you need them, at times.

You can think of a member variable as actually .. a global variable.. within that class!

The best reasons NOT to use globals are

The problem with global variables is that since every function has access to these, it becomes increasingly hard to figure out which functions actually read and write these variables.

So to understand how the application works, you pretty much have to take into account every function which modifies the global state. That can be done, but as the application grows it will get harder to the point of being virtually impossible (or at least a complete waste of time).
– Brian Rasmussen

It’s better to talk about size of scope than whether or not something is global. “Global” just means maximum scope. Instead of saying “global variables are bad,” I think it’s more helpful to say “minimize variable scope.”

A global variable in a 100-line program has a scope of 100 lines. But a member variable in a 1000-line class has a scope of 1000 lines. The latter may be worse.

John D. Cook

Some say globals are seen as bad because of their bright scope.

References

  1. Are global variables bad?
  2. Global variables: When ARE they acceptable?
  3. ok , global variable is condemned, singleton is despised, what’s the alternative?
  4. When is it ok to use a global variable in C?
  5. ibm.com: Use your singletons wisely
  6. code.google.com: Why Singletons Are Controversial
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: