Nick Malik said:
All of us are in the category of people who "may or may not know what they
are talking about."
The wiki page has a long quote from Kent Beck, creator of Extreme
Programming, discussing a major design issue that he refactored out simply
by removing a badly used Singleton. Realize that the C2 wiki is, first
and
foremost, a discussion space. You are going to see opinion when you go
there.
That is supposed to be impressive, then?
Opinion is the name of the game, and, IMHO, previously insufficent evidence
was presented to base an article warning off of. You might be able to win
the case(I've not put enough thought into the specific issues to comment),
but it takes significantly more than just a wiki to address that. Once I've
read the other material I may change my mind, or I may not. Common practices
need to be built on a general consenus, which I have not yet seen.
OK... here's a better article:
"Why Singletons are Evil"
http://weblogs.asp.net/scottdensmore/archive/2004/05/25/140827.aspx
or this one
"Perils of the Singleton"
http://www.softwarereality.com/design/singleton.jsp
How about
"Friends don't let friends write singletons"
http://members.capmac.org/~orb/blog.cgi/tech/coding/no_singletons.writeback
Here's a list of programming hot-topics, including one titled:
"The Singleton Design Pattern Does More Harm Than Good"
http://www.lisp-p.org/controversy/
Where's the proof that GOTOs are harmful? Where's the proof that global
variables are harmful? What do you mean by *proof* in this context?
There is no hard evidence in any of these cases. And issues with goto and
globals are mostly blown out of proportion by people selling conventions.
Believe it or not, every now and then I run into a circumstance where one or
the other may make cleaner code. However, because of best practices and
patterns people, I end up with messier code trying to avoid what I know will
result in whining from someone.
If you actually read some of the information on that is available, on the
web, about Singletons, you will see that the intent was to describe a
"Creational" pattern, while too many people are using it as a "Behavioral"
pattern (e.g. an OO version of a global variable). If global variables
are
harmful (and you don't seem opposed to that notion, and if Singletons can
be
misused by allowing the creation of global variables in an OO context,
then
that misuse should be considered, by reasonable people, to be harmful.
Logic.
You didn't ask about globals, that I can remember anyway. I did comment that
I felt the global variables are bad article was a bit better example of a
good link than the anti-singletons one was.
Global variables, in and of themselves, are not harmful. Perhaps some usage
of them is questionable. But then some usages of the if statement are
questionable. I don't particularly like the "NEVER USE GLOBAL VARIABLES OR
ELSE" threats issued in best practices books and articles. They are
inflammatory and without regard for reasons to use global variables and
patterns beyond the OO purists point of view. OO simply isn't everything and
shouldn't(perhaps cannot) be shoehorned into every solution. The mere fact
people use singletons and static members is a testimony to that.
Best practices are best only in the sense that they are ideal. Ideality and
the real world don't always coincide. Global variables are fine when they
are appropriate, as are singletons and gotos. No one of them should be
labelled as bad, just particular patterns using them(and, generally, only
those patterns which are superceded by others. Using goto to implement else,
for example, should be frowned upon. Using goto to exit 30 block levels
perhaps should not be. It is arguably clearer than 30 if statements checking
a guardian boolean).
Give me some credit. A warning is not a slippery slope, denying the
reader
of valuable technical content. It is a door to more knowledge that the
reader has the right to walk through, whenever they see fit. Heck... it's
Jon's article, not mine! He's an excellent writer. Do you think he can't
figure out how to strike a balance?
Its actually potentially a prod in the direction of an authors opinion
instead of prevailing opinion.
And, I suspect Jon certainly can and will balance his article, but he left
this discussion some time ago it seems. I merely stated that some caution
and extra consideration would be a good idea. You were the one that started
this particular subthread by suggesting that the warning was appropriate,
infact on the level of household cleaner poison warning labels. I simply
replied that you hadn't offered much in way of suggesting that this is a
warning thats anymore valid than say...a warning that the household cleaner
may smell bad. It may be true, but it may not really be a useful one. A
warning is certainly merited if there are real problems, but it is not if it
is based entirely on a small selection of people and their particular view
on the world.
The question I pose is that, without considerable research and
consideration, is it proper and the best course to offer advice against
singletons from a page as commonly quoted and as well respected as Jon's
article?
The door is open to anyone who would like to investigate further. I
didn't
present the best article to the readers who are not familiar with the
debate, and for that I (clearly) could have done a better job. I hope
that
the links above mitigate my mistake.
Yes, anyone can investigate further, they say that with regards to alot of
things(like commercials, for example), but what percentage does? Much in the
same way as it would be to mention no negatives at all, it is rather
unbalanced to only reference negative ideas about a given idea in an
article(even if that articles entire purpose is to discredit or discourage
an idea). An article that only points out the flaws in something or goes out
of its way to make a point in exposing flaws is particularly untrustworthy,
or should be considered as such, IMHO.
Again, this isn't to say that Jon can't or won't balance his article well. I
am just saying that there is alot more to the picture than simply the
negating side of the argument. I do think, however, that the best course may
be to write a seperate article lining out the points and making his
recommendations and linking to *that* from the technical article rather than
trying to wedge warnings into the technical article itself and risk
comprimising the article because of it. But, again, thats me.
*IF* those are actually misused and not just the vendetta of a small number
of loud people.
Vendetta? According to dictionary.com, a vendetta is "A bitter,
destructive
feud. [Italian, from Latin vindicta, revenge]
The meaning I intended was the one used in "personal vendetta", that is, the
particular participants have issues with Singletons or one persons
particular use of singletons and are out to eliminate or discredit them. A
great deal of the computer science world works this way, it'd be silly to
think that anything is implicitly immune.
Not sure that anyone on the wiki was seeking "revenge" or was "feuding"
with
anyone. Considering the fact that the C2 wiki is where many of the modern
discussions of design patterns began, I'm having a hard time imagining
these
conversations as being specifically opposed to OO design patterns... they
are merely being honest about the limitations and misuse of one of the
most
common patterns.
And again, that doesn't matter. That is almost innocence by association.
Just because it was on the C2 wiki doesn't make it any more correct or
proper than if it was on some two bit forum elsewhere. The people in the
discussion may well dislike the pattern enough to try to eliminate it
without a valid reason.
Also, as far as design patterns go, they are a take some and leave the rest
kind of thing. Many patterns are strong, yet some are lame, others uselss,
and many are way overused(like the NullObject pattern, for one). Simply
calling it a design pattern doesn't nessecerily make it right, it just makes
it someones opinion that they felt like publishing. It is far more a matter
of what you agree with than of who said it.
As I said, I should have provided a better article link. I hope that I
did
a better job this time.
I'll read them tomorrow and let you know. It is getting a touch late for
serious reading, methinks.