fun differences betwixt Java and C++ containers

…and for my next rant I wax poetic about the joys of writing code in the morning using C++ containers and in the afternoon switching to Java!

So I’ve got me an array of, I dunno uint32_t’s, and I’m just happy as a clam with my fun member functions and I’m callin’ ’em and I’m doing all sorts of cleverness, and then it comes time to do something with my amusingly arranged array. I need to call something that takes a pointer to an array of uint32_t’ or better yet I need to manipulate my fancy C++ array very quickly. No problem, this is still C isn’t it, bloated and strange but I can still get a pointer, why yes I can. A quick call to array::data() and I get me a pointer to an array of uint32_t’s…but not just any array of uint32_t’s _the_ array, not a copy (that the cpu had to do a shit load of work to make) the real thing… and I can do what I want with this array, may not be wise but I’m good to go!

…and later that day I’m writing some Java; and I got me a SortedSet… clever… arrange… member functions… But now I gotta call some Android crap and it wants an array because it’s going to display it somewhere. So I go to the documentation and I read, “toArray() Returns an array containing all elements contained in this set.” and I’m having flashbacks to this morning and I think jammin! I’ll just use these cutesy member functions to cleverly arrange my set and then I’ll call this refresh (update whatever) method and I already passed Android a reference to the underlying array and viol-la! …and it doesn’t bloody work. Well, it works the first time but I make my member calls on the set and I call refresh and I get the same display I got last time. So I reread the docs, it says, “toArray() Returns an array containing all elements contained in this set,” so what’s the problem… wait for it… Returns an array containing all elements contained in this set not the array. Effing Java decided to copy the whole bloody thing to a new array and then has the temerity to hand me a reference to this useless (and CPU intensive) pile of crap. Arrrgghh, I hate Java. Yeah, yeah, yeah, copy on write whatever, memset this pal!

Java.util.Timer and java.util.TimerTask abomination

grrr. okay so your writing an Android app and you want some bit of code to run on a semi-regular basis. So, you create a Task, something like…

private TimerTask refreshTask = new TimerTask() {
public void run() {
//do something...

…looks like a familiar design pattern, right. So in my onResume I new a Task and schedule the bloody thing and in my onPause I call Task.cancel().

…and then you run the app and it doesn’t crash right away noooooo it waits until you have to present to your boss and then it pukes all over itself, ehhem why? Well this is just one of the many unintuitive &^%*$^ that come up all the time in Java. You spend the next couple hours borking with different ways to stop and start the timer, then you spend a bit more making sure onPause and onResume are really called when they’re supposed to, and then… You figure out that not only do you have to new a Timer every time you want to restart you also have to new a TimerTask. Why!, I don’t know why, I guess they store some state in the timer task.