Java Auto-Casting and AutoCastMap

MyType myType = someObj.getMyTypeObject();
if (myType instanceof MyDerivedType) {
    MyDerivedType myDerivedType = (MyDerivedType) myType;
}

Did you ever need to write something like this? I think it might be common approach (when using some loose typing containers) to deal with objects that returns base type instead of derived type that we need. It is also obfuscated with defensive programming structure (by using instanceof type check). I don’t like defensive programming, where it is not needed so in my case it would turn into:

MyDerivedType myDerivedType = (MyDerivedType) someObj.getMyTypeObject();

You must admit that both these constructs looks awful (and sometimes requires to suppress compilers warnings). Most of the times this can be solved by redesigning corresponding objects, that casting is not needed, but sometimes you have to end up with one of these. Continue reading

SortedList for JavaScript

Recently I needed some heap implementation in JavaScript, that would be used in path-finding algorithm for web browser game I’m developing.

C-like implementation

In first attempt I used my friend’s BinaryHeap implementation, which was not using any language features – it was rather raw copy from it’s C version. Main goal was to avoid memory allocation. Performance (as expected) was not stunning, every attempt to calculate path (even short and straight ones) was causing about 8 fps drop and significant hang-up.

Maybe linear search will do?

Second try was driven by a thought: “Hey, in this queue there should be no more than about 15 elements – linear search should do the job!”. Implementation was pretty easy and I came up with something like this: Continue reading