Tags: Collapsing Geography, Software Development
2008

JavaScript Replace Java with C

So, here’s the thing. If you go into working with JavaScript thinking “wow, it will be like Java, only scripty” you are in for a world of disappointment. I think this is the reason people’s immediate reaction to the comment “I’m learning JavaScript”is to shake their head sadly and respond with “I’m sorry. Broken language.”

This is a mistake. First off, I know broken languages. Hell, I’ve invented one. JavaScript, has its problems, but the largest one is due to the rocket scientists who decided to name it, well, JavaScript. It was, of course, originally known as LiveScript, but for marketing reasons the name was changed.

And hilarity ensued.

Instead, try this mental shift: think of the language as cScript. You know, c, only scripty. I know, you’re thinking this is crazy, but work with me for a moment.

The syntax is very c-like, down to the ?: ternary operator, braces, and naming conventions. Now, unlike c, it isn’t typed, but that’s what makes it scripty. Plus, everything we do to work around c’s typed nature — void *, memcpy, function pointers, etc — just causes memory leaks, architecture dependence, and pain, so isn’t it nice to have a language that does that for you? Even the method of extending JavaScript objects — by creating functions and adding parameters in — looks a lot like what happens when you apply OOP principles to c.

Now, it is dog slow — hello, why the hell isn’t it JIT-ed? — and shows quirks from having evolved amidst the browser wars. But with the canvas tag and other sexiness looming on the horizon — not to mention the uber-fast ActionScript engine in Flash — I’m having a blast. This weekend I get to start working on my first test app, which will be fun.

For JavaScript, I worked through O’Reilly’s “Learning JavaScript.”The reviews are correct — there are a lot of typos — but thanks to Firebug I turned most of those into learning experiences. I also am most of the way through”Learning PHP and MySQL.”Same problem — lots of typos — but I thought it did a pretty good job of filling in gaps. However, in comparing how it used PHP to the”Head Rush Ajax,” I thought the Head Rush book’s approach of writing simple HTML, using AJAX to fill in the dynamic data, and applying PHP as the server language to generate JSON and XML data fits my brain better than the Learning PHP book, which is all about using PHP to generate HTML directly. That way seemed really awkward, required Smarty templates, and just looked like a lot of work.

Current plan of record, for those following at home, is to do first pass of test app in PHP, then recreate it in Rails. I figure the compare and contrast should be enjoyable.