Are global variable evil?

  • Thread starter Thread starter LP
  • Start date Start date
Hi,

IMO this is a bad approach, both for using a singleton, where a static
class should be used and for putting a bunch of unrelated variables in a
common place instead of grouping then based on use.

A singleton should be used when you want a single instance of a class being
created but the code that will use it. DOES NOT KNOW THAT , or is expecting
an instance of a class.

In your case of a "place to put everything that has no better place" you
would be better with a static class.

cheers,
 
Olaf Baeyens said:
A stupid example would be that you need to have a login for this server,
but
imagine that your files are spread on a local server and a internet
server.
Then you need 2 logings and maybe 2 user ID's.

Yes, but that was just an example. I'm sure I (or you) could come up with a
case where a global variable is appropriate.

Michael
 
I aggree, using singleon is a good practice to use global class or viable.
however, I have class A and Class B, both of them have their own static
vairables.
Do I need to come up another singleton class GlobalVar to collect all of the
Class A's static variables and B's static variables into this singleton class
GlobalVAr class or make Class A and Class B the way it is.
 
To summarize:
- "Globals" (nonstatic instance properties) are not "evil", but they are
"dangerous".
- Encapsulate as much as possible. Don't, however, kill yourself. The
aforementioned "if every method in a class needs the userID, make it global"
is a good guideline.
- Because I personally am somewhat anal and masochistic, I take the position
that just because every method *today* needs the userID, that doesn't mean
every method five years from now will. Hence, I will usually pass userID as a
parameter until I can no longer take the pain. I then break the paradigm and
do it both ways =).

My point is that within the rules of good OOP design, there is much rule for
stylistic preference. Don't let code nazis change your style without a good,
justifiable reason.
 
qq said:
I aggree, using singleon is a good practice to use global class or viable.
however, I have class A and Class B, both of them have their own static
vairables.
Do I need to come up another singleton class GlobalVar to collect all of
the Class A's static variables and B's static variables into this
singleton class GlobalVAr class or make Class A and Class B the way it is.
If the two instance don't depends on each other, just leave them alone.
 
Thanks Ignacio. I've had a long thought about all this.

First off, you are correct that my post used the word "class" when it should
have used "instance" or "object". As you say, whether we are dealing with
singleton or static methods, we are always using a "class'. My mistake comes
from my background in other languages, where a "class" is optional, and would
normally only be used if we were intended to create "iinstances".

I now think that I would prefer a class with static methods, to a singleton,
unless there was a demonstrated need for singleton. As you say, the cost of
Singleton is complexity, and there are good examples of static methods within
the API. The only time I can think of that Singleton is necessary is if we
need to inherit functionality from another class.

As for my argument that Singleton has the advantage of "flexibility", well,
as you say, that comes at the cost of complexity. In addition, I have found
that providing unnecessary "flexibility" is a waste of time. If we ever need
to restructure our class as a singleton, then that can be done with little
effort, so why complicate matters now, for something which may not happen in
the future, and which, if it does happen, is easily dealt with?

Regards,

Stephen
 
Hi,


Javaman59 said:
Thanks Ignacio. I've had a long thought about all this.

First off, you are correct that my post used the word "class" when it
should
have used "instance" or "object". As you say, whether we are dealing with
singleton or static methods, we are always using a "class'. My mistake
comes
from my background in other languages, where a "class" is optional, and
would
normally only be used if we were intended to create "iinstances".

IIRC this happen with all the languages I have found, as well as the
literature, there is not a clear difference between the type and the
instance, they use "object" to describe either one of the concepts.
I now think that I would prefer a class with static methods, to a
singleton,
unless there was a demonstrated need for singleton. As you say, the cost
of
Singleton is complexity, and there are good examples of static methods
within
the API. The only time I can think of that Singleton is necessary is if we
need to inherit functionality from another class.

No really , as I said before you should, well HAVE to use a singleton when
the code that will use it assume an instance. There is not a "always use
XXXX" in programming, each case should be analyzed independely.




Cheers,
 
Back
Top