3 ms·
Leaving out semicolons leads to errors. Ambiguity etc... return [1,2,3] What should that do? Should it return, or return [1,2,3]? There are many other exampl
by axod 18y ago
Leaving out semicolons leads to errors. Ambiguity etc...
return
[1,2,3]
What should that do? Should it return, or return [1,2,3]?
There are many other examples. Use semicolons and there is no debate over what should happen. Making them optional in javascript was a ridiculous idea.
- timknauf 18y agoKeep in mind that I come from a mainly Python and Ruby background; both of those languages have always had newline-terminated statements. Yes, neither Python nor Ruby nor newline-terminated Javascript would return the desired result. But neither language seems to be suffering hugely as a result. (As far as I know.) Why would you write a return statement like that in the first place? (Not intended as flamebait - I am genuinely curious to know.) If it is a simple typo that separates the 'return' from the '[1,2,3]', then isn't the negligent typist just as likely to leave off the all-important semicolon? Or is there a genuine reason you might want to put the [1,2,3] on a line by itself?
- axod 18y agook here's a more usual situation in which the programmer is blindly using their favorite bracketing style, and come across the issue... function getObject() { return // returns undefined, not the object. { age: 30, name: "axod" } } Personally I hate that style of bracketing, but some swear by it.
- timknauf 18y agoYeah, not really my favourite bracketing style either. Good example though - it is conceivable that this could happen.
- halo 18y agoJavaScript syntax lets you write statements over multiple lines - if the first line cannot be parsed correctly it assumes you meant to continue over to the second line. This leads to odd error messages and JavaScript being harder to debug without semicolons. Note that this is different to, say, Python, where newline explicitly indicates termination of a statement.
- timknauf 18y agoWell, Python will let certain statements continue over multiple lines as well, e.g. someFunction(parameterOne, parameterTwo) I think maybe the difference is that Python has always treated newlines the way it does now. JavaScript, on the other hand, has developed idioms based on the semicolon model. So has the rug has been snatched out from underneith its users by suddenly making them optional? Does the change make a lot of useful idioms harder to implement?
- cabalamat 18y agoI agree. You often get multi-line statements and in many languages/APIs they're a nature way of doing things. Here's an example (in no particular language): win = Window.new() .setTitle("A new window") .setForeground(Red) .setBackground(White) .setMenuBar(fileMenu, viewMenu, helpMenu); I think this would be uglier and more error-prone if instead you had to use \ or some other character to indicate they are all part of the same expression: win = Window.new()\ .setTitle("A new window")\ .setForeground(Red)\ .setBackground(White)\ .setMenuBar(fileMenu, viewMenu, helpMenu);
- timknauf 18y agoInteresting. I hadn't come across that idiom before. In Python, you'll have things like: myLongList = [1, 2, 3, 4, 5, 6, 7, 8, 9] No continuation character is needed, because the continuation is implied. However, I can't think of a direct parallel to your example above. It looks handy. What API/language are you alluding to? I guess the Pythonic way would be something like: win = Window(title = "A new window", foreground = "Red", background = "White", menuBar = [fileMenu, viewMenu, helpMenu]) But I disgress. :^)
- deleted 18y ago[deleted]
- sc 18y agoJavaScript can just as easily attain this way of using named parameters: win = Window.new({title: "A new window", foreground: Red, background: White, menuBar: [fileMenu, viewMenu, helpMenu]});
- cabalamat 18y agoWhat API/language are you alluding to? As i said before: no particular language.
- sc 18y agoThe dot notation itself indicates continuation: win = new Window(). setTitle("A new window"). setForeground(Red). setBackground(White). setMenuBar(fileMenu, viewMenu, helpMenu);
- timknauf 18y agoMaking them optional in javascript was a ridiculous idea. But since they are optional now, using them throughout our code won't help us for the example above: it would still wrongly return undefined because of the implied statement end. So it could be argued that semicolons provide a false sense of security in this new, semicolon-optional world. :^)
- lacker 18y agoHere's a more plausible example. Let's say you are trying to call one function out of a list of functions. What do you think this code does? function getIndex() { return 0 } function f1() { alert("foo") } function f2() { alert("bar") } var intIndex = getIndex() [f1, f2][intIndex]() It looks like it should alert "foo", but run it and you get an "intIndex is undefined" error. Good luck debugging that! The problem is, the javascript rules for continuing lines are that whenever the next character is any of [({+-, the line is assumed to continue. This is generally correct but not always. For comparison, the analogous Python rule is that a line continues either with an explicit \ or when one of [({ has not yet been closed with its pair. The Python rule doesn't lead to any ambiguous cases that I am aware of, because unclosed parentheses would just be invalid syntax.
- timknauf 18y agoInteresting. I've encountered errors in a similar vein, which in retrospect must have been caused by using an anonymous list in a similar way to your example: [f1, f2][intIndex]() The 'solution' seems to be something like var functions = [f1, f2] functions[intIndex]() but that's a big loss for conciseness.
- lacker 18y agoThe most convenient solution is just using a semicolon. If the line continuation rules were the Python ones, this would "just work"....