{ by david linsin }

May 28, 2009

More JDK 7 Language Changes

A couple of month ago, Joseph Darcy announced the first six new language features for JDK 7. Yesterday, he posted another 7 proposals, which Sun will take into further consideration:

1. Byte and Short Integer Literal Suffixes by Bruce Chapman

2. Binary Literals by Derek Foster

3. Underscores in numbers by Derek Foster

4. Language support for JSR 292 by John Rose

5. Indexing access syntax for Lists and Maps by Shams Mahmood Imam

6. Collection Literals by Joshua Bloch

7. Large arrays (revised) by James Lowden

Joseph says:

All the selected proposals were reviewed and judged to have favorable effort to reward ratios and to preserve the essential character of the language.


I think "Collection Literals" and "Indexing access for Lists and Maps" will considerably improve my daily coding life as an average Joe developer. Furthermore, "Underscores in numbers" could really improve working with numeric data. Here are a couple of code snippets from the proposals:

6. Collection Literals:
final List<Integer> piDigits = [3, 1, 4, 1, 5];
final Set<Integer> primes = { 2, 7, 31, 127, 8191};


compare that to the current way of creating a immutable List:

final List<Integer> piDigits = 
Collections.unmodifiableList(Arrays.asList( 3, 1, 4, 1, 5));


I really like this proposal, it'll make life so much easier. I use the notation above almost in every JUnit test to create some sample data in a List or Set. It's so much cleaner and more readable using the proposed syntax.

5. Indexing access for Lists and Maps
List<String> l1 = ["a", "b", "c"];
String firstElement = l1[0];


I like this notation, it's concise and feels very natural. On the other hand, I don't really mind writing List.get(0). Actually, this reminds me of Scala, where you can also say something like:

val l1 = List("a", "b", "c")
val firstElement = l1(0)


3. Underscores in numbers
int phoneNumber = 555_555_1212;


This proposal is supposed to increase readability and everything that accomplishes this should be added to Java. However, I believe, you have to find the middle ground and only apply conciseness, where it makes sense and doesn't hurt readability, but that's another story.

Most of these proposals are only nice additions and are not really essential. However, I think a lot of them are going to make my daily coding life more enjoyable. I'm excited to see them implemented and hope there will be a JSR soon.

May 25, 2009

Make it Easy to Contribute

The past couple of weeks, I've been playing a lot with Restlet, a Java based REST framework. I like the it a lot, because it's easy to use and you can get it up and running in no time.

The current version of Restlet is 1.1, but when I compared it to the upcoming version 1.2-M2. it was clear that I would start using that right away. It has built in Google App Engine support and uses an Annotation driven approach, which increases testability and ease of use. This is one of the rare uses cases, where I think Annotations are perfectly suited, but I'm digressing. Overall, it was a natural choice to use the milestone build 1.2 right from the beginning.

After a couple of weeks of coding against 1.2-M2, I started to encounter a couple of weird side effects. After testing the code against the current stable version, it was clear, that it must be a bug in the 1.2 milestone release. So I checked the Restlet site, which nicely describes the process of reporting bugs.

Before entering a new report, you should query the current issue database for similar open issues.


As a decent developer, I followed the link to the issue tracker and saw this:

srewed_tracker.png

I was shocked to see this kind of interface! I know I'm a developer and I should love these kind of fine grained and detailed options to query an issues tracker, but that's too much! It's not the first time that I see this interface, Java.net uses it too and I just think it's a PITA to use! Compare it to google's code hosting issue tracker interface:

google_code_tracker.png

It's worlds apart in terms of usability and ease of use! And the screenshot above even shows the "advanced" search mode. In most cases, you can find everything with a single search box - google like!

I think, if you want the community to interact and contribute to your project, make it as easy as possible for them. They are trying to help you! We all know the problem of issues reported multiple times or reporters not taking time to correctly file an issue. However, every feedback is valuable and you can always ask for further and more detailed descriptions, if necessary.

Coming back to my bug in Restlet, after I managed to query the issue tracker, I saw that it had already been reported. So I decided to switch back to the current stable version. Unfortunately, I had to jump through a couple of hoops to make it work on the GAE, but that's another story altogether.

May 18, 2009

Is JavaFX Really the Future?

javafx_java_net_poll.png

A java.net poll, which took votes over the past week, shows that a lot of developers are not quite sure, if JavaFX is the way to go in Java UI space. The "Editor's Daily Blog" tried to blandish the numbers:

The first thing to note is that more than 600 votes were cast. That means that this poll stimulated more community participation than any of the other recent polls. Does this matter? I think so. I think it suggests that, whatever may people think about the future of JavaFX, JavaFX is something that has captured their interest. People are following JavaFX.


To be fair, I voted for "It will endure as a secondary option". I'm not too sure if JavaFX will take off in the future. There was a lot of fuzz about it at last year's Devoxx. I talked to a couple of people back then, but all of them had different opinions. Some of them liked it, other were sceptical. However, the general opinion was "We'll have to wait and see!".

I rarely push out this kind of bash blog posts, but with Sun putting a lot of resources into JavaFX and this significant developer refusal, I think it's worth mentioning it.

May 13, 2009

Handling Concurrency in Domain Models

I'm working on a project right now that has a rich domain model, which we plan to use in different environments. One group of consumers are mobile clients, the others are highly concurrent servers. We try to cater to both environments in terms of parallelization, as well as keeping the domain model free of technical details.

When I design a domain model, I tend to keep it as technology independent as possible, that's probably the reason why I'm not the biggest fan of Annotations. You kind of lock yourself into a certain type of technology and you probably introduce dependencies on 3rd party code. Overall I think you are polluting your core classes with details that don't belong there.

One of those technical aspects I'm referring to is concurrency. You can either choose to prepare your domain model for a concurrent environment or leave it to the consumer of the core classes. Take a look at the following class as an example:

public class Foo {
private Map<Long,Bar> bars;

public Foo() {
bars = new HashMap<Long,Bar>;
}

public void addBar(Long argId, Bar argBar) {
bars.put(argId, argBar);
}

public Map<Long,Bar> getBars() {
return bars;
}
}


Obviously, this class is not meant to be used in multithreaded code. There are a couple of issues, which could lead to an unpredictable state of the Collection bar. Here's another version:

public class Foo {
private final Map<Long,Bar> bars;

public Foo() {
bars = new HashMap<Long,Bar>;
}

public void addBar(Long argId, Bar argBar) {
bars.put(argId, argBar);
}

/**
* @return immutable Map implementation
*/
public Map<Long,Bar> getBars() {
return Collections.immutableMap(bars);
}
}


As you can see there are a few easy things, you can do to prepare this class for usage in a parallelized environment. In fact, I think you should always do them, even if your code won't run in multiple threads. However, this code is not thread-safe either and you need to jump through a couple of more hoops until it is:

public class Foo {
private final Map<Long,Bar> bars;

public Foo() {
bars = new HashMap<Long,Bar>;
}

public synchronized void addBar(Long argId, Bar argBar) {
bars.put(argId, argBar);
}

/**
* @return immutable Map implementation
*/
public synchronized Map<Long,Bar> getBars() {
return Collections.unmodifiableMap(bars);
}
}


I think this version gets pretty close and you could call it thread-safe. I say close, because a) I'm not Brian Goetz and b) we don't know more about Bar. However, we can be sure, that there shouldn't be any problems with the Collection bars. You can add new Bar instances and retrieve them concurrently without affecting the Collection itself in any way.

The price we paid for this is clarity. The code gets a bit more complicated. I know there are other ways to make the Collection bar safely concurrent modifiable, e.g. using a ConcurrentHashMap. The point is, that if you have a complex and fairly large domain class, making it thread-safe would mean littering it with all those technical details, mixing responsibilities and making it way more complicated to maintain.

So where is the right place to handle concurrency?

While researching this topic, I found a great discussion on stackoverflow, where Alex Miller suggests the following:

Start from the data. Decide which data is explicitly shared and protect it. If at all possible, encapsulate the locking with the data. Use pre-existing thread-safe concurrent collections.

Whenever possible, use immutable objects. Make attributes final, set their values in the constructors. If you need to "change" the data consider returning a new instance. Immutable objects don't need locking.

For objects that are not shared or thread-confined, do not spend time making them thread-safe.


This doesn't answer my question directly, but it suggest to handle concurrency on the level of data. That means using the java.util.concurrent classes in your domain model and only for data which is shared between threads.

So what I take away from all of this, is that, it isn't really a question of where to handle concurrency, but rather when. I think if you can minimize the technical pollution of your domain model to the data that needs it, you can contain the damage of clarity. With the java.util.concurrent Collections you have an abstraction that hides the concurrency aspect quite nicely. I guess the combination of both is the way to go: Only optimize data that needs it, with a good abstraction, that hides the technical details.

May 04, 2009

springOne 2009 recap

Last week I was at springOne 2009 in Amsterdam, Netherlands. Unfortunately the city left a bigger impression than the conference. There were a lot of people from SpringSource and quite a few interesting sessions, however I didn't get into the spring feeling that I was hoping for.

The most outstanding announcement for me was something called "Spring Roo". According to Ben Alex, it's

...a sophisticated round-tripping code generator that makes it quicker and easier than you've ever imagined to create and evolve Spring applications


You can leverage it to create applications with technologies like Spring, JPA and Spring MVC, using a shell-based approach with tab completion, hints and great default behavior. A big part of the opening keynote was dedicated to Spring Roo, where Ben live coded a simple web-based voting application. You can already download an alpha release and check it out.

Overall the keynotes were the most valuable sessions for me. Most of the others had a rather introductory character and were quite basic, which I think should not be the focus of such a conference. I'm a little disappointed, but nevertheless, I hope next year will be better.

com_channels

  • mail(dlinsin@gmail.com)
  • jabber(dlinsin@gmail.com)
  • skype(dlinsin)

recent_postings

loading...