
Singletons Are Evil
This means that 25% of all classes in the code base are virtually impossible, or at least very hard to unit test. Testing gets difficult as the singletons' global state has to be initialised for each test. This slows down test execution and makes the tests more brittle. That's one of many reasons I just hate the singleton pattern. It's an object oriented anti-pattern. In case you do not believe me, I am sure you will believe well known people like Miško Hevery, Uncle Bob or J.B. Rainsberger to name just a few. At least read Steve Yegge's take on the singleton which is my favourite article on the topic.
Pure Evilness
Some years ago I worked on a code base which was similar both in size and the number of singletons involved. In fact it was not that bad, only 15% of all classes were depending on a singleton, still it was nasty to work with.
So I took the liberty to educate my team mates on the singleton's evilness. The following list was taken from my Design Patterns reading group notes and contains all negative effects of singletons that I'm aware of. Singletons are evil because they ...... introduce global state/global variables.
- ... hurt modularity and readability.
- ... make concurrent programming hard.
- ... encourage you to forget everything about object oriented design.
- ... are a throwback to non object oriented programming.
- ... allow anyone to access them at any time. (They ignore scope.)
- Finding the right balance of exposure and protection for an object is critical for maintaining flexibility.
- They typically show up like star networks in a coupling diagram.
- ... make assumptions about the applications that will use them.
- If nobody is going to use them they are basically just memory leaks.
- Signatures of methods (inputs) don't show their dependencies, because the method could pull a singleton out of thin air.
- All singletons have to stick in boilerplate code.
- Everyone who uses it has to stick in boilerplate code, too.
- When classes are coupled, it is possible only to test a group of classes together, making bugs more difficult to isolate.
- ... can prevent a developer from testing a class at all.
- Two tests that actually depend on each other by modifying a shared resource (the singleton) can produce blinking tests.
- Mocking would make unit tests run more quickly.
- It is simpler to simulate behaviour than to recreate behaviour.

6 comments:
Well presented blog. The diagrams make it very clear what you are discussing. Well done.
So what's the alternative, if you need access to a resource/service from several parts of a program?
After short research, I land on stackoverflow: stackoverflow.com/questions/1300655/whats-alternative-to-singleton
Ah yes, I should have added alternatives. Thanks for the link.
Singletons are only evil when they are used in conjuction with the service lookup pattern. If they are injected (ie the use of getInstance() in utterly few places) they pose no problems to testing or coupling.
@Johan
You are right. But in such a case the calling code does not see the singleton as it is, it's just using delegation. So it's not your typical singleton usage ;-)
Post a Comment