The Artima Developer Community
Sponsored Link

Computing Thoughts
Singleton Considered Harmful?
by Bruce Eckel
August 27, 2007
Summary
In Java Posse #136, the Posse discusses the Google Singleton Detector which automatically discovers singletons in your code and they all agree that singletons are a bad idea.

Advertisement

Here is the link to the show notes.

This is the kind of thinking that Java seems to promote for some reason: there's one right answer that you should apply everywhere. It's certainly comforting to think that someone out there is being "the decider" and that you only need to follow those decisions without question. Another place I've seen this is in the cult of "interfaces everywhere," in which you mindlessly create an interface for every class in case, I suppose, you ever need it. Also, the attitude that more static type checking will fix every problem that we discover.

Design patterns are design choices that arise out of need. Clearly, using a singleton as just another global variable is a bad idea, but does this make singleton itself a bad idea? Sometimes you need a "single point of truth," a registry where you go to get information that is used universally. This isn't a casual choice, but it happens, and when it does a knee-jerk reaction that says singletons are bad is not going to help. Even Dijkstra's famous "goto considered harmful" that has been the genesis of these kinds of diatribes is qualified -- he was talking about unconstrained gotos, otherwise Java's labeled break and labeled continue would be considered "bad."

Talk Back!

Have an opinion? Readers have already posted 34 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Bruce Eckel adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Bruce Eckel (www.BruceEckel.com) provides development assistance in Python with user interfaces in Flex. He is the author of Thinking in Java (Prentice-Hall, 1998, 2nd Edition, 2000, 3rd Edition, 2003, 4th Edition, 2005), the Hands-On Java Seminar CD ROM (available on the Web site), Thinking in C++ (PH 1995; 2nd edition 2000, Volume 2 with Chuck Allison, 2003), C++ Inside & Out (Osborne/McGraw-Hill 1993), among others. He's given hundreds of presentations throughout the world, published over 150 articles in numerous magazines, was a founding member of the ANSI/ISO C++ committee and speaks regularly at conferences.

This weblog entry is Copyright © 2007 Bruce Eckel. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us