News for hackers. Stuff that matters.
Re:Actual drawbacks to omitting semi-colons? by tlrobinson on Wednesday October 06, 2010 @02:40AM (#1762982)
I think this style ("npm style") is trying too hard to be clever without much benefit. Classic JavaScript style (see: Crockford) has done just fine and is familiar to everyone who knows C-style syntax. No reason to change it now.

I'd rather consistently have semicolons at the ends of lines than crap like this:

    ;(x || y).doSomething()
    ;[a, b, c].forEach(doSomething)
It's easier to always include the semicolons at the end of statements rather than try to figure out the edge cases where missing a semicolon would change the behavior.

I'm also not a fan of the "comma-first" style, especially for var declarations...

    var foo
      , bar
      , baz
      ;
I prefer:

    var foo;
    var bar;
    var baz;
It's fewer lines, consistent, symmetrical, and it's impossible to introduce errors through copy/paste or reordering.
Re:Actual drawbacks to omitting semi-colons? by jasondavies on Wednesday October 06, 2010 @04:52AM (#1763216)
I've grown to prefer:

    var foo,
        bar,
        baz;
It seems less cluttered. You can of course introduce errors through copy/paste/reordering, but I think it's a reasonable tradeoff. I agree about comma/semicolon-first being strange though.
Re:Actual drawbacks to omitting semi-colons? by railsjedi on Wednesday October 06, 2010 @12:13AM (#1762677)
I hate semicolons too, but I wouldn't recommend removing them in your javascript. Douglas Crockford says to use semicolons, so use semicolons. JS lint is a useful tool and its worth having your code conform to it.

If you really hate the syntax quirks of javascript like me, a better solution is to just preprocess it. CoffeeScript has made me really enjoy writing frontend ui code again. http://jashkenas.github.com/coffee-script/

Re:Actual drawbacks to omitting semi-colons? by Marticus on Wednesday October 06, 2010 @12:55AM (#1762783)
True fact - CoffeeScript did the same for me even though it was heavily frowned upon by others.

Typically I omit them if I can. And I have no reason to do so other than I think it makes the code look cleaner (I am often quite zealous on proper indentation / return lines).

I figure if using them works for you, rock on. If you hate them and don't use them, rock on. I really don't get bothered by debugging others' code either way as long as it is "readable-formatted" and works.

I've found that usually you fall into a situation where you have a company standard and have no choice in the matter though, and it doesn't even make a crap what your personal preference is.

Edit: To be fair though I only got "Eagle" on the test, but I chalk that up to only giving the test about 5 minutes of my time, too.

Re:Actual drawbacks to omitting semi-colons? by jashkenas on Wednesday October 06, 2010 @12:23AM (#1762700)
One note about this is that CoffeeScript generates JS source that passes JavaScript Lint. You can use `coffee --lint` to verify this, and make sure you get "0 error(s), 0 warning(s)"...
Re:Actual drawbacks to omitting semi-colons? by aaronbrethorst on Wednesday October 06, 2010 @12:28AM (#1762710)
Speaking of, thanks for the introduction to CoffeeScript on Sunday. I think you made a convert out of me. I'm actually kind of enjoying the idea of writing a front end in JS without Sproutcore or Cappuccino solely because of CoffeeScript.
Re:Actual drawbacks to omitting semi-colons? by eli on Wednesday October 06, 2010 @12:00AM (#1762646)
Well, there are a few narrow examples where omitting semicolons will lead to incorrect code. If you're a superhuman javascript whiz who never makes mistakes, then sure, leave out the semicolons. But for the rest of us mortals, it would be nice if you left them in.
Re:Actual drawbacks to omitting semi-colons? by probablycorey on Wednesday October 06, 2010 @12:03AM (#1762652)
Typical pant-wearer.
Re:Actual drawbacks to omitting semi-colons? by zmmmmm on Wednesday October 06, 2010 @12:31AM (#1762717)
The one place where this really bites me is return statements. For example:

    function x() {
       return 
    	   2+3;
    }
    alert(x());
Every now and then I'll reformat a line by breaking it in two and it takes me 10 minutes to figure out why my code is not working.
Re:Actual drawbacks to omitting semi-colons? by VMG on Wednesday October 06, 2010 @01:01AM (#1762796)
damn it I forgot that - does anyone know how to make vim highlight this?
Re:Actual drawbacks to omitting semi-colons? by grayrest on Wednesday October 06, 2010 @01:32AM (#1762848)
In your after/syntax/javascript.vim:

    syntax match javascriptReturnError /return\s*\n\r\?\s*\w.*$/

    ...

    highlight link javascriptReturnError Error
Obviously you can play around with the regex to tweak what matches.
Re:Actual drawbacks to omitting semi-colons? by nostrademons on Tuesday October 05, 2010 @11:23PM (#1762562)
"Coding styles are like assholes. Everyone has one, and nobody likes to look at anyone else's." -- credited to Eric Warmenhoven
Re:Actual drawbacks to omitting semi-colons? by roryokane on Wednesday October 06, 2010 @12:27AM (#1762707)
The analogy falls apart where people like to look at their own coding styles, but nobody likes to look at their own asshole. So the rule is more like “nobody likes to look at assholes at all”, which is a pretty different situation from coding styles. That’s a relevant enough distinction that I would call Warmenhoven’s analogy a bad one, good for provoking a laugh but not for discussing coding styles.
Re:Actual drawbacks to omitting semi-colons? by nostrademons on Wednesday October 06, 2010 @01:03AM (#1762807)
Actually, I looked up Warmenhoven's quote on the net after posting, and the original was "nobody likes anyone else's". That eliminates that little semantic problem.
Re:Actual drawbacks to omitting semi-colons? by roryokane on Wednesday October 06, 2010 @12:21AM (#1762694)
If you prefer not to use semi-colons in JavaScript but need to add them for other’s sake, you could try programming in CoffeeScript (http://jashkenas.github.com/coffee-script/), which is a language meant to be a bunch of small improvements to JavaScript that compiles into JavaScript. It doesn’t require semi-colons. You can show others just the compiled JavaScript, and they need never know you are using something else.

Edit: ninja’d by railsjedi: http://news.ycombinator.com/item?id=1762677

Re:Actual drawbacks to omitting semi-colons? by pseudonym on Wednesday October 06, 2010 @01:16AM (#1762829)
I realize semicolons aren't required for javascript, but I put them in anyways merely because I do so much coding in other languages where semicolons are required that it's silly to try and modify my habits just because it's not a requirement to put in.

Just because you don't wear pants at home doesn't mean you're not going to feel silly showing up at work without them.

Re:Actual drawbacks to omitting semi-colons? by shazow on Wednesday October 06, 2010 @12:12AM (#1762675)
There are numerous cases where lack of semicolons create ambiguous code which introduces bugs.

Here are some: http://gist.github.com/612814

Edit: Er, tried to paste code in-line but formatting got messed up.

Re:Actual drawbacks to omitting semi-colons? by benjoffe on Wednesday October 06, 2010 @12:19AM (#1762691)
Well, the code isn't actually ambiguous (since the spec states well defined behaviour), just very easy to misinterpret.
Re:Actual drawbacks to omitting semi-colons? by jfarmer on Wednesday October 06, 2010 @01:46AM (#1762877)
Ambiguous to a human, vs. a compiler.
Re:Actual drawbacks to omitting semi-colons? by rtlechow on Wednesday October 06, 2010 @09:13AM (#1763749)
Ambiguous to a human who doesn't understand the rules of automatic semicolon insertion. Maybe such a human shouldn't be using javascript in the first place, instead of working in fear.
Re:Actual drawbacks to omitting semi-colons? by jfarmer on Wednesday October 06, 2010 @11:35AM (#1764357)
Human doesn't understand complex rule with counterintuitive corner case. Film at 11.
Re:Actual drawbacks to omitting semi-colons? by aes on Wednesday October 06, 2010 @02:59AM (#1763018)
On the "if we were to design a programming language" level, it seems that a feature like ASI (Automatic Semicolon Insertion) is not only useless but even harmful. Every programmer is required to learn the rules of the mechanism in order to understand others' code. Furthermore, it needlessly increases the amount of possible programming styles, which, in my opinion, is a bad thing.

C programmers have always used semicolons and never wasted a second thinking whether they should.

Re:Actual drawbacks to omitting semi-colons? by xiongchiamiov on Wednesday October 06, 2010 @03:06AM (#1763039)
> C programmers have always used semicolons and never wasted a second thinking whether they should.

And, as one who primarily writes in Python and Ruby, I always find myself omitting them, and spending more than a second wondering why they're necessary.

Re:Actual drawbacks to omitting semi-colons? by nostromo on Tuesday October 05, 2010 @11:53PM (#1762630)
I always use semicolons in js because I usually compress the code down by removing superflous whitespace -- which would make this style of code not function at all.
Re:Actual drawbacks to omitting semi-colons? by mncaudill on Tuesday October 05, 2010 @11:55PM (#1762636)
The major compressors are syntax aware. When they compress your JS, they'll put in semicolons where needed.
Re:Actual drawbacks to omitting semi-colons? by suninwinter on Wednesday October 06, 2010 @01:02AM (#1762802)
A newline takes up just as many bytes as a semicolon.
Re:Actual drawbacks to omitting semi-colons? by masklinn on Wednesday October 06, 2010 @01:41AM (#1762864)
But if you have a newline you generally have an indent. Furthermore, there are places in JS where semicolons are absent and newlines are not (after braces).
Re:Actual drawbacks to omitting semi-colons? by pjscott on Wednesday October 06, 2010 @03:44AM (#1763103)
It's moot, anyway. Format your code for readability, and let a minifier handle making it smaller. Then let gzip finish the job.
Re:Actual drawbacks to omitting semi-colons? by pjscott on Wednesday October 06, 2010 @01:39AM (#1762860)
Unless you're using CR+LF as your newline delimiter.
Re:Actual drawbacks to omitting semi-colons? by po on Wednesday October 06, 2010 @12:11AM (#1762673)
I leave semicolons out in my code. But it's python so I think that's ok.
Re:Actual drawbacks to omitting semi-colons? by coderdude on Wednesday October 06, 2010 @02:19AM (#1762943)
I sometimes use semicolons in Python for convenience when writing short debug code. Fewer lines, fewer pound signs I have type to comment it out later.

e.g., print somevar, othervar; exit()

Re:Actual drawbacks to omitting semi-colons? by cracki on Wednesday October 06, 2010 @08:43AM (#1763649)
import pprint; pp = pprint.pprint
Re:Actual drawbacks to omitting semi-colons? by RyanMcGreal on Wednesday October 06, 2010 @06:31AM (#1763366)

    SyntaxError: not a chance!
Re:Actual drawbacks to omitting semi-colons? by mkramlich on Wednesday October 06, 2010 @03:28AM (#1763076)
yep, and roughly speaking:

javascript - var - ';' - '{' - '}' | s/function/def/ ~= python

Re:Actual drawbacks to omitting semi-colons? by devmonk on Tuesday October 05, 2010 @09:13PM (#1762308)
I understand the pants thing.
Re:Actual drawbacks to omitting semi-colons? by j_baker on Wednesday October 06, 2010 @12:18AM (#1762687)
I'm fine with coding conventions that aren't anal. Do I wear pants to my friends' houses? Yes. However, I won't go over there many times if the rule is that I have to wear black khakis I bought at Wal mart and have exactly 5 pockets.

Same for coding conventions. Want me to use camel casing for global variables even though I think it's hideous? Fine. Want me to put closing parenthesis on a newline by themselves on a multiline function call and I put it at the end of the last line? Find something better to do with your time.

Unfortunately, I don't know enough JavaScript to know where this falls.

Re:Actual drawbacks to omitting semi-colons? by benjoffe on Wednesday October 06, 2010 @12:22AM (#1762698)
One related issue is how should one mark up code that is intended to flow over multiple lines? Consider doing a long chain like:

MyObj.item.childItem.getAll()[0].properties... // etc.

It's common to want to split this over several lines. I am in favour of placing the the period at the end of the line to indicate that the expression continues. Other people prefer the dot to start the next line instead. I don't like the latter since the first line then looks like a mistakenly omitted semi-colon.

Re:Actual drawbacks to omitting semi-colons? by silentOpen on Wednesday October 06, 2010 @03:10AM (#1763045)
The dot is the operator.

It is customary to stack operators by indenting.

Why are lines without semi-colons at the end bad?

Re:Actual drawbacks to omitting semi-colons? by blahedo on Tuesday October 05, 2010 @11:33PM (#1762593)
Um, ok, the ASI quiz is clearly lame: out of 41 I missed two and wasted one, and I know no Javascript whatsoever. So I'm not sure what that was supposed to prove.
Re:Actual drawbacks to omitting semi-colons? by serichsen on Thursday October 07, 2010 @08:43AM (#1767610)
The problem is that there is an actual drawback to making semicolons optional: adding a newline between tokens can change the meaning of the code!

This "feature" is really just a bad idea. I would hope that it gets thrown out in future versions of the standard (is there one?), and use semicolons in the meantime.

Re:Actual drawbacks to omitting semi-colons? by stevefink on Tuesday October 05, 2010 @11:10PM (#1762547)
I am certain there are words of wisdom in The Pragmatic Programmer book that correlate to this post. I'll update my comment if I find it, but I'm sure someone here will remember it before me.
Re:Actual drawbacks to omitting semi-colons? by js4all on Wednesday October 06, 2010 @02:22AM (#1762952)
I always use semicolons, because I also code in several other languages, that require them. But I have no problem, if someone leaves them out. This is how JavaScript was designed.
Re:Actual drawbacks to omitting semi-colons? by d0m on Wednesday October 06, 2010 @12:07AM (#1762663)
I know this is about semi-colons.. but it's more the comma at first that I found weird.

Example of what I mean:

- "Do a, b, c, ..." vs - "Do a ,b ,c ,..."

Re:Actual drawbacks to omitting semi-colons? by crizCraig on Wednesday October 06, 2010 @02:28AM (#1762962)
Considering semicolons are needed or optional in most languages why not just get in the habit of typing them? Sometimes that means I slip them in my Python code, but everything still works. Just makes more time for thinking about what the code is actually doing.
Re:Actual drawbacks to omitting semi-colons? by jpr on Wednesday October 06, 2010 @03:15AM (#1763057)
Sometimes I wonder how many gigabytes of flamewars about "coding style" C-like syntax has caused.
Re:Actual drawbacks to omitting semi-colons? by Roboprog on Wednesday October 06, 2010 @08:17AM (#1763582)
Indeed. Something to be said for line oriented languages like Ruby, Python, shell-script, xBASE, BASIC (I said that?!?), et al.

This is how it is, now use your time to understand the bigger structure, not punctuation.

Re:Actual drawbacks to omitting semi-colons? by code_duck on Wednesday October 06, 2010 @01:12AM (#1762822)
What? You should use in semi-colons.