Sunday, October 5, 2008

Thank You, Java 5

Alex Miller polled Java users and a fair number are still using JDK 1.4. This post is intended for those folks: have hope, there is truly useful stuff in Java 5.

In this post, I will mention a couple of my favourites. These have been well-documented, and are not even the 'coolest' features, but my respect for them has been well-earned over the last few months.

Background

I've been part of a team that has been moving a project from JDK 1.4 to Java 6, and introducing Hibernate/Postgres to replace (in incremental steps) an existing, legacy framework that works with an OODB. This has been a serious refactoring and fairly risky. My analogy is that of putting in a subway transit system into a modern city: it is a massive job, but will be well worth it.

Ten months later, we appear (knock wood) to be finished with a major version of this migration. I've used the following Java 5 features before, but now I am truly fond of them.

The For Loop

When I first saw the new for loop, I yawned:


List<String> list = new ArrayList<String>();

// snip

for( String s : list ) {
System.out.println(s);
}


It was a nice bit of syntactic sugar, but I was unimpressed. Now, I can't stand to see an old-style loop. The new loop is so much cleaner. A major bonus for us is that it works with arrays:


String[] array = new String[]{ "abc", "def" };

for( String s : array ) {
System.out.println(s);
}


As we have refactored, the code literally looks washed and waxed afterwards. Very nice.

Generics

When I first saw generics in Java, I was unimpressed. With a background in C++ and other languages, I understood the point but felt that Java had lost some of its Smalltalk influence, and that the syntax was plain ugly. When I realized the "wall of erasure" (as coined by Brian Gilstrap), I was even less enthused:


List<String> list = new ArrayList<String>();

list.add("Cubs swept by Dodgers.");
list.add("any team can have a bad millenium!");

// on a static page, generics allows us to avoid the cast,
// which is boring, and compile-time checking.
// The biggest advantage is in a good IDE
// where this information comes alive:

String first = list.get(0);


Now, I am an absolute fan. Yes, the corner cases are sharp, and the FAQ is very long for a reason. Yes, we may well need reified types to get full generic capabilities. I don't care: the new static typing of collections saved our project. Seriously. We were wading through dozens of collections in the legacy code and would have never been able to untangle all the knots without Java generics and Eclipse. I repeat: generics saved us, and I am grateful.

The Upshot

We shouldn't suspend our critical thinking and skepticism when it comes to new language features. However, we should also believe that people are earnestly trying to add value when new features are proposed. For one, I am going to keep an open mind when it comes to looking at the new features for Java 7+. Who knows: the features which cause us to roll our eyes may well be our future favourites.

ps. This is another post but unit testing and integration testing (with dbUnit) was equally essential to our success. Of the bugs in new code, my guess is that 90% were in code that was not tested.

pps. We have barely used static imports, but Eric has done some cool things with them. They are underrated, IMO.

11 comments:

Casper Bang said...

How can you leave out Enum? The amount of conditionals you can factor out by its strong typing way of associating data is remarkable and I have yet to see a project that didn't benefit from it.

Michael Easter said...

Sure, Java 5 brings a lot of cool stuff including Enum, the concurrency library (huge), and other goodness.

This post talks about features which I didn't appreciate at first, but have come to respect in a profound way. I liked Enum from the start.

Anonymous said...

The for loop has the unfortunate(?) effect of using iterators, and iterators fail fast, and THAT means that any modification, or QUERY (get) of a collection with a for loop is enough to fail at runtime. Sure it's a programmer error most times, but not always.

Mark Hughes said...

Companies that haven't moved to Java 5 aren't staying behind because they're luddites, afraid of new technology.

They have working systems running on Java 1.4, often with dependencies on specific versions of WebSphere or other libraries, and millions, often hundreds of millions, of dollars riding on the stability of their systems.

Changing Java version "just because", when the existing one works, is an irresponsible business practice.

Martin Wildam said...

I wonder why not praying for Java 6 - For desktop applications there is for instance the tray notification icons and bubbles - just to mention something new.

Michael Easter said...

@ Anon. Good point on the implicit iterator. We were burned by that a couple of times.

@ Mark. I am not saying anyone is afraid or a luddite. The intent of this post is: if you haven't worked with Java 5, here are some features that I find useful, even though I originally wasn't enthused.

As Alex and others have pointed out, there are reasons people are still on Java 1.4. I'm not telling them to change "just because", or at all.

@ Martin. If possible, yes, a project should use Java 6. There are many aspects to Java 6 (and especially Java 5) beyond this post. Again, I'm just saying that I have come to appreciate the features described. I also think that the value of generics is often lost in the wave of (fair) criticism against the feature.

Frank Carver said...

My favourite Java 5 feature is variable arguments. Judicious use of them also gives code that "washed and waxed" feeling.

Shams said...

apart from the ones you mentioned I found a particular feature of generics quite useful - bounded type parameters with multiple interfaces separated by &.

We used it a lot in one of our swing apps. Unfortunately I don't see many people mention this feature while talking about generics :(

Joe Attardi said...

@Mark: Well, Java 1.4 reaches its EOL on October 30. No more security patches/updates/etc. If a company has millions of dollars riding on stability, they shouldn't be using an EOLed platform.

Anonymous said...

Probably one of my favorite features of Java 5 is the edition of annotations. Not only did it make my apps more useful, but it made my life immeasurably easier by enabling frameworks like XStream, JUnit 4, Guice, etc.

Admin.Mediocre-Ninja said...

Annotation turns out to be the most excellent feature of Java 5 .

Generics is pretty good, but not as expected. It falls into the Sun's infamous 'unnecessarily complicated' stuff, and the code might be messy.

Foreach style is nice, of course.


Will Dolphin recover some reputation for Sun ?