Wednesday, July 1

5 Reasons to Insulate Yourself from Java Dates

Java's Date implementation sucks. That's no secret to anyone. Calendar sucks just as much... also no secret. They are difficult to work with and sometimes they make absolutely no sense at all. For instance, why are all the seemingly useful methods in java.util.Date deprecated? What is the real point of java.sql.Date, it provides little value and has all the shortcomings of java.util.Date. Then there's Calendar. A land where something like JANUARY=0 and SUNDAY=1 is perfectly logical. If you want to set a Calendar date, there's a nice method that does this, accepting 3 parameters, year, month, and day. To the uninitiated, should be perfectly reasonable to say:

cal.set(2009,1,1);

The only problem is, it is completely counter-intuitive to the way normal people understand date notation. The result is that a completely reasonable statement ends up setting the incorrect month. Additionally, the numbering method is inconsistent when using days of the week. The constant for Sunday is 1 instead of 0. Lets say you have all that straight, Calendar has one more counter-intuitive "feature". In the normal world, knowing that day values start with 1, what would you expect from the following code?

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 0);
System.out.println(cal.getTime());
cal.set(Calendar.DAY_OF_WEEK, 0);
System.out.println(cal.getTime());


I would personally expect an IndexOutOfBoundsException since days are supposed to start with 1. Not so. Calendar KNOWS you meant to subtract a day, so it nicely rolls the date for you. Which means you will end up with a day you probably didn't intend AND you could also end up with the wrong month and year. Nice.

Those are a few of many complaints about Java Date/Calendar. Do a Google search on "java date suck" and you'll get a longer list than you want to read, but I think I've made my point. At some point, whether via OpenJDK or by the JSR process or finally deciding to improve their API(not likely), this general crappyness is going to have to change.

The end result is a lot of ugly, non-OO, util code meant to overcome Date/Calendar's shortcomings. If you've been coding in Java for long enough, you've probably seen something like:

Date dt = DateUtil.newDate("01-01-2009");

It's better than doing the dance with Calendar to set the date every time, but to plagarize a saying from a colleague of mine, we're putting a band-aid on cancer here. Those kinds of things make working with dates simpler, but they don't actually correct the issues.

So, reason #1 to insulate yourself from Java dates... the implementation sucks and it's sucked since the beginning of time in Java.

Reason 2: Not everyone requires millisecond precision. If you only need precision to days, why use all the extra memory to pass around useless information for hours, minutes, seconds, and milliseconds. At that point you could use in an int instead of a long.

Reason 3: You can expose methods that make sense in your application or enterprise. If you don't need a "getTime()" method because you're never going to use a long, don't confuse other developers by providing it. If you are an architect and you put in methods you don't intend to use just because they are in the standard implementation, developers will use it and, if you don't intend for them to use it, they will probably end up with something like:

java.util.Date date = new Date(new MyDate("01-01-2009").getTime());

Let's face it, that's a Daily WTF in the making and you, as an architect, have created opportunity for a developer to write this kind of code by exposing methods you don't intend to use.

Reason 4: Your own implementation of Date allows you to wrap the best date implementation available and swap it in and out of your application. This idea is pretty familiar to most people who use a lot of third party libraries and some even like to do this for database adapters. Here's the thing... in reality, you're more likely to change your date implementation than your database. If you're date-related code is based on your own abstraction this becomes a lot easier.

Reason 5: It regulates date handling across the application/enterprise. No one has to question if we're using joda time, java.util.Date, java.sql.Date, or any other date-related functionality. We use our implementation... period.

It's simple to do, it takes very little code, and it will make your date handling code not suck. To me, that's worth the small amount of time it will take to implement.

Tuesday, June 23

More tech in a new kind of revolution

In the last couple posts I have singled out Twitter for the role it is playing in the conflict going on in Iran. Recently, the murder of a young Iranian woman forced another technology to the forefront, YouTube.

The YouTube example is a bit different. First, it's penetration is far deeper than Twitter. It's technological challenges are much more evident than those of Twitter. And they actually did something new. Before YouTube, streaming video on the web was sketchy at best. Still, for all it's popularity, YouTube has been utilized mostly by video bloggers, teens with webcams, and America's Funniest Videos on the web. Sure, there's the "9-11 truth" and "free energy" videos, but they are a very small minority in a sea of skateboarding dogs and toddlers performing their own version of the "Eensy, Weensy Spider".

However, last Friday, the battered opposition in Iran, as well as the rest of the world, found someone a rallying cry via YouTube and spread over social networks including Facebook and Twitter. A gruesome video of a young, relatively westernized woman in Iran who was shot and killed as she talked to a friend on her mobile phone.

The video was uploaded to YouTube and in less than 10 minutes it was world news. It could not have spread faster if the CNN cameras were sitting right there, broadcasting live. In less than an hour, "Neda" had become the new face of the opposition.

So, in the last two weeks, we've seen Twitter go from relative obscurity to a battlefield that governments of the world provide support to in order to keep it up. Some have even called for a Nobel Prize for the founders of Twitter because of it. We've seen YouTube go from hosting videos of bored kids talking to their webcams to hosting the type of video a cameraman of a major news network could go a whole career and never capture. It took conflict to force these technologies to the forefront. The real question is, what will be next?

Tuesday, June 16

War Has Been Declared

The last 100 years could easily be called a century that changed war forever. In each iteration we witnessed advancements in the art and technology of war that have never been equaled in the history of mankind. And with each iteration, we progressed our ability to destroy to a point where our capabilities were so alarming, simply the threat of war was enough to keep the peace.

Today we have a new war. It is a war waged on a very small battlefield with support from non-combatants around the world. It's a war of subversion, a war of information, a war with no boundaries, and a war with a new weapon. Twitter.

That's right, a week ago Twitter was a microblogging tool that most of the world had never heard of. Two days ago it turned into a lifeline for the Iranian people to tell the world what the mainstream media either would not or could not. By the end of the day yesterday, it had become a battlefield. For two days, while typical media sources were blocked by Iran's government, Twitter somehow had them stumped. While they could somewhat control what was coming in, they could not control what was going out. As people around the world began to pick up on what was going on, they started to run interference... creating anonymous proxies, setting up VPNs, and departing from generally accepted practices to protect the identities of those who were actually in danger. Twitter even postponed it's regularly scheduled downtime, realizing the gravity of the situation they had been drawn into by no action of their own.

Today, the opposition movement in Iran is hiding in plain sight, obfuscated by the noise created as people around the world flood the #IranElection topic at a rate that prevents my poor twitterfall view from keeping up.

Now the governments, always slow to pick up something new, have attempted to adapt. Instead of trying to prevent, the government in Iran has begun to create its own noise by attempting to lure those who desire to help but may not think about what they are doing, into providing information.

I continue to be amazed by the way events have been unfolding in this situation. My prayers are with those in Iran who are fighting a government they don't believe in and that they might one day be able to sit on a train, coming home from work and write whatever they want because they don't worry what their government might do about it.

Monday, June 15

Is Twitter Really Relevant?

My first impression when I learned of Twitter just over a year ago was something to the effect of, "Really? What would I do with something like that." I thought it was an interesting case for the ability of a dynamic language like Ruby to prove itself with some high volume transaction processing. However, until today, while I have followed a few of the software geeks I like to read from and posted some sarcastic comments of my own, @mikenishizawa has been pretty quiet and disinterested in Twitter.... until today.

Today I saw Twitter become the lifeline to the world for the pro-reform movement in Iran. Today I read about young people taking to the streets with a serious fear that they may be shot but feeling that their cause was worth the risk. Today, I have watched the #IranElection tag on TwitterFall, mesmerized by what I was watching.

Some of it was just voicing support, some of it was fellow geeks tweeting proxies to evade the Iranian governments attempt at censorship, some of it was was raw footage of what was happening on the ground by real people taking pics with cell phones. There was no Muslim and Christian. No talks of terrorism or political debate about a nuclear Iraq. These people are fighting for the basic right to have a say in their lives and how they live them... a right that we sometimes take for granted in the US, where these rights were afforded to us merely because we were born where this fight has already been fought.

It is a fascinating thing when a technology lives up to it's potential. It begs the question of why some technologies survive and some fail and why some never realize the impact they could make on the world? When you think about it, the idea of Twitter is not revolutionary at all. I've heard it called "IRC on a webpage" and I've recently complained about companies and projects who use it to broadcast updates like it's RSS. But today, "IRC on a Webpage" may just have been the thing that drew mainstream, world-wide attention to the struggle in Iran. Who knows, it may have even prevented a repressive, authoritiarian government from cracking down in an even more brutal way and censoring any chance of us seeing it in the West. People got a chance to interact with the news they were getting and got a chance to be a part of the protest even from the other side of the planet. As far as I'm concerned, Twitter's relevance was proven today.