A comment on Singletons

Allright time for a little tech talk. The following post is really meant for me and is pretty much entirely about programming. So you've been warned to stop reading and go somewhere else to waste your time.

Singletons are a tricky thing. When first introduced to students, it represents something awesome. You see something that can be accessed anywhere by any object. This seems to open up a world of possibilities. This one amazing place you can stuff just about any piece of information. Singletons are pretty awesome in this regard but all of these powers are also what cause singletons to become super villians of the software patterns.

My experiences has shown me that Singletons cause severe software arch rot. Right in front your eyes, Singletons can cause your supposodly brilliant design to slowly crumble and fall apart. The first major problem is that they get in the way of what an object should be designed to do: complete a task and complete it well. The fact that singletons are super-globals slowly encourages placing data that is not global into them and giving them functionality they do not need.

The next horrid problem with Singletons is that they begin to limit your design. While you may say "there will only be one instance of this object" and set out creating this object following that motto. The moment you realize that you need more then one, you will have to unwind a hideous amount of code. Now what if you had started with the asumption that there could be more then one of these objects? Your code would have to be formed around passing references and becomes much easier to switch from non-singleton to singleton.

My feeling so far is that Singleton should not be implemented until late in its development. This encourages better design both before and as your coding.

The above comments are made by a student and should not be taken as coming from an experienced proffesional.

No comments: