27 March 2012

Me against the world: Singleton?

I just looked at http://www.reddit.com/r/java/comments/qpb8p/java_ee_revisits_the_design_patterns_singleton/. I find it rather interesting that the comments against Singleton have low scores and comments in favor have high scores.

This leads me to think: is it me against the world? Well at least my kind of programmer - that thinks that Singleton is a code smell - is in minority? Just to boost "my" side of argumentation let me be clear:

Don't us Singleton(s)!

I have personally had firsthand experience of inheriting a code base where Singletons was used extensively. It's a total nightmare! For me two areas stand out:
  • Writing test code for Singleton is much harder. You always have to watch out if one part of the code makes a side effect that makes your test case either false positive or false negative.
  • Converting a single threaded system into a multi threaded system just does not work with Singleton. You basically have to remove everything for it to work well and to avoid unnecessary synchronizations.

No comments:

Post a Comment