In this blog post I ramble on about how writing stuff in JavaScript is fun. Bonus conclusion: Node.js doesn’t suck, either!
But first: Context.
After some discussion, I decided that it might be nice to allow comments on Gwene. However, if people make public comments, there should be a way to display these comments to the public at large. “Here. I’ve made these comments about your crappy blog. See how wrong you were.” So Gwene would have to have a web interface.
Besides, it would mean rewriting bits of it, since it would be Wrong for Gwene to show the entire contents of the RSS feeds. Doing so might mean that Gwene would (possibly) take away some traffic from the original sites. Besides: Legal stuff.
So I decided to write a new interface. Besides, I needed something to do while not being away on holiday for a few days.
I wrote a C program called warp that takes an inn overview file, parses the thread information, and outputs a (binary) index file suitable for parsing and squirting out onto the web. The main point of this program is to enable the web interface to do a O(1) lookup onto the index file on (SSD) disk, read a chunk of data, and output all the thread roots for the group.
I then wrote another C program called woof that takes a list of articles (stored in the binary index file created by warp) and outputs HTML of the article contents. The original RSS entries are shortened drastically (to 100 characters), while the comments originated on Gwene are presented in full.
Then we get to the JavaScript bits.
The fun bits.
Since there are no usability constraints here (blind people just use the NNTP interface), I wrote the web interface in a very JS-ey way. The HTML output by the server is extremely trivial, but is transformed by client-side JavaScript into a pleasant (I hope) DOM.
All fun and games, so far, but the thought of writing the server bits in PHP just made me gnash my teeth. There’s nothing much wrong with PHP, but it’s simply the most boring programming language ever. There’s absolutely nothing fun about it. Just a boring soup of mis-named functions.
So I looked into the most hyped of all hyped yesterday’s technologies: Node.js.
I looked at an example static server, copied the code to the local disk, pointed Node at it, and specified http://localhost:8080/../../../../../../../etc/passwd as the URL, and was promptly served my password file in the browser.
It just works!
After writing some functions to parse the binary index file by warp, I then had a complete working interface to the spool. It took me just a few hours to implement the Node bits. The async callback style for something as simple as this just had four levels or so.
So here’s my point: JavaScript is fun. JavaScript is fun because it’s a simple, logical, straightforward language, that has one single killer feature: How natural and easy it is to create closures. After a while you start introducing closures all over the place, and use mappers and other functional goodies instead of iteration.
People complain about the awfulness of programming with callbacks in Node.js. I think these people are, to use a technical term, retards. I can’t back that up with hard evidence, but I think it’s pretty self-evident.
JavaScript is fun to me in a similar way that C is fun to me. For my day job I program in Common Lisp. For my Emacs hobby I program in Emacs Lisp. Both languages are so expressive that I have to think about what I’m doing. With C, and to a lesser extent JavaScript, I can just type away without having to think about a single thing for minutes on end, because you have to type so much to get anything done.
In addition, I get a feeling of accomplishment when doing something — anything — in these languages. Implement a bank in Common Lisp? Eh. Whatevs. Parse a NOV line in C? Whoho! It doesn’t crash! Yes!
Not that JavaScript is like C in that regard. It’s very un-crashey, and more expressive than most languages people futz around in.
Writing a simple service in Node feels like programming, while doing the same in PHP feels like script drudgery. I’m not quite sure why that is, but there’s just something intrinsically boring about PHP that sucks all joy out of using it.
So why all the hate for JavaScript?
I think that most people who natter on endlessly about how awful JavaScript is have one thing in common: It’s the one other language than the one they’re trained to write in that they come in touch with. (And I use the word “trained” deliberately.)
They’ve been trained to use one single crappy language, like Java or C#, and then they come into contact with The Other, and every way The Other differs from The One True Language I’ve Trained In is Evil.
So, to them, declaring local variables to avoid making them global is just moronic. No matter that that’s how it works in plenty of other languages they aren’t familiar with. Having several equality operators is just iNsAnE, despite all sensible languages having the same concerns (== is kinda like equal while === is very much like eq). And closures are just so incomprehensible. Boo hoo. And on and on and on.
So stop trying to use insane languages that compile to JavaScript. It’s fine the way it is.
Aww. JavaScript. Cute and fun.