91
General / Re: A question about the dreaded singleton
« on: August 12, 2013, 10:40:07 pm »
The difficulty in testing comes from how in order to test you have to initialise all the dependencies of a class/function, either to fakes, mocks or real implementations.
And since when using the Singleton design pattern some of those dependencies are global, that means that class/function hasn't declared it needs to go off to that singleton explicitly, so knowing to do that requires either overly specific documentation or going through the source code under test line by line. Neither are desirable and mean the code is not inherently self-documenting.
This is related to a general problem with the Singleton design pattern: Any usage of them is implicit. The usage of the global singleton is a non-obvious side-effect, and that's the kind of place that at best just wastes some of the programmers time and at worst is where the crashes, bugs, glitches and mistakes love to hide.
A few good blog posts on this can be found here:
http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/
http://misko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/
http://misko.hevery.com/2008/08/25/root-cause-of-singletons/
There is little wrong with your code having only one instance of a class (singleton), that's an often unavoidable state, but if you were to go and take that instance and shove it into a global (Singleton design pattern) is a bad idea for all the reasons global variables are bad ideas.
It's almost always better to have one instance of something you explicitly pass around to what needs it, and have the lifespan of that instance be only as long as it's needed.
And since when using the Singleton design pattern some of those dependencies are global, that means that class/function hasn't declared it needs to go off to that singleton explicitly, so knowing to do that requires either overly specific documentation or going through the source code under test line by line. Neither are desirable and mean the code is not inherently self-documenting.
This is related to a general problem with the Singleton design pattern: Any usage of them is implicit. The usage of the global singleton is a non-obvious side-effect, and that's the kind of place that at best just wastes some of the programmers time and at worst is where the crashes, bugs, glitches and mistakes love to hide.
A few good blog posts on this can be found here:
http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/
http://misko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/
http://misko.hevery.com/2008/08/25/root-cause-of-singletons/
There is little wrong with your code having only one instance of a class (singleton), that's an often unavoidable state, but if you were to go and take that instance and shove it into a global (Singleton design pattern) is a bad idea for all the reasons global variables are bad ideas.
It's almost always better to have one instance of something you explicitly pass around to what needs it, and have the lifespan of that instance be only as long as it's needed.