4 ms·
Please give a concrete example of this pain that you have actually experienced. I understand that ASI (automatic semi-colon insertion) is in theory ambiguous be
by slimed 7y ago
Please give a concrete example of this pain that you have actually experienced. I understand that ASI (automatic semi-colon insertion) is in theory ambiguous because you are relying on the compiler, or in some cases many different compilers, to execute the statement correctly. In practice, for over a decade, I have never run into such an issue. I suspect it's due to engineers trying to get too cute with their implementations.
- z3t4 7y agoif(foo) doX() doY()
- slimed 7y agoFor the benefit of the rest of us, can you explain what issues this causes and where/how you are running this code?
- NoInputSignal 7y agoIntuitively this becomes: if(foo) doX() doY(); Intuitively, as a someone who uses C style languages, this becomes: if(foo) doX(); doY(); Can someone confirm which intuition is correct? I ask because, to me: it not being intuitive is a problem--even if it is not classified as ambiguous. It being only contextually intuitive, also seems somewhat problematic--but maybe excusable if it is assumed you know some similar language going into it and it behaves similarly to that other language.
- fenomas 7y agoNo idea what GP was trying to say, but the C style intuition is correct. The bug in that code is due to omitted brackets, it has nothing to do with semicolons/ASI.
- z3t4 7y agoMy point was that its hard to tell where the semicolon will be inserted. As well as the intention of the programmer. Here is another example: if(x) return "foo" return callback (1,2,3,4)
- fenomas 7y agoI think I see what you're saying, but the context here was asking for cases where omitting semicolons causes non-obvious bugs. I mean: if(x) return "foo" clearly there's a bug there, but adding in semicolons wouldn't change the code's behavior. So it doesn't affect the "is it bad to omit semicolons?" debate either way.
- z3t4 7y agoif(y) return "bar" The linter will tell you there should be a semicolon, and you are like, - no it shouldn't.
- z3t4 7y agofoo=arr[i] (counter++)
- gherkinnn 7y agoA typical response is return false Where the program returns undefined. I can only think of these examples as straw men. Not once did I run in to an ASI bug.
- slimed 7y agoThank you. I have yet to see an example in this thread or anywhere else that did not fall into this category.
- fenomas 7y agoNote though, that this ambiguity doesn't really affect the whole "whether to use semicolons" debate either way. If anything, it's more likely to occur in code that does use semicolons than in code that doesn't.
- gherkinnn 7y agoQuite right. As do other examples.
- Etheryte 7y agoWithout opening your console, consider whether the below code throws or not and why: var a = [] var b = '' var c = {} [].concat.apply(a, [b, c])
- hombre_fatal 7y ago;[]... is one of two rules you memorize when you omit semis. It's not even that common of a case, though your own example doesn't even make sense. I don't really understand the drama, just like people who use semis in place you don't need them. In real code, you should be using a linter anyways. At which point semis or no semis becomes purely an aesthetic concern. Like whether you use single or double quotes. I haven't used semicolons in over 8 years and never had an issue. And with tools like eslint and prettier, you have no excuse to be making such trivial mistakes, like adding superfluous semicolons like function(){};
- DonHopkins 7y agoI have other more important rules I need to memorize with those brain cells, that I don't need to voluntarily choose to give myself more problems that require me to memorize ridiculous rules and obscure special edge cases to look out for all the time, that inexplicably screw me if I happen to slip up and forget to apply them. I stopped playing "step on a crack, break your mother's back" years ago, when I was old enough to actually have somewhere I needed to be.
- DonHopkins 7y agoCrockford on Bootstrap's semicolon omission: “insanely stupid code” (github.com) https://news.ycombinator.com/item?id=3842713 https://news.ycombinator.com/item?id=3842713 https://github.com/twbs/bootstrap/issues/3057 https://github.com/twbs/bootstrap/issues/3057 And don't forget to semicolon-harden your most important mission-critical code: https://www.bluejava.com/4PN/Semicolons-in-JavaScript---The-Definitive-Guide https://www.bluejava.com/4PN/Semicolons-in-JavaScript---The-... ;;;;;;;;;;;; // protective wall of semicolons ;;var a = 10, b = 20;; ;; ;;function add(a,b) ;;{;; ;;return a + b;;;;;;;; // this function MUST work ;;};; ;;;;;;;;;;;; // protective wall of semicolons