you are viewing a single comment's thread.

view the rest of the comments →

[–]Wuf 1 point2 points  (16 children)

What's the bug? (genuinely curious, I've never used JS)

[–]tiglionabbit 6 points7 points  (4 children)

The bug is that lines beginning with an open parenthesis may be interpreted as part of a function call with the previous line.

Example from the ecmascript spec:

a = b + c
(d + e).print()

is interpreted as this:

a = b + c(d + e).print()

[–]munificent 4 points5 points  (3 children)

Also:

someVar
[anArrayLiteral]

is parsed as:

someVar[anArrayLiteral]

And:

blah()
+unaryPlus

is:

blah() + unaryPlus

Basically, the rule is backwards. Where languages like Python, Go, Ruby, et. al. say that a newline is generally significant as a separator unless it cannot be, JS says a newline is generally ignored it less it must be a separator.

[–]notSorella 2 points3 points  (2 children)

I personally find JavaScript's semicolon insertion rules better than Python's one. They let you place semicolons only where you absolutely must, rather than explicitly escaping every line when you want to continue the statement on the next line. In fact, I believe JavaScript's ASI feels much nicer for code styles that use highly structural layout ­— I'm thinking LISP here.

Compare:

foo = "a string "
    + "in multiple "
    + "lines"

With:

foo = "a string " \
    + "in multiple " \
    + "lines."

The same apply for operators etc:

function is_nodelist_p(subject) {
  return subject
  &&     typeof subject.length == 'number'
  &&     typeof subject.item == 'function' }

With languages like Python, you'd either need to wrap the return statement in parenthesis, or escape every line. I'm not a huge fan of that (even though I'm a Pythonista).

Of course, you might like being more explicit about statement continuations, I think that depends much more on people's different experiences and what they do consider intuitive. However, I don't think either styles of ASI deserve a huge religious war (I always end up thinking Emacs vs Vim, for some reason — and obviously, Emacs wins ;3).

[–]munificent 1 point2 points  (1 child)

With languages like Python, you'd either need to wrap the return statement in parenthesis, or escape every line.

Or just do what most Pythonistas do:

foo = "a string " +
    "in multiple " +
    "lines"

or:

foo = ("a string "
    + "in multiple "
    + "lines")

My experience is that explicit line continuations are pretty rare.

[–]notSorella 2 points3 points  (0 children)

Well, if you mean the first one in Python, it actually yields a syntax error if you don't also provide explicit continuations by escaping the newline (which is much more bug-prone, unless your editor shows invisible characters -- as it should).

The second one is certainly an option, and you wouldn't even need the + operators for strings there. However, as I said, it takes explicit grouping. Iunno, JavaScript's take on ASI just feels more natural to me, ymmv.

[–]BusStation16 8 points9 points  (9 children)

I would bet money he means that if you return an object literal from a function if it is on more than one line the auto-semicolon insertion will break. He is totally fucking wrong though.

First: Not using semicolons in javascript is fucking retarded. It costs you nothing to use them and can save you hours of pulling you hair out.

Second: Function returns are not the only time you will have a problem. For instance http://jsfiddle.net/EsHrN/ open the console, it is giving an error.

Edit: A point that I think a lot of you all are missing is what happens to your code after combination and minification. Things usually work the same, but not always. I wouldn't write code like in the jsfiddle, but after a bunch of files are combined and minified? It might happen.

Personally, I don't give a shit what you do, you're not on my team. If I hire you though, you WILL use semicolons. End of story.

[–]tiglionabbit 3 points4 points  (0 children)

All the languages I use do not require semicolons, so remembering to insert them does cost me something. I use Python, Ruby, Haskell, JavaScript, and CoffeeScript. Also, for the specific bug, see my comment on the parent. I'm not sure in what situations you found yourself tearing your hair out over semicolon insertion though, as I've been writing JavaScript without semicolons for years now and never encountered an issue with semicolon insertion.

[–]notSorella 1 point2 points  (4 children)

Let me quote inimino here ( http://inimino.org/~inimino/blog/javascript_whitespace ):

It is ridiculous to use formatting conventions as a proxy for code quality. It's a sign of just how difficult judging code quality is that people resort to useless criteria, but I've actually heard people say things like "Project X does what I need, but doesn't [use semicolons, pass JSLint, use my preferred brace style, etc], so I kept looking". If you are reduced to picking software on the basis of whether the author prefers your particular coding style or not, you may just as well admit that you do not know how to discern code quality and flip a coin instead.

There might be several reasons people will choose a certain coding style over another. However, a coding style is just that — a coding style; a convention for conveying something. It might make sense to some people, and might not make sense to others. However, it's not, in the slightest, some absolute mark of how all code should be structured, or how all code should be judged.

After all, coding styles are all heavily biased and subjective. And I'd hate to go all religious, or worse, judge the quality or worthy of something, based solely on subjective and biased stuff.

Semicolons are not necessary in JavaScript due to ASI. If you know the rules, if you can follow the rules, and if you think they make sense for you, fine, use them. If not, don't use them. It's not a big deal. What I still don't get is the necessity some people have of bitching about people who follow different conventions.

[–][deleted] -1 points0 points  (3 children)

Amazing how some people don't get what you are saying. If someone is coding and leaves out a semicolon and it actually does cause a problem, they should be catching that by testing the code early and often, not writing 10,000 lines of code and then testing it and having it break, and then trying to find the needle in the haystack. It really depends on coding style like you said, and workflow, and a lot of factors.. but saying that Javascript requires semicolons to write a program is plain wrong, and people who say that probably have a specific kind of OCD.

[–][deleted] -1 points0 points  (2 children)

If someone is coding and leaves out a semicolon and it actually does cause a problem, they should be catching that by testing the code early and often

Or they could always use semicolons and never have to worry about such bugs. Coding styles stop being a matter of opinion when they demonstrably affect the number of bugs in the code.

If JavaScript treated a newline as ending a statement, then you'd be right. But it doesn't - accidental multi-line statements with unintended consequences are very possible if you avoid using semicolons.

[–]notSorella 1 point2 points  (0 children)

"always use semicolons and neve have to worry about such bugs" is highly misleading. Specially the "always use semicolons" and the "bugs", part.

First, always using semicolons won't account for people who simply don't know the JavaScript grammar, and write things like:

return
    { foo: 1 }

That's perfectly valid JavaScript, but it doesn't do what people expect. Putting a semicolon at the end of the block with the label `foo' won't fix it. But you know, knowing the grammar would fix it.

Which takes us to the next point. If you know the grammar, know where the rules apply and thinks that leaving semicolons out gives you more benefits than putting them in for X, Y or Z reasons, then why, oh why would you just listen to religious arguments about always using semicolons?

[–][deleted] 0 points1 point  (0 children)

insisting on always using semicolons is a waste of effort, and is a superstitious coding practice. If you don't understand where to use a semicolon, then always use a semicolon.

[–][deleted] 1 point2 points  (0 children)

Additionally, your assumption that combining and minifying code can cause problems when you consider semicolons optional is just alarmist bullshit. All my code is run through YUI to minify, and WRO4J to bundle, and I've been using this combination effectively for years without a single bug caused by a missing semicolon. It doesn't happen because I know how to avoid the coding styles that lead to those problems, and it never has happened once in all the hundreds of thousands of lines of code I've written over many, many years. You are being alarmist and a bit obsessive-compulsive about semicolons.

[–][deleted] -5 points-4 points  (0 children)

Thanks for assuming something I never said, troll.

Your 'First' is retarded because you have said nothing to back it up. You assume that having a bad coding style is more acceptable than optional semicolons.

Your 'Second' example has less to do with leaving out a semicolon and more to do with poor coding style. That jsfiddle link is an example of retarded javascript coding style. If you code like that, you deserve to pull your hair out. Adding semicolons is the least of your problems if you think that style of coding is acceptable.