Brian Button - One Agile Coder

Blogging on all things .Net, C#, and Agile

My Links

Blog Stats

News

Archives

Post Categories

Agile Solutions Group

Blogs I read

Enterprise Library V1 Team

Button's Law of Design Maturity

The prevalence of Singletons in a design is inversely proportional to the maturity of the design, and the designers.

Be it known, that from this day forward, this shall be known as Button's Law. (Just kidding)

There is a note a truth here, however. I've talked to a lot of the best designers that I know in the field, and we all share a common opinion. (Not that I'm putting myself in the same class as these developers. I'm merely stating that we share the same opinion!!!) Singletons can be a useful pattern, but they are also the most abused pattern seen in the wild. You can read about my personal feelings about singletons in Scott Densmore's blog.

Joshua Kerievsky has written an excellent summary of this point of view in his new book, Refactoring to Patterns. Reading this chapter in his book is what brought this rant to mind.

I now return you to your regularly scheduled programming.

-- bab

posted on Tuesday, August 17, 2004 8:22 PM

Feedback

# re: Button's Law of Design Maturity 8/17/2004 2:11 PM Grant

I agree with most of what's already been said, but I do see some uses like HttpContext.Current that are pretty handy. Then again global vars are pretty handy and so is coffee. :)

You're preference would either be reference by pass (ctor/method) or, minimally, exposing the singleton as only IHttpContext (e.g.)?

# re: Button's Law of Design Maturity 8/17/2004 9:10 PM Tim Burns

The problem with creating a singleton is that when you create it it makes design sense, but it leaves you open to problems down the road. Say you have a singleton for all the parameters of your system and you use that in a servlet handler.

ApplicationResources messages = ApplicationResources.getInstance();
...
messages.getMessage("date.format");

where getInstance() returns a static instance of ApplicationResources.

Then, sometime down the road you decide to implement a convience method for the date format.

Date myDate = messages.parseDate("date.format", date);

The problem is that parseDate will look like a thread safe method but every so often myDate will be totally wrong, because it isn't thread safe and myDate will by yourDate. I would say just avoid the singleton.

Tim

# re: Button's Law of Design Maturity 8/19/2004 6:15 AM Adrian Florea

In fact, this is why in the entire .NET Framework (v.1.1) there are just 3 singleton classes: System.DBNull, Microsoft.JScript.Empty and Microsoft.JScript.Missing. :-)

But version 2.0 of the Framework will add another 7: System.Empty, System.Reflection.Missing, System.Data.Odbc.OdbcFactory, System.Data.OleDb.OleDbFactory, System.Data.ProviderBase.DbConnectionPoolIdentity, System.Data.SqlClient.SqlClientFactory and System.Data.OracleClient.OracleClientFactory.

# re: Button's Law of Design Maturity 8/19/2004 4:27 PM Paul Bartlett

Nothing to do with singletons, but coincidentally I also noticed an anti-metric (i.e. one to be minimized) yesterday: ifs/kloc (i.e. # of if statements per 1000 lines of code). It was just a "gut feel" initially, but I guess my reasoning was that too many ifs suggest either too little, or incorrect, usage of OO features.

# re: Button's Law of Design Maturity 8/19/2004 10:11 PM Darrell

Good stuff. I've looked at singletons, but the only one I ever wanted to implement (and got a friend to implement) was a client-side app where I didn't want my hotkey (winkey!) to keep creating new instances of the app and instead bring it to the front.

# re: Button's Law of Design Maturity 8/20/2004 12:14 AM Brian Button

Grant,

<pre>
I agree with most of what's already been said, but I do see some uses like HttpContext.Current that are pretty handy. Then again global vars are pretty handy and so is coffee. :)

You're preference would either be reference by pass (ctor/method) or, minimally, exposing the singleton as only IHttpContext (e.g.)?
</pre>

The way I usually use singletons when I have to is to put access to them at the head of a thread of control, and then pass them into my code to where ever they are needed beneath there. That gives me the advantages of the singleton (global access and limited creation), but still gives me the ability to pass one into a particular method call during unit testing, etc. So, my preference is, in deed, to pass them where they are needed.

# re: Button's Law of Design Maturity 8/20/2004 12:17 AM Brian Button

Paul,

I love that metric! I read another blog entry about that a few days ago where the author was talking about if statements that check stuff internal to your code were missed opportunities for polymorphism, and the only valid reason for having them was in checking things that were outside in your environment.

Maybe we saw this in the same place???

-- bab

# re: Button's Law of Design Maturity 8/25/2004 3:25 AM J. B. Rainsberger

An interesting conclusion, Brian; and I like it. Of course, when your first published article goes by the name "Use your singletons wisely", you kinda have to be of that mindset, don't you? :)

http://www-106.ibm.com/developerworks/webservices/library/co-single.html

# re: Button's Law of Design Maturity 9/27/2007 11:13 PM biseo

Singletons frequently are used to provide a global access point for some service.

# re: Button's Law of Design Maturity 1/9/2008 3:50 AM edongzkisiete

Great blog and information.

Title  
Name  
Url
Spam Protection:
Enter the code you see:
If you can't read it, click your refresh button to get a new image.
Comments