Home > English > End of the world post mortem

End of the world post mortem

As we all know, on last Friday world was supposed to end.

Luckily your friendly neighbor Java programmer found in the internet a piece of code responsible for running that, left by Evil Mayans, which looked like this:

public class World {

	private WorldDestroyer worldDestroyer;

	public void end(){
		Date endOfTheWorldDate = worldDestroyer.getApocalypseDate();
		if(endOfTheWorldDate.equals(new Date())){
		}//no need for ending bracket as the world ends :D But Java compiler does not know that :( Feathered Snake damn you holy Sun!

So what our hero could do?

First of all he could delete the if check or even remove whole World class, however that would most likely fail some tests and would result in alarm raised by Evil Mayan CI server. They would know that something is wrong,  fix the code, hence the world would end!

So he had to be smarter. Our hero sat and thought for some time, but not for so long, remember The End Of The World is coming! Using all his Java knowledge and team work experience he did the change!

public void end(){
		Date endOfTheWorldDate = worldDestroyer.getApocalypseDate();
		if(endOfTheWorldDate.equals(new Date())){

So what he did? He used one of biggest flaws of java.util.Date MutabilityYes, you may change internal state of Date using one of it’s method. No need for some sophisticated techniques.

I’m doing all of Java interviews in DC, and it astonishes me how many people don’t know that this class is Mutable. Yes, after instantiating it, you may change it internal value freely, which may lead to a lot of problems.

It surprises me how many people does not know about it. During last interview, right before Christmas, Senior Java Dev (with >5yrs of experience) tried to convince me that the class is immutable and that may be one of it flaws.

And what’s the big deal?

You should never trust in Date object, you’ve shared or received from external World (in OO terms -> outside your class).

It is possible to build anything on top of java.util.Date. In most cases, no one will try to harm you, but I would not build large application / framework on top of that assumption. So while working with Date, you’re going to learn everything about defense programming. Quick enough you’ll understand that it’s not enough to just copy input parameters, but you also have to return defense copies while returning values.

But okay, let’s stop bashing Date class for a moment. If you want to learn more about techniques that may be useful to work with it, please check the book that EVERY Java programmer should read: Effective Java by Joshua Bloch. It may not have anything about Date class, but it for sure shows how to achieve immutability and how to work with mutable code.

Other good solution is to use 3rd party library, like e.g. JodaTime It will solve a lot of your problems. It’s also quite popular, so most of the frameworks will provide support for it’s classes.

Ok, let’s investigate second flaw of this code. It’s not testable.

You just can’t test code that is dependent on time. It has to fail from time to time, raising false alarm. If you assert based on year, it may fail at midnight every 5-8 years. If you assert on month, it may fail before payday, etc. But don’t worry, the battle is not lost yet. The solution is trivial: create an external class called Clock / TimeService / TimeManager / DateFactory or something along the line, consistent with you’r project naming convention, and just move instantiation of Date there. What we achieve this way? You can switch implementation of that class for test purpose and you won’t get broken CI builds from time to time :)

So how it should look like in the end? (at least this variant of code)

public class World {

	private WorldDestroyer worldDestroyer;
	private Clock clock;

	public void end(){
		Date endOfTheWorldDate = worldDestroyer.getApocalypseDate();
Now much better. Good that evil Mayans did not know about it :)
Categories: English Tags: , , ,
  1. prad
    28/12/2012 at 22:37

    If I really wanted to destroy the world and, at the same time, to use java.util.Date, I’d use after() method to be really, really sure to succeed ;-) The equals() method does not give the 100% of “positive” (depending on point of view…) result here.

    • 29/12/2012 at 01:37

      Good that those vile people did not hire skilled devs or else we all would be done ;)
      As for Date itself there are other issues, like Timezones and such, but working with Date could be a nice chapter in a fat book about Java :)


  2. Adam
    28/12/2012 at 23:18

    Object can me mutable or immutable, not class.

    • 29/12/2012 at 01:35

      True enough, but it’s more of wording issue. I refer to all instances of course.
      I also ignore reflection and other tricks that could change the state of the object :)

      But thanks for pointing that out

  3. Cwiat
    14/08/2013 at 06:59

    Someone have already planned endOfTheWorld by inventing JAVA.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: