The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
JavaScript and Semicolons

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page


Posts: 201
Nickname: cfis
Registered: Mar, 2006

Charlie Savage
JavaScript and Semicolons Posted: Mar 23, 2006 11:05 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by .
Original Post: JavaScript and Semicolons
Feed Title: cfis
Feed URL: http://cfis.savagexi.com/articles.rss
Feed Description: Charlie's Blog
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by
Latest Posts From cfis

Advertisement

Did you realize that semicolons are optional in JavaScript? I have to admit ignorance until just a few weeks ago - and I've been programming JavaScript on and off for seven years. Naturally, when I learned this I went and stripped all the optional semicolons from my code (well, it seemed like a good way to kill a few minutes at the time). It was a bit jarring to see a curly brace language without semicolons. See for yourself1:

function(testName) start
{
  if (!this.log) return;
  this.testName = testName;
  this.lastLogLine = document.createElement('tr');
  this.statusCell = document.createElement('td');
  this.nameCell = document.createElement('td');
  this.nameCell.appendChild(document.createTextNode(testName));
  this.messageCell = document.createElement('td');
  this.lastLogLine.appendChild(this.statusCell);
  this.lastLogLine.appendChild(this.nameCell);
  this.lastLogLine.appendChild(this.messageCell);
  this.loglines.appendChild(this.lastLogLine);
}

Versus:

function(testName) start
{
  if (!this.log) return
  this.testName = testName
  this.lastLogLine = document.createElement('tr')
  this.statusCell = document.createElement('td')
  this.nameCell = document.createElement('td')
  this.nameCell.appendChild(document.createTextNode(testName))
  this.messageCell = document.createElement('td')
  this.lastLogLine.appendChild(this.statusCell)
  this.lastLogLine.appendChild(this.nameCell)
  this.lastLogLine.appendChild(this.messageCell)
  this.loglines.appendChild(this.lastLogLine)
}

It also took a few days to get out of the semicolon habit - although I still mistakenly put one in here and there (or more commonly name something like_this instead of likeThis).

So is it worth it? I think so. I fall squarely in the camp that believes syntax matters - the less of it the better. In fact, I think sparse syntax plays a significant part in Python's and Ruby's popularity. Granted this change isn't much - but every little bit counts. Less syntax means less code which means faster comprehension.

1Do you know which library this code comes from? Its from unittest.js in script.aculo.us.

Read: JavaScript and Semicolons

Topic: Added generics Previous Topic   Next Topic Topic: James McGovern on Ruby… again

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use