Re: Singletons
On 12/2/2012 4:04 AM, Dave Abrahams wrote:
Please, everyone, before arguing for Singleton,
In most singleton threads I sad in the last decade arguments were put
not FOR singleton, but AGAINST singleton-bashing.
take some time to
research the arguments against it and understand how Dependency
Injection addresses those issues.
Honestly I didn't see the actual problems of singletons actually listed
in those DI-proponent articles, let alone address them. In a general
sense at least.
PFA, and singleton both have pros and cons, that we programmers should
match against the actual situation and go with the better. Or refactor
the system.
As for DI, that looks like a new branch in PFA I personally see it way
more poisonous than the singleton approach, and would not be surprised
at all if 10 years ahead we saw more DI-bashing threads than singleton
bashing today.
As sure, widening the global state is not good -- but if it's easy to
locate the *few* spots interested in the part really, it may be mitigated.
argument-based PFA blurs that possiblity, and DI does even worse, it
widens the object's state, for many objects. It makes actual usage less
visible rather than more visible as claimed. And another side effect
that it has tendency to create over-engineered solutions. And way more
overengineered ones than Singletons::MySingleton::GetTheInstance() crap.
I did experience so much bloat following those bashing threads, for most
everyday topics, assignment to flags replacing break, 8-level nested ifs
replacing an early guard-clause return, pasted code-blocks replacing
goto CLEANUP (in C); tons of getter/setters replacing public data in
structs equivalent to std::tuple, and many others.
Yes, globals and singletons CAN be bad for many reasons. But in many
situations those reasons don't apply at all.
But experience shows that due to general bashing many people will stop
evaluating reasons, just jump to use the strongly and emotionally
suggested alternative. Creating a worse system and way more work to
those who actually understand SW engineering.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]