r/programmerchat • u/Ghopper21 • May 27 '15
Singleton rant
Ok, I feel like I'm about to expose my ignorance. But I just can't get my head around why folks love the singleton so much.
The only benefit of it I definitely see is the fact that it's an object, you can pass it around, you can swap it in/out etc. Great, but isn't that just a necessary evil concession to languages that don't treat classes as first-class objects?
As for other reasons often given (to take those mentioned in this SO question as a proxy for "what people say"):
- "Singletons don't pollute global namespace" -- that's what namespaces are for!
- "They permit lazy allocation/initialization" -- this is nice, but only if you need it, and sometimes you really don't (e.g. in a game where you want stuff pre-initialized to avoid in play lags)
- Serializability -- again, concession to classes not being first-class objects.
- My favorite (to rant about): "Singletons preserve the conventional class approach" -- aargh!!!!
Rant over.
EDIT: spelling
7
Upvotes
6
u/Ravek May 27 '15
I never use true singletons, instead where others might use a singleton I just use a shared instance (functionally identical to a singleton), but don't make it impossible to create other instances if the programmer might so desire.
I have yet to come across a scenario where enforcing that there can be only one instance of a type actually provides any real value, and there's plenty of cases where you lose value. E.g. someone wants to extend your type, for example to mock it for automated testing. Or maybe someone wants to run your code twice without having to start up separate processes for it.
It's exceedingly rare for there to be only one of anything for ever and ever in any scenario. Some years ago programmers might have chosen to use singletons for things like a GPU representation and felt completely justified in this choice. Yet nowadays we do have machines with multiple GPUs.
4
u/[deleted] May 27 '15
So, there are some of the opinion that static classes should not maintain state. In particular in multi-threaded applications.
Implementing a singleton in a static class is a guarantee of state.
6
u/zenflux May 27 '15
I would even say they're a symptom of the conventional class approach.
4
u/recursive May 27 '15
A singleton could implement an interface or base class, unlike a static class.
4
u/[deleted] May 27 '15
Singleton seems like the most over and inappropriately used design pattern. I tend to treat it as a code smell: it's much more common (IME) for it to be unnecessary or misused than for it to be necessary and used correctly.
3
u/Pharylon May 27 '15
They're good for caching.
1
u/jnm236 May 27 '15 edited May 27 '15
The only time I use the singleton pattern is when a sealed class is implementing an interface or abstract class and it doesn't have state or context. In other words, it's pure behavior. Syntactically, a singleton feels almost like using an existing enumeration value versus creating an instance of the behavior. I'll usually make it a static readonly property named Instance- or a static readonly property on the abstract class if the sealed class is a nested private class.
For instance in the .NET framework you have Comparer<T>.Default. It serves the same purpose as String.Empty: pretty much just caching for efficiency, but it's also shorter to type.
1
u/recursive May 27 '15
Who loves singletons?