Globals, pros and cons?

Started by
25 comments, last by Mxz 18 years ago
Quote:Original post by Riekistyx
So people who teach this methods are incorrect, wrong, and its never used eh?

I know a place that teachs it heavily and relies on it.

So let me ask you this. Are you in companies saying this to me? Or just some persons who dont prefer singletons.

I cant begin to see how its messy at all. Infact Hungrian notation is probably more ugly to look at than this. So you tell me...


I guess you are speaking about singletons here - I may be wrong.

This is true: people who teach you that you should use singletons instead of globals are plain wrong. Singletons have their roles, but it is uberdifficult to use them correctly - as a consequence, it is a good idea to not use them. Now, I may be wrong, but I believe that my own experience (I have been a software architect for years) is valuable.

Bad use of singleton == use of a disguised global.

Moreover, C++ don't allow you to create perfect singletons (the destruction problem is really annoying) although good enough implementation is generally OK.

You should read Washu's journal (see my sig) - he made 3 entries about singletonisis last year (surf his journal, you'll find them).

And Hungarian notation, when used correctly, is incredibly useful. See Joel on Software's essay about this.

Regards,
Advertisement
I think it's safe to say that you should keep your global variables to a minimum but that doesn't mean you can't have any at all. Even a single global c++ object can contain many member variables that are all (if public) globally accessible through that object.

And Code Complete even has a section called Reasons to use global data, basically saying you should really only use them for your application specific highly used data. The way you write your overall code will have more of an impact on whether or not someone else can understand it then if you used global variables.
Quote:Original post by Riekistyx
So people who teach this methods are incorrect, wrong, and its never used eh?

I know a place that teachs it heavily and relies on it.

Unfortunately, a lot of places which teach programming are subpar when it comes to the things they teach. This is a fact of life.

Quote:So let me ask you this. Are you in companies saying this to me? Or just some persons who dont prefer singletons.
Both.

Quote:I cant begin to see how its messy at all. Infact Hungrian notation is probably more ugly to look at than this. So you tell me...

It's not that it's messy. It's just that it's stupid. There is no good reason to use a singleton "instead of a global".
Quote:Original post by Riekistyx
So let me ask you this. Are you in companies saying this to me? Or just some persons who dont prefer singletons.
I know a number of professional developers and consultants (all very skilled people) who realize that a singleton is almost always the wrong answer.

It comes down to this. If you are using a global, you are probably making a design mistake. If you are using a singleton, you are almost certainly making a design mistake.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Quote:Original post by Mxz
I don't think you are correct to say that a deprecated feature will never be removed, for the very reason that the definition of the word deprecated in C++ is: Normative for the current edition of the Standard, but not guaranteed to be part of the Standard in future revisions.


Deprecated means it's not guaranteed to be in future release; however, that doesn't mean it's guaranteed to be gone. Certainly many deprecated features will be removed. Some won't because they're too widespread. Committee members have said as much. I suspect that this is one of the features that won't disappear. Of course, I could be wrong.


Quote:Original post by Mxz
I think it is also worth stating explicitly that use of the static keyword is only deprecated when declaring objects in namespace scope, your post seemed to confuse that slightly.


To help me out next time, how was my post confusing? As far as I can see there's only one time I mentioned static but didn't mention namespace. It didn't look confusing to me.
Quote:Original post by Troll
Quote:Original post by Mxz
I don't think you are correct to say that a deprecated feature will never be removed, for the very reason that the definition of the word deprecated in C++ is: Normative for the current edition of the Standard, but not guaranteed to be part of the Standard in future revisions.


Deprecated means it's not guaranteed to be in future release; however, that doesn't mean it's guaranteed to be gone. Certainly many deprecated features will be removed. Some won't because they're too widespread. Committee members have said as much. I suspect that this is one of the features that won't disappear. Of course, I could be wrong.


static will probably be a part of C++ so long as there's any attempt at remaining (mostly) compatible with C. Or, until C adopts C++ features like namespaces (which is unlikely because the lack of name mangling in C is one reason it's still used).
Quote:Original post by Troll
To help me out next time, how was my post confusing? As far as I can see there's only one time I mentioned static but didn't mention namespace. It didn't look confusing to me.


The part I thought may have confused people was the very first thing you said, namely.

Quote:
static variables are deprecated




This topic is closed to new replies.

Advertisement