Are Singletons Bad?

March 23rd, 2008

So for the better half of my Friday I did research on how to best implement a service controller1 for the new AS 3 Heavy Video Player I am building. Normally I would just slap together a few singleton utility classes and be on my way but, after having a conversations with Myles2 about a blog post he read that said using singletons were bad, I started thinking about if I was going down the wrong path.

I believe there are 3 categories of code in the Flash World:

  1. Design Pattern - OOP - Standardized - Framework driven
  2. Implements parts from #1 but breaks the rules to get app done quickly
  3. Just wrong (Use global, root, and timeline code - throw backs to AS 1/2)

I would put myself in category 2 but my goal has always been to be in category 1. The biggest thing that keeps me out of category 1 is the fact that I don’t come from a programing/Computer Science background. My traditional artist turned programer past is something I fight with all the time and when it comes to mapping out an application and how to build it, I sometime hit a roadblock where I find some Design Patterns don’t always work 100% in Flash. So when I started rewriting Heavy’s AS 3 library I didn’t think much about all the singleton utility classes I wrote. When Myles told me to read this article it got me to thinking.

See in AS 2 using Global, Root, and Static Classes (as Singletons) was common practice. I always knew it was bad and did my best to avoid global/root as much as possible but when a deadline is looming around the corner and I don’t have time to correctly implement a truly oop system, I just did what was needed to get the project out the door. In fact, my own MCVC framework made use of a Singleton like Controller class accessible from all Models and Views in the system. This basically was my way of not using global and circumvent the whole scope issue. I was never 100% comfortable with using it but it worked. Even when Myles built the MLB Flex driven chat, he used a static class to bridge the gap between classes and the event syetem.

So in analyzing the dilemma of overusing singletons I am left with two questions:

  1. What do I gain by not using singletons?
  2. Do I really care?

What do I gain by not using singletons?

In AS 2, my singletons used static functions and were very specific to their use; mostly functioning as utilities/helpers. In AS 3 my Singletons simply uses an internal class to keep the constructor from being called more then once. Actually, if I removed that check, the class would work like any other class and I wouldn’t have to use a getter to the instance to access it. I would simply need to create a new instance. So my Singletons really are normal classes but I chose to force only one instance to be instantiated at any given time. I do see how accessing the class directly Singleton.instance.function() is bad but I find it messy to have to pass in a reference to the instance and call on that through chains and chains of embedded class instances. In past projects this passing in of a class instance reference made it very difficult to quickly change or modify whole code systems inside of my projects. Simple client design changes could have disastrous repercussions while I tried to track down exactly how to rework loose references to class instances.

It appears that the two main reason for not using singletons starts with the braking of OOP Modularity3 because of the direct connection to the Class and not an instance of that class. The other reason is not being able unit test/substitute said Singleton for testing. The unit testing argument I can’t comment on because I don’t really do it. I don’t even know many Flash Developer who do. As far as testing goes, most of my classes have debug modes built into them that I can toggle on and off in the doc class or through Flash Vars so I don’t buy into that argument. Further more, I use a variable reference as a shortcut in the constructor of my classes that points to the singleton utility4 so if I needed to, I can substitute a debug class there or just place the singleton class into debug mode. When it comes to OOP Modularity, I don’t feel that you can truly achieve this in AS 3. How can you with an application language that uses a library with graphics that force you to link them directly to a class; completely defeating the whole separation of logic from display? Even if you used dummy classes to represent all of your assets, you would still need to remember state and that generally gets attached to the graphic representation anyways in Flash. Flex does a better job of separating the two but asset attaching/manipulation is not much better.

Most of what we do as Flash Developers is throw away. It is not like we are building an OS, or a production quality application. Most of us are stuck doing one off product sites, micro sites, or even worse banners. For those of us lucky enough to build full scale web apps with many layers and back end integration with live streaming data, I understand the argument to be as close to OOP and follow stricter design patterns in your code architecture. However right now, I am just making a video player, does it really apply? I want my video player to be scalable, and I have a nice UI class system for a standardize way of implementing each button along with a custom JS event system but outside of that I am only building a Video Player. Will the OOP police arrest me because I chose to get video xml data through a singleton utility instead of a model’s instance? After I get the xml my Singleton spits out a new model instance with the data, isn’t that close enough?

In the life cycle of this player I can’t think of one instance where I will have an application with multiple video players playing at once so should I build my system to support the hypothetical situation? I only need one video at anytime, seems like overkill to make a system that supports XX videos players for OOP sake when my singleton enforces the fact that there should only be one standardized way to retrieve video data from the back end. There is a Singleton Design Patter5 , and in small apps like a video player you come across lots of instances were you only have one of a given thing. This brings me to my next real question.

Do I really care?

I would be lying if I said no. The fact is at then end of the day, I really do care if I am using the correct design pattern and making my code maintain modularity. Unfortunately this is an internal dilemma and not one placed on me by my job. Granted the code I write for my job would be more practical if it was all extendable and reusable but I tend to rewrite lots of code over and over agin from project to project. I never get it right the first time so I am always improving on what I last wrote even though I could have just extended it. So that whole notion of modularity is out the door when I start my next video app in 6 months. or even worse when AS 4 comes out.

I had a little talk with Kåre from AKQA about whether we are overdoing the whole OOP idea of coding. Him and I are big believers in standards but in the fast passed world of agency development there is very little time to strictly follow all of the conventions. AKQA’s developers write some really sick stuff, but every hour costs a client money and they have to push out a working product on a tight deadline. Kåre is someone I highly respect and even he was thinking about not using models because of E4X and how easy it is to just get the data directly from the source. That is a conversation for another time but the idea behind cutting corners for productivity sake is an important one. The majority of us develop under in the confines of a fast paced work environment. Unless you are doing this as a hobby, then by all means break out the Computer Science books and find your OOP nirvana.

At some point I really question who am I writing code for? Myself or someone else? I am a very competitive person and I always strive to be the best that I can so when it comes to myself I spend hours and hours making my stuff conform to the correct design patter. When it comes to work, I can’t explain to my boss why this took me 10 hours when it should only take 3. Well 10 hours was trying to shoe horn Action Script into doing something it just didn’t want to do from the beginning. My singleton is a code Swiss army knife. Its not always the right tool for the job but it serves its purpose in a pinch.

I hope this rant hits a cord with some of you. Some people may with think I am promoting poor coding practices but that’s not the reason I rase this question. I am interested in knowing what other people think about using Singletons and just how far down the OOP path we need to go before saying “ok that works, what’s next?” Can a happy medium exist where we write applications that closely follow all the right standards but cheat just a little or is there something Adobe can do to make flash work better inside of the common Design Pattern models to help us out? The answer gets even more vague when you look at other languages. The most interesting is Objective C6 and how they use lots of Singleton like classes as the foundation of their code. Hope this helps stir up the thinking, questioning the rules is healthy and should always be encouraged. Don’t forget to comment below!

Here are some other interesting links on the topic:


  1. To handle services like saving favorites, logging in, alert windows, and other core functions []
  2. we use to work together at MLB.com but now he is over at RGA []
  3. In computer science, modularity is the property of computer programs that measures the extent to which they have been composed out of separate parts called modules. A modular approach to programming is gaining popularity in fields of artificial intelligence systems integration, where a large-scale general A.I. system is composed of modules that each serve a specific purpose and communicate with each other to produce the system’s overall behavior. []
  4. Example _utility = Singleton.instance; []
  5. In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. Sometimes it is generalized to systems that operate more efficiently when only one or a few objects exist. It is also considered an anti-pattern since it is often used as a euphemism for global variable. []
  6. Seeing how me and a few thousand other developers are looking to build apps on the iphone []

2 Responses to “Are Singletons Bad?”

  1. The Flash Art of War » Blog Archive » Recognizing a good programer? Says:

    [...] on the topic of good coding practices I forgot to post a link to this great artical on recognizing a good programer. I have used this as [...]

  2. spasticfantastic Says:

    Thanks for an interesting article, thinking about things instead of reiterating the same tired concepts.

Leave a Reply