Static Typing

Sunday, 26 July 2009 8:02pm

I'm not big on comparing the merits of different languages. Every language I've used serves a good purpose for some task. So I generally pick the best language for the task at hand.

In general, though, I always tend to prefer static languages for anything but a small or quick and dirty project. Especially when its a code base I intend to maintain for some time.

I just spent the weekend refactoring Fan's JavaScript compiler to change how I model Fan fields in JavaScript. The way I had previously done it looked like this:

// Fan
class Foo { Int x := 5 }

// JavaScript
Foo.prototype.x$get = function() { return this.x; }
Foo.prototype.x$set = function(val) { this.x = val; }
Foo.prototype.x = 5;

Which isn't great, but didn't really have alot of options. But it seemed nice enough. It grouped the accessors and storage neatly together. And in most cases, since I can access the storage directly, the code overall was fairly clean.

Unfortunately that model made it very difficult to make certain use cases of fields and methods work (like overriding a const field with a method). So I changed the JavaScript to look like this:

// Fan
class Foo { Int x := 5 }

// JavaScript
Foo.prototype.x = function() { return this.m_x; }
Foo.prototype.x$ = function(val) { this.m_x = val; }
Foo.prototype.m_x = 5;

That change would have been a bit painful in any language, since there was just alot of code to update (the sys library and all the native implementations are written directly in JavaScript).

However, since JavaScript does not have static type checking, I pretty much had to tackle errors one-by-one using the test suite and Fresco (the web UI I'm developing at SkyFoundry).

This is probably the biggest reason I prefer static languages. When you have to make sweeping changes on a large code base, you simply can't verify your changes 100% in any language. But a compiler with static type checking can help you tremendously. Especially for code that is difficult to unit test, like UIs.