Incrementalists and Completionists.

My good friend Justin sent me this article insightful article titled “Incrementalists vs Completionists“.  This seems to neatly highlight one of the ways that smart people can end up disagreeing about the next steps in a situation.  I recognise a little of both traits in myself, and it also relates to the “Perfect is the enemy of the good” aphorism.

Web Frameworks in Java

I’ve been working on a small web side project lately. Being a mostly Java oriented developer – I wanted to build a java web-app. It’s been about 2 years since I worked on a web-app full time – at which time we were using Struts 1 point something and jstl.

I did a little digging around and found a couple of blog entries and documents that talk about web framework comparisons…

Unfortunately, none of these resources yielded a clear winner. The one thing everyone does go on about consistently is Ruby on Rails. Nobody seems to be happy with Java Server Faces, although there seems to be hope for the future. The middle ground seems to be filled with Tapestry, Struts 2 (the artist formerly known as WebWork) and Spring MVC (step by step guide here). There also seems to be some interest in Stripes.

Given that the Spring folks seem to have a good, clean, simple approach to things, I thought I might go that way for my app. Having said that, I quickly learned that just about all the web frameworks now use spring as their back-end container. Glowing praise indeed. I invested a couple of hours in downloading and unpacking the latest jars for frameworks, jdbc drivers etc, before I had a change of heart. Since everyone has been going on about Rails for the past year or so, why don’t I give that a try??

My first look at rails was watching the videos on the Rails website. These do a pretty good job of demonstrating how to start doing something with Rails. This blog entry assisted with getting rails installed on my Fedora server, and before long I was rocking along.

It seems like rails is basically the “most applications work like this” pattern but with automation scripts. I didn’t have to write a build.xml file, or write a task for exporting the db schema; write something to make spring load or anything else. The first thing I did was build a db table, populate it, and then build a web page to interact with it. The time I did spend was on building useful functionality: making it call Google maps and get geographical data, and then making the Javascript event model work on the client… things that I regard to be complex (or at least – highly visable). Prior to “giving up” on java, all my time had been spent plumbing. This is not to say java is bad…. it’s just that the rails framework took care of all the easy stuff by itself.

It’s obvious to me that I will pay a price for the ease of startup later on, but if you wanted to build a web application to see if it “sticks”, you don’t particularly want to pay the startup tax – you want to see if it’s worth investing any real time on it. IMHO – this is where rails is killer… from idea to implementation is much quicker than it would be with Java; and this leaves more time for what is often the harder pain: figuring out the requirements and user interaction.

The learning for the java folks IMHO is that some application layout standards need to be agreed upon, and then a standard application skeleton (or skeleton builder) needs to be published. We need to get to a point where you can do something like scrip/generate controller and have something generate a new Controller, View and supporting configuration for you, so that less time is spent doing the same old grunt work. Hopefully it’s already out there and I just haven’t found it yet :).

Singletons & Double Checked Locking

I regard singletons as being perhaps the dirtiest of the GoF patterns. It’s problematic to assume there would only ever need to be one of something. As an example – the project I am currently working on has a client side application which has a series of singletons that facade server functionality. It turns out that this works very well, except there’s no way to renew these singletons if the user wants to log out of the server and log in again with a different user account. This means currently, the application has to be closed and reopened. This isn’t a show-stopper, but it is annoying.

Regardless of this, implementing thread safe, lazy initialized singletons in Java has always been problematic due some problems with the Java Memory Model. This is covered in detail in this excellent developerWorks article. I was mistakenly under the impression that this had been fixed in Java 5, but alas – it appears not. What I wasn’t aware of until today is that there is an alternative thread safe manner in which to implement a lazy-initialising singleton:
Initialize on demand holder classes.

I think the better approach nowadays however is to wire your application together using the Spring framework. Spring is very lightweight to implement, and will take care of initializing your singleton-like objects in order and wiring them together. Since spring offers full object life-cycle support, it’s easy to have it close all your objects out, and re-initialise with new ones, should you desire that.

String.format()

I ran across a bug in some code today where a developer had set up a template string similar to the following:

"{1}:{2}/{3}"

Subsequently they had used a series of replace commands to fill in the values.

String.replace("{1}", "foo");
String.replace("{2}", "bar");
String.replace("{3}", "baz");

It seems that this code is sub-optimal for two reasons. First, it’s not good from a performance point of view, and secondly, it could fail if the first string replace inserted {2} into the string. This would cause the second replace to do two substitutions which may or may not be desired.

I didn’t delve too deeply into the reasons for this, but the debugger showed this code as outputting a string which looked like: “foobarbaz{1}:{2}/{3}” which was the source of the bug. I went hunting for better solutions.

Knowing that Java 1.5 has had printf() functionality similar to C added to it, I wondered if there was an equivalent of sprintf(). It turns out that there is – it is a static method on String called String.format() (javadoc linked). This is a tremendously powerful tool in C, but the java version also allows more sophisticated options like pulling various components of date objects out. The full range of possibilities are documented here.

The new code looks much cleaner IMHO:

String.format("%s:%s/%s", "foo", "bar", "baz");

You should also note that printf() itself is now available on PrintWriter objects such as System.out.