Log in

No account? Create an account

JavaScript is bullshit - Kef's Mirror of Darkness

About JavaScript is bullshit

Previous Entry JavaScript is bullshit Jan. 20th, 2012 @ 06:35 pm Next Entry
I started making a JavaScript clone of the NES game Spot (a.k.a. Ataxx, Boogers, and several other things). I managed to throw together a version that used a 3-ply minimax search (albeit an incorrect one; I fixed it the next day I think) within 24 hours. It plays reasonably well and can often beat me, though it occasionally makes moves that seem strange, so it may or may not be buggy. I'm trying to improve it with alpha-beta pruning, but that's taking a while and I'm giving up because I'm tired of dealing with JavaScript's BS. All the good things about JavaScript that made it easy to throw this thing together have been undone by this point. I'll revisit this project when I rewrite it in something more sensible, if ever.

Here's the thing. In a game like Spot (or checkers, chess, etc.), the AI often needs time to think. The naïve way to code this — and the perfectly sensible way of coding it in 99% of programming environments — is to just call a function called "findBestMove" or "doAI" or whatever. Well, JavaScript doesn't like that. If you call a function that takes too long to execute, the page will freeze and, if it takes too long, the browser pops up warnings about the script possibly being frozen. (IE pops up a warning after only a second or two!) You're supposed to work around this by using setTimeout to do a little bit of processing at a time, but this sort of workaround was definitely not written with recursive tree traversal algorithms in mind!

So one way you can work around it is using a thing called a "web worker", which is JavaScriptese for "heaping pile of shit". The message-passing system it uses feels like it came straight out of WinAPI. I understand the idea is to make it easy to code multithreaded stuff correctly, but I think this isn't the right way to do it. At the very least, they could have had the messages invoke on____ handlers instead of only a catch-all onmessage handler that requires you to use a switch statement to distinguish between different kinds of messages.

Now let's suppose your web worker code (i.e. game AI) has a bug (which mine does). Well, you can't use a function like console.log to try to find it because workers don't have access to the console. So you have to write a workaround using message passing. Joy. So let's say you do that and it isn't good enough to find the bug. You need to step through it in a debugger like Firebug. Guess what? Firebug can't debug code inside a worker! Chrome's debugger supposedly can, but y'know what? I'm tired of messing around with this shit.

By the way, did I mention IE9 and below don't even have web workers? So kiss IE goodbye if you want to use them.

And all this is without even going into the stupidity of, say, having variables global by default (which bit me several times until I learned about "use strict" — the use of which can introduce compatibility problems of its own). Honestly, I don't understand how people write complete apps in this thing. A language where you have to jump through a million goddamn hoops just to write something similar to a chess program? Pretty goddamn pathetic.

Leave a comment
Top of Page Powered by LiveJournal.com