gasp! I need some air, and was searching for it at - of all places- on google, semi-geek that I am, when I came across this website. Not quite what I was looking for, but refreshing nevertheless.

Friday, August 20, 2004

Java and Rocket Science

Yup - you guessed it. It's Friday again, which means my sanity is coming to a dismal end. So here's my personal way to vent it out.

Had an interview in the morning, and then came to work to find the same insanity in the air as always - people twiddling their thumbs and making money, some more than the rest, people throwing darts on the magnetic dartboard and missing the entire board, people listening to songs played through the tiny wimpy speakers of their sad computers.

So, now that I was in the interview mood, I asked myself - what would I ask someone on a Java interview? How about - "Launch a missile to the moon, in 3 lines of Java code"? In case you are wondering, here's the answer:
MissileLaunchingSystem myMLS = new MissileLaunchingSystem();
myMLS.setDestination (Destination.MOON);
myMLS.launch();
Note that you should use Destination.MOON instead of giving your own destination, because it's always better to use Java constants from the packaged classes than create your own. Also, the coordinates of the moon may be changed in future versions of Java, and this way you avoid having to rewrite your code every time that happens.

Of course, the import statements have been left out ("as an exercise to the reader"). We had a small discussion on this - I feel it should be a part of the javax.* package, since it's more of an extension of the Java language than something that's an integral part of it. So it should probably be javax.astronomy.RocketScience.*, or something like that.

Now, you should note that this code is not very good - it does not handle the exceptions properly. Here's a better way to handle the exceptions:

try {
.... {code as above}
} catch (NoMoreMissilesException e) {
System.err.println ("Sorry - no more missiles - please buy some more");
e.printStackTrace();
} catch (IllegalLaunchDestinationException e) {
System.err.println ("Sorry - destination not allowed!");
e.printStackTrace();
} catch (Throwable th) {
System.err.println ("Launch failed: exception in launching missile!");
e.printStackTrace();
}
The code is considerably longer, but then the user has a better clue as to why the missile was not launched. In case of time sensitive execution like this, it's very important to inform the user about the state of the launch, and the reasons as to why it failed. After all, this is not Citigroup's SDLC process for mundane finance work - THIS IS ROCKET SCIENCE!!

If you ever come across an easier way to launch missiles, remember where you read about it first!

0 Comments:

Post a Comment

<< Home