The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
SlightlyLoony
Tera Contributor

Not long ago I read an apparently sincere and serious article on the web, wherein the author urged developers to write code with fewer characters in it, as that would use less electricity and therefore help the environment. He continued with a calculation of the energy that could be saved by such a practice. This analysis left me despairing for the future of the human race, as it consisted entirely of grotesquely mistaken assumptions and silly arithmetic errors. In the end, this poor author had convinced himself that shorter code comments could reduce the carbon footprint of the human race by over 50%. Wow!

There's a little quirk of JavaScript that many people don't know about: semicolons are (mostly) optional. When I first came across this factoid, I was reminded of the "Green Code" article — that author would have been delighted to find this! But one might be interested for other reasons as well. Here's an example of the optional semicolons being left off: this code (which looks quite odd to anyone who has written much Java or JavaScript) is perfectly legitimate:


var x = 1
var y = 15
var z = x + y
gs.log('The answer is ' + z + '.')

That same code, written on one line, will fail:

var x = 1 var y = 15 var z = x + y gs.log('The answer is ' + z + '.')

The JavaScript parser can't make sense of that mess. But if you put the semicolons back in, it works just fine:

var x = 1; var y = 15; var z = x + y; gs.log('The answer is ' + z + '.');

1 Comment