Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Wednesday, August 27, 2008

Singletons; those silly little Singletons

Miško Hevery recent series of articles about Singletons & singletons (note the capitalisation) is one of the best discussions of the topic I have read. In the latest article he has hit the preverbal nail on the head with the distinction between a Singleton & a singleton.

Skip to the end for a quick summary

I have worked with a lot of code containing a lot of Singletons, many caused by my own hand, and they have one thing in common. They all sucked. Sorry let me quantify that horrendous statement; the fact that the object(s) were Singletons made working with the code in an environment other than production nigh on impossible. The underlying code was sound. What was using the Singleton.getInstance() accessor rather than forcing code to pass a reference to the object which at one point so happens to be a singleton. Imagine these scenarios:


public class MyClass {
private final Singleton sing = Singleton.getInstance();
}


public class MyClass {
private final Singleton sing;
public MyClass(Singleton sing) {
this.sing = sing;
}
}


Wow so it's the same overall effect (an instance of Singleton makes its way in MyClass) but the second one is just plain better. Get your Singleton to implement an interface & then you've succeeded in a decent level of separation (which is what my group did with the last Singleton). But there was one thing that I was confused about (but did not know) & it's very similar to people who hate Javascript. A singleton (a single instance of an object) is a very good thing; a Singleton (a globally held reference to a single instance of an object) is not & never will be. However a program needs a context to run from (which is what the Singleton was being used for).

Just think about something like Struts (1.x); there's only normally one instance of an ActionServlet per application. A context if you will. This is where Miško's article has really driven this point home. A single instance of an object is no bad thing; in fact it's normally quite a desirable thing. What hurts you is the Singleton anti-pattern. It becomes gospel; the alpha and the omega and trust me you do not want to program yourself into this cul-de-sac.

Anyway to summarise Singleton bad; single object instance good.

Tuesday, March 04, 2008

Wow ... seriously?

I've just had the misfortune to read an article about how to use Java from Perl. Maybe I am missing the point here but that sounds like one of the most awful things you could possibly do. I've spent the past 5 years working in bioinformatics (an industry where Java & Perl are the major players outside of algorithm work) and not once have I ever thought "I wish I could use my Java in Perl".

It's amazing really but I've encountered this exact problem of Java to Perl communication over the past 3 weeks the final decision was to communicate using JSON & then leave Perl to work over the incoming JSON data. This works brilliantly and we have a very clear separation of concerns between Java & Perl. Java gets the data, passes it into Perl & then Perl can manipulate it to it's hearts content. It's even good for testing since we have canned JSON files with data.

Now if it was a Perl implementation in the JVM I think I'd be more interested; but for the moment I think I'll pass on these modules.

Tuesday, February 12, 2008

A Perl of Wisdom for Me

Every so often I find a really nice construct I didn't know existed in Perl and I can never remember it. Well I'm going to post them here and see if it kicks my memory into gear.

Copying an array

my $array = [1,2,3]; # From somewhere
my $copy = [@{$array}]; #De-reference array into an anonymous array & assign

Populating a hash from an array via a slice

my %hash;
my @keys = qw/ key key2 key3 /;
@hash{@keys} = (); #Populate hash with keys and no values

#Populate hash with keys from array and values from same position in the assigned list
@hash{@keys} = (1,2,3);

Copying one hash to another

my $input = {key => 1, key2 => 2, key3 => 3};
my %target = (key, 4);
#And now for the copy magic
@target{keys(%{$input})} = values(%{$input}); #1 deref is better than 2 but is ok for tiny calls

Anyway I hope this is of use to someone other than me. If not then it'll be of some use to me.