Saturday, September 8, 2012

JavaScript and the Semicolon

Sometimes, we realize that we have taken a course for decades, and have not really thought about why. I found myself in that curious position when I read this entry JavaScript Semicolon Insertion, whose author knows 50 times more about the state of the ECMAScript specification then I do.

So I have written allot of javascript over the years. I have been writing JavaScript professionally since 1997 and the browser wars, so that is allot of semicolons. I never really thought to wonder about it, just inserted them in the appropriate place at some line terminations.

Good.
function add(event, callback) {
  if (fn_p(event)) {
    callback = event
    event    = this.default_event }
  insert_callback.call(this)
  return this

  function insert_callback() {
    if (this.value)  fire(this, event, callback)
    else             add_callback(this, event, callback) }
    }
}

Bad
function add(event, callback) {
  if (fn_p(event)) {
    callback = event;
    event    = this.default_event;
  }
  insert_callback.call(this);
  return this;

  function insert_callback() {
    if (this.value) {
      fire(this, event, callback);
    }
    else {
      add_callback(this, event, callback);
    }
  }
}

But, this is all well and good from a human readability standpoint, but I have serious doubts about the adverse reactions from putting good human readable code through javascript uglification and minification.

No comments: