Skip to content

Instantly share code, notes, and snippets.

@dbrady
Created March 3, 2011 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbrady/853207 to your computer and use it in GitHub Desktop.
Save dbrady/853207 to your computer and use it in GitHub Desktop.
Example of how the Node.js community prepends commas
// Consider the humble object:
var dave = {
name: "Dave",
age: 39,
color: "green"
};
// If I add to this and forget to add another trailing comma, it's a
// syntax error:
var dave = {
name: "Dave",
age: 39,
color: "green"
pants: "none"
};
// Now, since the dark ages of C to the modern era of Ruby and even
// Javascript, compilers have allowed you to leave an extra trailing
// comma for just this problem (and also for simple code emitter
// scripts: for value in values { write a value, write a comma }, etc:
var dave = {
name: "Dave",
age: 39,
color: "green",
pants: "none",
};
// Try it in Ruby, kids! ray = [1, 2, 3,] # it works!
// But the javascript engine in IE (it's ALWAYS IE, amirite?) chokes
// on that trailing comma. So we're back to accidentally leaving out
// those @#$! commas. The Node.js community has worked around this by
// saying "well, since the comma is apparently the most important
// character on the line, it should LOOK like the most important
// character". (I'm not saying I entirely agree, but I do actually
// understand the sentiment. So here's how the Node guys do it:
var dave = {
name: "Dave"
, age: 39
, color: "green"
, pants: "none"
};
// Now when you add a new item, you'll never forget the comma because
// you always add the comma on the same line as the new element. Also
// because you CAN'T STOP BARFING and that's a pretty good mnemonic
// trigger.
@polgfred
Copy link

polgfred commented Mar 3, 2011

Technically, the comma is supposed to be aligned such that the words line up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment