Posts Tagged ‘example’

Oups

Monday, March 15th, 2010

While cleaning up in the core classes of Trekwar yesterday (documenting functions, formatting/structuring code, minor improvements) I also saw lots of equals(Object o) methods. And in my infinite wisdom I decided to use generics since it is used pretty extensively throughout the code anyway, so I changed basically all the equals methods.

example:

public boolean equals(Object o) {
        if(o instanceof Structure)
            return equals((Structure)o);
        return false;
    }

was changed to:

public boolean equals(Structure s2) {
        return name.equals(s2.getName());
    }

Now this is not a problem when writing code, as you pretty much always compare objects of the same type. However, the code also uses the Java Collections contains() method a few places, and it always uses the equals(Object) version. So when my classes no longer provided this method, the default one inherited from java.lang.Object was used instead. This caused a strange array of bugs to appear (star systems on the map not shown with faction color/icon except for the Cardassians for some strange reason, users not having any technologies, unable to build any structures, generally erratic behavior, etc..). Luckily I discovered the cause pretty quickly and only wasted about an hour or so on this :)

I guess the lesson is that objects that needs to be compared, directly or indirectly by contains() or other methods, should always define an equals(Object) method that just passes the call along to the equals method written with generics.

public boolean equals(Object o) {
        if(o instanceof Structure) {
            return equals((Structure)o);
        }
        return false;
    }

And perhaps that you should not mess around with your code for hours on end without stopping to test if you broke it once in a while :)

7 Days of hell

Friday, November 2nd, 2007

Damn, this has been an exhausting week.. It all started at work last Friday when we got a list of lots of crappy functionality we had to implement in the game by Monday. So my weekend was pretty much spent on adding products and setting up tenders in PRIME:

Tuesday evening was lost while taking the final part of the boating course.

Wednesday I had to walk over an hour outside in the pouring rain, and take the VHF/SRC exam.

Thursday I had to walk  an hour outside in much worse weather, and take the boatman’s exam.

Now it’s Friday and I’m taking the day off to recover.

At least both exams went great and I should get my VHF Short Range Certificate, and my certificate of boatmanship in a couple of weeks.