Rob Williams is a probabilistic Lean coder of Java and Objective-C. Rob is a DZone MVB and is not an employee of DZone and has posted 170 posts at DZone. You can read more from them at their website. View Full User Profile

Quick Look at Java 8

04.04.2013
| 2750 views |
  • submit to reddit

Wow, this video of doing Java 8 in IntelliJ got me all excited. I personally am not often swayed by 'you'll type less' arguments, and the idea that all coding is a series of iterations through lists or maps, but the compare stuff, especially, for anyone who's written a lot of Java, looks like a day pass signed by the warden to someone whose notion of the outside has been the prison yard for the last 15 years. I am coming around to the idea that 8 is going to rival or maybe surpass 5 as a release. (After the 8 show from IntelliJ, I pushed on to their TV screencasts and watched the one on Android dev. Seriously, that‘s the best commercial for Xcode there is. Wow, it‘s painful to watch. They should have low dubbed the soundtrack to Mayberry R.F.D into the background, it was so pokey and lacking crispness.. Can‘t imagine doing long tours strapped into that ox cart.)

Also read through the docs on the new time package in Java. The docs really read like the work of a team that is doing more state legislature sausage making than language design. My favorite part was when they say ‘we figured we should probably have classes for each abstraction, but then that would have been too many, so we compromised.‘ That‘s kind of like saying ‘well, someone could possibly try to cure cancer with our language, but that would probably require a lot of classes.‘ It does kind of raise an interesting question: if the flag is thrown in cases like this where we have known analogs already in the user‘s mind, what hope is there for doing truly complete object models in complex realms that many of the users are going to have to learn?

On the good side, the time package does look much nicer to use than the original JDK package or Joda. I like the readability of the example code, and it seems like it does shield some ugly details. They obviously have done a lot of date programming, as they say in passing that having to deal with Timezones makes things much more complex. That is so true, it‘s ridiculous. Not the least reason being that your code is most likely going to be getting the current timezone all over the place, so when you test it, it will return different results depending on where it is being run But then also, when daylight savings happens, you can get crazy results, like the offset from GMT goes nuts the day of the change because it was not DST at midnight, but then after 2 am it was. Here‘s there example:

    LocalDate customerBirthday = customer.loadBirthdayFromDatabase();
    LocalDate today = LocalDate.now();
    if (customerBirthday.equals(today)) {
    LocalDate specialOfferExpiryDate = today.plusWeeks(2).with(next(FRIDAY));
    customer.sendBirthdaySpecialOffer(specialOfferExpiryDate);
    }

I was thinking about some of these things today and was wanting to go look at using Categories for testing problems like this. For instance: suppose I have a class and I have the timezone inside. I don‘t want to put a setter on that thing so I can test it. However, in Objective C, I can add a category that is only in test scope that adds that method, my tests use it, and none of the production code will know it‘s there. We could do that with aspects in Java, but it‘s more work. And aspects are really not first class citizens.

 

Published at DZone with permission of Rob Williams, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: