Monday, November 06, 2006

PyJamas: a Python GWT clone

Google Web Toolkit is an interesting idea: let's write all our web site logic in one programming language instead of 2. You write all your code in Java and the framework's black voodoo custom compiler generates client side JavaScript for you, which in theory you'll never have to see and more importantly never have to debug. An analysis of GWT deserves its own post but right now I'd actually like to talk its Python clone: PyJamas.

You can check out the web demo here: PyJamas KitchenSink.

I admit that I haven't yet had the time to play much with PyJamas, but I have a few theoretical reservations.

I don't think its hard to see why GWT has potential. JavaScript does have a somewhat deserved bad reputation. I learned to hate it back in 1998-2000 when I was a full time web developer working on sites that had to support all major browsers and lost a significant amount of hair while struggling with browser incompatibilities. Yes, times have changed and JavaScript/EcmaScript has come a long way, but it is still a pain in the neck when it comes to development and especially debugging. And as we all know, developers spend most of their time debugging.

Here's where Java comes in. It's strongly and statically typed, so the compiler can check all kinds of stupid mistakes. I notice how annoying it is when I switch to a dynamic language from Java that I don't find out about these until I actually run the code:
  • wrong type or number of arguments when calling a method
  • calling a non-existing method
  • referring to a non-existing class
  • missing imports
  • etc.
All these can be due to typos, copy & paste errors or simple lack of caffeine. :) However when I'm developing in Eclipse the IDE -- actually it's embedded compiler -- catches them instantaneously. I don't have to fire up the server process and invoke it in a browser window. It all happens when I'm still typing the code. One has to admit that this saves a lot of time.

Since dynamic languages don't have the luxury of the compiler catching these simple problems, there is a much stronger motivation to write an extensive unit test harness, so that these basic errors don't eat up too much of the developer's interactive debugging time. Having a test harness is a good thing, but having to write tests for trivial stuff that the compiler could check is, well unproductive.

Now its a matter of debate exactly how much time is saved or wasted by declaring your types and relying on the compiler to do these checks or not declaring them and creating a test harness and relying on shorter turnaround times instead.

Going back to PyJamas, I believe the problem is that Python and JavaScript are not different enough to have a significant productivity gain when you're writing and debugging Python code vs. JavaScript. All the silly mistakes will have to be caught at runtime.

I'm still looking forward to trying it out though since there is a good chance that I'm completely missing the point. :)

3 comments:

Nagy "Winter" Lajos said...

"Now its a matter of debate exactly how much time is saved or wasted by declaring your types and relying on the compiler to do these checks or not declaring them and creating a test harness and relying on shorter turnaround times instead."

Or not declaring your types
(just a tiny little bit) and relying on
the compiler to figure everything out at
compile time. Called type inference and been around for decades. Eventually,
things will converge towards type inference (I think even in C++ they
are planning on using the `auto' keyword
for type inference) it will take a while ;) Remember Ada and generics. It took Java a mere 20 years to arrive where Ada was at 1985. Another 20 years and we can expect to see limited type inference in mainstream languages, at that time, a technique already 50 years old.

Szocske said...

Type inference in Java? Maybe for local variables. The specificity of the types used in method signatures is a conscious design decision. Encapsulation, information hiding, interface stability.

James Tauber said...

Even with my bias as the author of pyjamas, I agree that Java to Javascript provides certain benefits that Python to Javascript does not.

However, if you are already a Python programmer, pyjamas provides a way of working in a familiar environment, reusing your existing code, developing both client-side and server-side code in the same language, etc.