Monday, August 10, 2009

Objective-C, Javascript

There are relatively few programmerly things that benefit from being done in a GUI. Most everything is better done as text or on a command line. But UI creation is a big exception to that. UIs are definitely best done in a builder. My experience with Cocoa's Interface Builder over the past month has been awesome. It's so awesome, I don't know how I could even ever go back to doing things by hand. That's how awesome it is.
What's more, it's turned me on to other GUI builders, like the one for WPF in Visual Studio; and it's totally obliterated any lingering desire I've had to write Swing or *name-that-framework* code anymore. Clearly, the way of the future is XML markup. And that's the key difference between something like Winforms and WPF or Cocoa; there's no generated code, just generated XML, and that XML is simply read in at runtime to set up your widgets and your event bindings. That's the way to do it -- it sortof has a web feel to it even.
Anyway, I can't ever go back to writing Swing GUIs by hand. It's too monstrous. And all the Swing GUI Builders just generate crappy Java code for you. I don't want code generators, I want markup generators. I want something like Glade for GTK, except for Swing. Though I'm not sure I'll ever get it since Sun has abandoned Swing and nobody likes Java Desktop Apps anyway.
But Objective-C is actually a pretty awesome language, and combined with Cocoa and all the Apple tools, OSX is a pretty awesome platform for rich application development. Everything is easy to use, the Objective-C runtime, though lacking garbage collection, is dynamic and has an excellent reference counting facility -- the Cocoa API is consistent and comprehensive... it's a pretty awesome package overall, and I can see why developers like developing applications for the Mac.
It does bother me though that I'm dependent on their tooling to be productive on Cocoa. You can't really write Cocoa interfaces without the Interface Builder, and all that stuff only runs on Mac and only is available for Mac. I might have missed it, but as far as I know there is no interface builder for GNUStep -- I don't even know if GNUStep has a UI library like Cocoa; maybe some sort of wrapper for GTK.
Anyway, I had an awesome time developing for the iPhone and for the OSX desktop environment, and I think Objective-C is a cool language that really intrinsically fits the MVC model and suites the task of creating desktop applications really well.

----


Javascript is really Scheme. It's Scheme with a half-assed, ugly varnish of C. But it's still Scheme to the core, and a lot of its querkyness can be explained in terms of it being Scheme; once you understand that it is Scheme, you understand why it behaves in certain ways. Like why variables declared in an if statement or a for-loop are available throughout the enclosing function scope. That's because, like in Scheme, environment is a factor of functions. Every time you create a new function, you instantiate a new environment to place your variables in. For loops and If-statements don't create new functions, so the environment your in, and hence all the variables you declare, are declared in the enclosing function scope, just like they would be with scheme "(define x 1)", but instead you have "var x = 1;".
Javascript could have been a lot more awesome if it had just been a little more straightforward. If they could have skipped the C-ishness, and just done straight Scheme with some Self, programmers coming from more typical languages wouldn't be so shocked by Javascript's schemeish and selfish behavior. If Javascript were more plane about what it was about and how it worked, there would be less confusion. The guy who wrote "Javascript: The Good Parts" said that he had an epiphany where he realized Javascript was just Scheme... he'd have had that epiphany sooner if Javascript wasn't perpetually travelling in Cstyle disguise.
But Javascript is pretty awesome, cause despite all its grossness, the first class functions and dynamic typing and prototype inheritance and object literals and array literals are all so awesome and so powerful that they make up for all the rest. And provided you avoid the nasty parts, you can program in Javascript just like you would Scheme, and you get something awesome and powerful like JQuery as a result. I think John Resig really understands or understood what Javascript was about when he created JQuery; he didn't try and force it to be like other languages and build some guady inheritance heirarchy that's classically fake -- he used lambdas and functional programming techniques to create a really awesome and expressive framework that brings the fun back into DOM manipulation and Ajax and such. I really love JQuery, and I'm starting to really love Javascript as I begin to understand more of how to write good Javascript. Indeed, the most important realization for any Web 2.0 developer to make is that you CAN write good Javascript. It is not impossible. Javascript isn't this terrible bomb waiting to go off. You can get it right, and it can empower your web pages.

Something interesting: Cappuccino is a web framework -- essentially a port of Cocoa to Javascript. What's really awesome about it (one of the things) is that you can use the Interface Builder to create your web guis. Because it's all xib files, they were able to write a converter that takes the xib xml and turns it into an equivalent for their framework. Pretty awesome.