Why are singletons bad?

Started by
90 comments, last by _moagstar_ 15 years, 1 month ago
Quote:Original post by jonathanjansson
Quote:If you are an OO purist, globals and singletons both violate various pure OO principles.

I don't think they violate OO principles.

Well, they do. For more info, google Law of Demeter, Open/Closed Principle, and (arguably) the Acyclic Dependencies Principle.
Advertisement
Quote:
Or there is no need for another of the same object, and the code is easier to use. Or singletons could be used to contain boiler plate code that the developers would have to run every time they want to do something.

No, those are wants, and poor ones. The singleton pattern, as expressed by the GoF book (which is the de facto standard for definition of many design patterns) expresses the singleton as SimonForsman did. Your additions are just fluffy attempts at justification, and are non-canonical.
Quote:Original post by TheMac
I am going through this in my head and I can't understand why for one, singletons are hard to test in your unit tests.


You might be able to unit test a singleton, but you cannot unit test anything that uses that singleton.

A unit test by definition tests a single thing. If a class or function uses a singleton, then any test written for that entity will depend on the singleton and therefore the test is no longer a unit test.

If your singleton touches some larger system e.g. the windows registry, the file system, a database, etc, then your tests will also potentially have some nasty side effects. You don't want that stuff going on in your unit tests, or any other kind of test for that matter.
On a few different occasions, I have tried to completely avoid using singletons. I kept ending up with more code, and a design that didn't work well. Sometimes an object is used in so many places, it just makes sense to have global access. I will say that trying to avoid singletons can lead to better code, but you have to give in sometimes.
Quote:Original post by incin
Sometimes an object is used in so many places, it just makes sense to have global access.


global access != you may only ever instantiate one;
Quote:Original post by Telastyn
Quote:Original post by incin
Sometimes an object is used in so many places, it just makes sense to have global access.


global access != you may only ever instantiate one;


QFT. If only everyone realized this...
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
My company uses singletons extensively. They have a very serious bug uncovered by me in regarding to the use of singletons. The cause of the problem is that they use singleton in their libraries(which are compiled into dlls) and every instance of their classes accesses/shares the same singletons though they are supposed to be unique instances, not sharing any resources or information. For example, you set a information in one instance, all other instances got the same information because they access the singleton. Another example is all the instances ask the singleton(same resource) to do some work, all the tasks are enqueued, all the tasks are waiting to be executed by the same singleton and the application is perceived by user to be unresponsive. The tasks of each instance can be executed in parallel if it has a normal class member, instead of accessing a singleton.

They overlooked this bug because they usually tested one instance, not multiple instances, during their testing. It is too much work to convert/redesign the library code not to use singletons.

Singleton are just disguised global variable IMO. Rule of thumb is not to use singletons in your library while it is alright to use singletons in your application.
Quote:Sometimes an object is used in so many places, it just makes sense to have global access.
Can you give an example?

In any case, as has been mentioned, if you need global access to an object, just make the object globally accessible (there's no need to make it a singleton).
I generally don't mind singletons, *if* they're used solely to control instantiation and are passed into objects using standard dependency injection type techniques.

Unfortunately, they're usually used as a "OO-compliant" version of globals.

I think there's some basic conceptual stuff to understand design patterns well, and singleton is one of the patterns that appears useful (for the wrong reason) to folks that haven't made that conceptual jump yet.
Quote:Original post by Antheus
Ever tried unit testing a database application where you need to test DROP statement? In common design where database access is singleton or global hard-coded, accidentally running such test without changing the configuration will wipe the production system. So how do you test it automatically?

Much easier if systems are not coupled.


QFT.

not exactly a DROP statement, but in general. once it's a hot database, with money-data in, and all, and you have to test stuff around, have fun if you can't just switch the database easily. i first had, in all my apps, a singleton connection (just so damn easy in .net). i learned the quite hard way (no dataloss in the end in the database) that this isn't how it should go :)

singletons are just bad. everyone who doesn't understand this still has to learn a lot in clean, save, proper programming.

i do use globals sometimes. i do use global functions sometimes. but never singletons.

edit:

i have found quite some places where globals can be handy (in small scale apps, that is). i never found one point, where a singleton has any gain over a global. if i thought it may be that way, taking the global away and making it a locally pass-around object was always simpler to implement, less to think about, and then, 100% working and debuggable and testable.
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

This topic is closed to new replies.

Advertisement