blog.smart-java.nl
Ordina J-Technologies – Java Blog

Archief voor May, 2009




What is OSGi? (short version)

Door: Stephan Oudmaijer, 29 May 2009

Since modularisation is a hot topic for Java 7, most developers by now should have a basic understanding about what OSGi is. But I often get the question; What is OSGi?

OSGi is The Dynamic Module System for Java[TM]. OSGi enables Java applications to become modulair and express dependencies on (versions of) other modules. Which means that your application no longer depend on the classloader (not) taking care of dependencies. There are a number of issues OSGi addresses, but I wont even bother writing them down here since Peter Kriens has an excellent blog post about those issues.

Java 7, and in particular JSR294, also tries to solve the modularisation problem. The expert group is working with the OSGi alliance to implement modularisation in Java, but they still have a long way to go. If you are interested in the progress of the expert group you can follow the JSR294 EG mailing list.

Below is a list of resources about OSGi:

  • http://www.infoq.com/presentations/osgi-the-foundation
  • http://www.infoq.com/presentations/colyer-server-side-osgi
  • http://www.infoq.com/presentations/dm-Server-Rod-Johnson
  • http://www.osgi.org/blog/2008_12_01_archive.html
  • http://www.theserverside.com/tt/articles/article.tss?l=MigratingPathToOSGi

Now you know!




Compare with generics please..!

Door: Roy van Rijn, 18 May 2009

I’ve been developing with Java 5+ for quite a while now. Not all developers are this lucky, some are still stuck with 1.4… some even with 1.3! But my clients all made the excellent step forward to Java 5 (some even to 6). The problem is, they moved the runtime/JDK but most clients forget to move their developers!

The language brings some good improvements, the for-loop is easy to understand, and almost all the developers are using this by now. The problem starts with generics. There is a part most developers understand, the Collections API. Almost all programmers use lists now as: List<Integer> instead of a plain old List. This is a good start, but it must not end here! First, I must admit, generics in Java can sometimes be hard and confusing (when using <? extends X> and <? super X>). So I’m not talking about the hard stuff. Its the use of ‘easy’ generics that can be extended to make life so much easier.

For example the piece of code below:

public class LabelPlaceholderComparator implements Comparator { 
 
    /**{@inheritDoc */ 
    public int compare(Object o1, Object o2) { 
        LabelPlaceholder p1 = (LabelPlaceholder) o1; 
        LabelPlaceholder p2 = (LabelPlaceholder) o2; 
        return p1.getLabel().compareTo(p2.getLabel()); 
    }
}

Of course, there seems to be not much wrong with the code, I see it all the time. Yes, the code breaks if you put something else in the comparator, but hey… the Javadoc says it only accepts LabelPlaceholders! So lets use this code:

List<LabelPlaceholder> holders = fillList();
Collections.sort(holders, new LabelPlaceholderComparator());

Done! Its working and no problems right? Wrong. The IDE complains about this code. For example Eclipse shouts:
 
Type safety: The expression of type LabelPlaceholderComparator needs unchecked conversion to conform to Comparator<? super T>

At this point, most programmers at the company I work for now will just ignore this warning. They might even add:

@SuppressWarnings("unchecked")

What a shame… First of all, what is Eclipse trying to tell us here? The compiler doesn’t know we created the Comparator with only LabelPlaceholders in mind. But the compiler does know (with generics) that the List only contains LabelPlaceholders. So the warning is (in understandable English):

I’ve got a list here of T (LabelPlaceholders) and a Comparator for Objects, this can go wrong! I’d rather have a specific Comparator for this job. Do you have one for me?

The solution to this problem is very simple, but most neglect to use it:

public class LabelPlaceholderComparator implements Comparator<LabelPlaceholder> { 
 
    /**{@inheritDoc */ 
    public int compare(LabelPlaceholder p1, LabelPlaceholder p2) { 
        return p1.getLabel().compareTo(p2.getLabel()); 
    }
}

As you can see, the code is much smaller. The interface is now generified, it knows we are going to compare LabelPlaceholders now, nothing more, nothing less. Also, we don’t have to cast anymore, because of the generics you can’t put anything else in there.

So, lets go to the conclusion: Why is the latter code better code?

  1. As you can see, the code is smaller!
  2. There are no casts, the code is safer (no ClassCastException or eleborate class checks)
  3. If somebody uses your code, he/she knows what kind of objects the Comparator can handle. You don’t have to read the Javadoc or the code to see what it does.

Throughout the projects I encounter I keep finding examples of places where generics would have made the code smaller/safer/more understandable. For some reason the programmers still only use generics on collections. So, even though generics aren’t perfect, please use them where/when you can, it’ll always add clarity to the code, and most of the time it’ll also make your code safer, and in some cases the code gets smaller because you can leave away casts and class-checks.

Don’t ever let me see public int compare(Object o1, Object o2); again! :-)




Book: “The Inmates are Running the Asylum”

Door: Vincent Hartsteen, 17 May 2009

In my previous posting Can software developers build usable GUI’s? I mentioned that I was reading The inmates are running the asylum written by Alan Cooper. During my vacation in Italy last May I’ve finished reading the book. The fact that I read this book while I was relaxing in the sun on a campsite at Lake Garda may sound like a detail that isn’t worth mentioning. But actually I think it is. I’ve tried to read IT related books before while being on vacation but I never (I repeat, never) managed to finish the book. Dan Brown or John Grisham can keep my attention during a vacation but Martin Fowler or Mark Cade fail to do so. But Alan Cooper has written The inmates… in a very entertaining manner and I have enjoyed reading it.

But what is the book about? Just like About Face. Essentials of User-Interface Design the book is about the usability of software based products. In The inmates are running the asylum Alan Cooper focusses on the role of the Interaction Designer (which should not be confused with a GUI Designer). He states that in many projects the focus is on adding features instead on fulfilling (real) end-user goals. The reason for this is the way in which software-engineering is organized. The inmates (that is us, the developers) are running the show (and we shouldn’t) according to Alan Cooper.

Many software-based products are so-called Dancing Bearware. At first it is fun to see a Bear moving around as if it is dancing. But after a while you’ll notice that the bear isn’t dancing that good. And is it really dancing? The same goes for many software-based products. In a demo it really looks nice and seems to work fine. But after a while when you have a closer look you’ll notice that working with the product isn’t that fun anymore. Recognizeable? To me it is (have a look in the box with all the nice gadgets you had over the last few years…).

Cooper gives many examples of where software-based products fail to support its end-user. However he doesn’t leave it at that. He points out where in his view the real problem lies and what can be done about it. He describes a method that his company uses to improve the interaction between humans and software-based products. After explaining the basics of this method (where he introduces personas) he gives some examples of projects where he used his method. In my opinion this method can be combined with RUP (personas instead of human-actors). Coopers method isn’t the silver bullit with which we are guaranteed to build perfect applications. But it shows that software-based products should also be looked at from a point-of-view that isn’t natural for programmers. This is where the Interaction Designer comes into play and where the organization needs to be changed so that this role is fitted in the correct spot. And they should be running the asylum, or at least have a lot more influence, instead of us…

Like I said before, the style of the book is very entertaining and on top of that it gives a nice insight in how software-based products can fail. I would suggest this book in addition to all the hard-core technical books we have on our bookshelves.