Singleton Is a Bad Idea
nedbatchelder.com
nedbatchelder.com
Though I do admit to occasionally violating my own precepts and using them.
But given a semi-decent dependency injection framework, and a choice of lifetimes like 'singleton' (only one per container), 'transient' (as many per container as required per type) and 'scoped' (as many per container as required for transactions), do I really need to feel bad about the first choice?
My app only has a single task scheduler: that's sort-of important, as otherwise there is just chaos. So that's a singleton. Maybe not the same singleton depending on whether I'm running a test or the actual production app, but a singleton nonetheless. So what, exactly, is the big issue with that?
Nothing about global visibility, and again, any decent dependency-injection framework should manage that well enough.
So, still mystified about all the singleton-hate here!
But hey, thanks for the hot take.
(I know why, generally—low-expressivity languages make it nightmarish to actually do this.)
If you need globals and your language supports global data or functions, use that. If not, Singleton is a viable alternative.
Open globals are unstructured, and more likely to turn into a messy free for all.
* It's a solution to store, encapsulate and abstract global data
Is this a python-centric complaint? I usually write C# and Singletons are just one of a few different scope types for defining objects in the IoC container, no easier, harder, rarer or more common than any other class type.
As many pointed out, singleton = global variable.
That is the wrong approach. Treating the singleton like a normal object that must be injected makes the pattern way more useful and safe
Singletons are a tool, that at some point you just can't avoid. We can discuss the way to create or access them, but saying they are just all bad is a diservice to younger engineers that are learning proper paterns.
Yes, making sure no one makes more than one instance of the class is defensive programming and you could consider it bad, but a lot of techniques in programming are defensive like that, especially in OOP.
A simple example of a constraint is 'private' modifier on a method. You could make your arguments against 'private' constraint and say "that's an assumption that might not hold in the future", so we shouldn't have private methods.
Yes, these things are tools, and in some cases, better tools have been invented.
The filesystem tree on an unix OS is a better example but that was also been identified as a mistake and there have been attempts to rectify it (namespaces)
The issue is really around having multiple handles for writing (not just reading or appending), and the number of times you actually have this problem here is vanishingly rare.
There I can certainly agree, but ...
> An exe is a singleton, a file is a singleton. Most things in a computer are singletons.
I would say you are mistaking the instance for the class. An individual exe/file/... are instances of the abstract concept file or executable.
What you are interacting with is usually an instance, not a class.
No, a singleton is a pattern that ensures that there is at most a single instance of a class.
This is a remarkably bad example, because either (a) a program might need more than one board (e.g., a server for multiple games), in which case the example is irrelevant, or (b) singularity really is a fact intrinsic to chess boards, and is therefore a perfect example of a Singleton.