Wednesday, 20 August 2008

Singletons are misunderstood

This post seems to echo a meme thats been floating around the developer communit for the last year : Singletons are bad. ( If you don't know what a single is, check the wikipedia definition ) .

Now I disagree with this sentiment - singletons are not pathalogical liars, they're just misunderstood. Or, more accurately, they're frequently misused or misapplied. Often you see singletons used when a developer just wants to be lazy and do something in one line. Take the example from Mr Hevery's blog :

testCreditCardCharge() {
  Database.init();
  OfflineQueue.init();
  CreditCardProcessor.init();
  CreditCard c = new CreditCard(
    "1234 5678 9012 3456", 5, 2008);
  c.charge(100);
}


Database, OfflineQueue and CreditCardProcessor are both singletons that shouldn't be . In the example of Database you'll run into trouble with that mechanism extremely quickly if you ever need more than one database connection at a time.

However take this piece of code.

testScreenCapture() throws InterruptedException {
  ScreenCaptureRobot.setInterval(1000);
  ScreenCaptureRobot.start();
  Thread.sleep(1500);
  assertTrue( ScreenCaptureRobot.isRunning() );
  BufferedImage image = ScreenCaptureRobot.getLastScreen();
  assertNotNull( image );
  ScreenCaptureRobot.stop();
}


ScreenCaptureRobot is a utility class for taking screenshots at timed intervals. It should be a singleton - you wouldn't want multiple instances of this running (it would rapidly hog resources, on windows at least). It should also be coded with the following guidelines :


  1. You must comment and do so correctly.
  2. If you must call an init() method before calling a method then say so in that methods documentation.
  3. If a method is called when it's inappropriate to do so then it should return informative errors, not ambiguous general java exceptions.


In conclusion I don't think Singletons are pathological liars, they're just misunderstood. It's true they are too easy to misuse, and extremely easy to shoot yourself in the foot with, or at least other members of your team.

Friday, 15 August 2008

Ming the Applet

Can the Applet take on Flash and even kill it? Or will it become like Ming the Merciless, forever trying and failing ?

I must say I like the way things are going for the applet people. I became convinced this morning when I saw this posting in Joshua Marinacci's Blog about replacing the applet loading screen. It seems applets may soon be the John Travolta of the browser world, especially with java 6 update 10 coming real soon now.

When you add JavaFX to all this then applets are well and truly going up against flash in the browser space. But I have to ask the question : what is flash being used for that we want applets to do ?

1. Show annoying adverts with mosquito noises that turn users away from the page before they've read anything
2. Make the next StrongBad
3. Produce web games that help you not work on a Friday afternoon

I think the indie gamers will take care of number 3 and we're bound to see applet games making a comeback. I don't think we're going to see applet cartoons anytime soon, and I hope most java developers would rather gouge out their eyes than work on an irritating applet add that asked you to punch a gorilla or something.

I think (and hope) we see something new - desktop like apps within a browser. And, if I may be so bold, I'd like to coin a new term here and call them WebTop applications. Something richer than html/css/javascript stack, more consistant than flash and stabler than.... well applets as they used to be.

Go applets go !




On a side note, I think I should fess up and publicly recant my doubts about JavaFX that I've expressed to friends and colleagues . I keep seeing new posts, new articles and the buzz is building. It think JavaFX is going to make it and Sun isn't going to abandon it next Java One. I even saw a "Whats wrong this JavaFX feature" article yesterday, a sure sign of critical mass in developer mindshare.

Friday, 8 August 2008

What am I ?

I've got a blog ! Why ? Because I there are thoughts, musing and ideas I have that I can't cajore or force other people to listen to.

So I've just done the new user thing and edited by profile. All the usual stuff (gender, age, color of pants, etc) and, of course, "Occupation". Now I write computer software for a living. This used to be called Computer Programmer, plain and simple. But these days it's called Software Engineer, Application Developer or even [langauge of your choice] Guru by agencies or employers trying to appear more hip and down with da kidz than they really are. I liked Computer Programmer - it was descriptive, simple and dignified. I don't really consider myself an engineer and I think developer sounds a bit like a building company.

So what did I choose ? Elite Coder . I think that does it nicely.