Wednesday, April 26, 2006

haXe

The prevalent way to develop web apps these days is to use a server side language (PHP, Perl, Python, Java, Ruby, Erlang, C++, etc.) for backend logic, Javascript for browser (AJAX) logic and Flash/Actionscript to create swf's if you need vector graphics, animation and interactivity beyond that which AJAX can provide. This makes web development quite burdensome and complex. haXe was born out of a seemingly simple idea: it doesn't have to be this way!

If you were willing to put your aesthetic sensibilities aside, you could use Java to develop both both server side logic and client side applets. However, the JVM hasn't caught on as a platform for client-side development because the JVM isn't universally available (and it's a big download), and applets tend to be ugly and slow to start. (I'll reserve more articulate Java bashing for future posts :) ). Ruby on Rails has made progress in the one-language-for-all-needs direction with a cool new feature called RJS, which allows you to write Ruby code that Rails compiles into Javascript. However, there's no way to easily integrate SWF programming with ROR as far as I know. In addition, although I really like ROR, Ruby's speed is not exactly blazing and its lack of compile-time type checking has its disadvantages.

Enter haXe.

haXe is language with an open source compiler and a set of libraries that you can use to create all aspects of a web app: Javascript code, compiled SWFs and server side code. haXe has some great benefits: it lets you reuse code between all platforms; it eliminates the burden of shifting mental gears every time you work on a different aspect of your application; it makes it easy to write portable code that runs on all browsers and/or different Flash player versions; it has a very fast compiler; and, like most good things, it's open source.

haXe is also a very well designed language. Its syntax is similar to ActionScript 2.0's syntax, but haXe's type system -- a mix of static, dynamic and inferred typing (similar to that ML-derived languages) -- is far better than ActionScript's simple dynamic typing. From my experience using haXe, its type system is extremely helpful for catching errors early and developing maintainable code without sacrificing the flexibility of dynamic typing when you really need it. haXe's support for object oriented programming, closures, iterators, type parameters (similar to Java generics), enums (powerful variants types) and fully typed anonynous objects should convince you that haXe isn't a toy language by any means.

As you might have guessed, all is not perfect. haXe's server side features are not very mature -- but Nicolas Cannasse, haXe's creator, is developing them rapidly (and the Necko Virtual Machine, on which the server side code runs, looks pretty solid: Necko is supposedly 30x faster than PHP). The Flash 8 support is superb, but it's not clear when Nicolas will implement support for Flash 9. (Right now Flash 9 is in beta. I think Nicolas is planning to tackle Flash 9 support when Adobe makes the final releases.) In addition, the haXe community, although very enthusiastic, is much smaller than the various LAMP communities, so don't expect too many 3rd party libraries and tools (but you don't need such luxuries anyway: write your own!).

I've been mostly doing Flash development with haXe and I'm pretty impressed with its capabilities. I haven't used the Javascript compiler much, but it looks quite neat and I will probably use it more I have a greater need for AJAX. I also haven't touched server side features, which are the least developed aspect of haXe. I think that in a few months, when haXe is more mature, it will be an excellent language for all aspects of web application development.

Keep haXe on your radar. In the meantime, unless you're doing Flash development, go back to Ruby on Rails -- but don't forget that "haXe on Rails" ("haXOR," aptly suggested by a mailing list member) is around the corner.