Wednesday, October 03, 2012

JS++

Sometimes you dont think of the obviousness of an idea until someone hits you on the head with it.

Typescript was released today and it finally dawned on me (despite the very visible existence of Coffeescript in that exact same mold for ages) that creating a pre-processor for Javascript that cleaned up all the messy bits for you was a viable option.

After all, that's how C++ came to be, isnt it?

But if I were to do it, I'd do it a bit differently:

  • I'd not have the TS-style classes, interfaces and the like; instead I'd just have a simple object creation syntax that's better than the default JS one or the JSON-style one
  • Modules would map to CommonJS or AMD modules, but they would behave (from a deployment perspective) more like Fantom pods than the module-within-module craziness that TS seems to expound. It sucks in Java and I'm convinced the Fantom model is better simply because "One conceptual container == one deployment container" is such a reduction in cognitive overload.
  • Embrace hoisting for "top-down style" writing of code, but introduce true scopes to retain sanity.
  • Some implementation-neutral way of embedding the ultimate javascript into its host environment through the preprocessor. This is something tools like yeoman and bower miss, IMO.

Those are some of my main themes, I'd say; there's more that another reading of the Crockford bible should produce.

Design notes: 
  1. Since this should be a simple "if you see this jspp code, spit out that js code" type thing, I'm thinking something as simple as StringTemplate might suffice, although it might be too early to speculate that far. If it did though, I'd build the whole preprocessor as a series of calls to a function called becomes(srcstr,deststr)
  2. The implementation will have to support output of multiple files from the processing of a single input file.
  3. If this turns out to be small enough, we could finally have a JS language that runs transparently on Rhino. CS currently faces the issue that its main code is larger than the size that Rhino can handle.
Postscript: I'm tentatively calling this idea JS++ simply because that's how C++ came to be; but I can see how this might not make good marketing sense. If so, here are some alternative names:
  • Better Javascript : .bs :)
  • Easier JS: .ejs