Searching for a good article or discussion on globals (and how to avoid them)

Started by
13 comments, last by Kwizatz 16 years, 3 months ago
Quote:Original post by jpetrie
Quote:
Aren't static member variables the solution to globals? just wondering.

No, they're no different.


Exactly, but static member variables at least aren't exactly orphans, no need to redeclare them as extern in whatever source file you need access to them, and they move along with the class definition in case of code reuse.

I guess its a matter of practical evilness vs technical evilness? usability vs concept?
Advertisement
Quote:Original post by Kwizatz
Quote:Original post by jpetrie
Quote:
Aren't static member variables the solution to globals? just wondering.

No, they're no different.


Exactly, but static member variables at least aren't exactly orphans, no need to redeclare them as extern in whatever source file you need access to them, and they move along with the class definition in case of code reuse.

I guess its a matter of practical evilness vs technical evilness? usability vs concept?


No, it just means you might save some typing while implementing the spaghetti.
Quote:
Exactly, but static member variables at least aren't exactly orphans, no need to redeclare them as extern in whatever source file you need access to them, and they move along with the class definition in case of code reuse.

I guess its a matter of practical evilness vs technical evilness? usability vs concept?

No. They're just not solutions. Who cares if they move with the class, or are "orphans" -- what's an orphan? A static variable is basically a global in a namespace, plus some other language-related fluff and constraint, nothing special. They have the same problems. If static variables exhibit the same deep-rooted problem that globals have, how can you consider them a "solution" in any sense of the word?

They just have slightly different implementation flavor.
Quote:Original post by Kwizatz
Quote:Original post by jpetrie
Quote:
Aren't static member variables the solution to globals? just wondering.

No, they're no different.


Exactly, but static member variables at least aren't exactly orphans, no need to redeclare them as extern in whatever source file you need access to them, and they move along with the class definition in case of code reuse.

I guess its a matter of practical evilness vs technical evilness? usability vs concept?

Those differences are all trivial though. The real problems (as stated above) are dependancies and coupling and a static member variable isn't any different.

Static variables within a function, however, can be significantly different. But they come with their own set of problems.
Ok, got it, I still think they do have their uses (for example to keep a colection of object references), though obviously using globals for something arguments suffice for is indeed a bad practice.

This topic is closed to new replies.

Advertisement