An Apology For JavaScript

JavaScript is an excellent programming language. As a language, its strength lies in the simplicity and regularity of its syntax. I’m going to show you how much simpler it is than even Python or Ruby. OK, here is how you define a function in JavaScript:

function hello(target) {
    alert("Hello, " + target);
}

And here is how you create an anonymous function:

function (target) {
    alert("Hello, " + target);
}

There really aren’t many surprises here. Basically, omit the name and you have an anonymous function object. The following is very similar to the first excerpt (not exactly, but close enough):

var hello = function (target) {
    alert("Hello, " + target);
}

There are no arbitrary limitations or unwieldy syntax shifts in JavaScript surrounding named and anonymous functions. You can call a function with another one:

foobar( function() { do_something(); return 42; });

Python calls anonymous functions lambdas and gives them strange syntax and arbitrary limitations. Here is a normal Python function:

def hello(target):
    print("Hello, " + target)

An anonymous function looks like this:

lambda target : "Hello, " + target

Oh, wait, it looks wierd and doesn’t actually print anything. It only returns a string value. Yeah, that’s Python’s lambdas. There’s been a lot of talk about it, and Python 3 has dumped them completely. Named functions are still first class objects in Python, so you can use those in most of the ways you would use an anonymous function, so JavaScript’s calling a function with another function gets expanded a little in Python:

def thing:
    do_something()
    return 42
foobar(thing)

It is still elegant, just not as useful, IMO.
Ruby’s named function:

def hello(target)
    puts "Hello, " + target
end

Ruby’s anonymous function:

proc { |target| 
    print "Hello, " + target
}
# --OR--
Proc.new {|target|
    print "Hello, " + target
}
# --OR--
lambda {|arg|
    print arg
}

While the syntax may be… disconcerting, Ruby anonymous functions are just as powerful as JavaScript’s.

In my day job, I write code mostly in PHP. The common PHP installation on a given server these days is PHP 5.2. This language is severely lacking. It does not have first class functions, though it is dynamic. It has classes, but they were strangely modeled after Java, a statically typed language. It is a sad little language deployed just about everywhere. In my daily work I bounce between JavaScript and PHP. Since the advent of V8, Google’s insanely fast JavaScript implementation, I have thought it would be fun to use JavaScript on the server side. You get all the power and flexibility of a dynamic language, running several times faster than Python and Ruby with the ability to share code between server and client-side. My dreams have come true in the form of Node.js.

Now I know that server-side JavaScript has been around for a long time, it just hasn’t seemed appealing for a number of reasons. First of all, I’ve only been doing professional web development for about a year, and I’ve only learned to appreciate JavaScript during that time. Secondly, early server-side JavaScript was in the form of ASP + JScript running under IIS. A crappy JavaScript implementation running on a closed Microsoft web stack is not my idea of appealing.

I’ve dabbled with using the “better” languages on the server side such as Python (Django and Pylons), Ruby on Rails, and Perl (Catalyst and CGI.pm). The distinct advantage is that these languages have large libraries and communities with vast quantities of pre-written code available with liberal, open source licensing. The frameworks are also mature and powerful. However, this does not exist for PHP. There are some libraries and code fragments available but the community surrounding PHP kind of sucks. The reason for this is that good programmers hate the language and don’t want to waste time building things in it for free. Instead, they build things in Ruby, Python, and Perl.

The advantage of using JavaScript on the server is the easier context switching between client and server code. With JSON as the communication layer, there is no impedance mismatch between your server and client. Data can flow freely in both directions. Anonymous data structures in JavaScript are extremely powerful. They exist in Python, Ruby, and Perl, but the syntax is a little different and there are some gotchas here and there. Using JavaScript on the server and JSON as the data transport, you get a homogeneous stack. I hate writing a bunch of code and then watching it fail miserably because I was using the wrong operator to concatenate strings or constructing anonymous list with the wrong delimiters.

Programming fluently is like speaking fluently, it takes a lot of time with a single language to get the point where you don’t have to stop and look things up all the time. The time it takes to look up how to do X in language Y is a real productivity killer. Sure, you are learning, but it’s something that you’ve already learned in a bunch of other languages, it’s just that you haven’t done it enough in this particular language. Or each language does it very slightly differently, so you get them confused over time. In the world of hobbyist programming, all the different language quirks are fun diversions, and you can ponder the benefits of this or that language feature or syntax or library API. But when you need to get things done for a client, they add up to annoyances. If I could just get and stay fluent in JavaScript and work with a complete JavaScript stack for web development, I think I could be very productive.

In time the JavaScript server-side frameworks and libraries will be built up and it will be a good thing.

Comments 1

  1. MV wrote:

    You should look at http://commonjs.org/

    – MV

    Posted 25 Nov 2009 at 5:16 am

Post a Comment

Your email is never published nor shared. Required fields are marked *

Powered by WP Hashcash