Monday, March 29, 2010

Use Java Generics ...

I sense that the world of Java developers is rather divided on this, but it is imperative that you understand and use Java Generics effectively. Pick up the very readable Naftalin/Wadler book, Gilad Bracha's introductory paper on java.sun.com and try to read the gist of Angelika Langer's FAQ to get to the bottom of it. Do it now.

There is no provision for this post if above is the only thing it were to say. Google is there to help you find these references. I wanted to point out a minor advantage of using Generics in Java code. All the major advantages like catching the errors as soon as you make them is covered nicely in the above references. Here is that minor time-saver:

If you use raw collections (e.g. List and not List<String>), it is rather time-consuming to find what they contain.
Of course, one could argue that one should check Javadocs for that. But in a state-of-the-art IDE like IDEA and a modularized build, you generally tend to look at the source and in deeply rooted call trees, it is not immediately clear instances of what type are added to a List. So, consider using generic types.

No comments:

Post a Comment