5 ms·
Imagine still using semi-colons in JS source in 2020 and "bookmarklet compatibility" being your reasoning.
by slimed 7y ago
Imagine still using semi-colons in JS source in 2020 and "bookmarklet compatibility" being your reasoning.
- jarfil 7y agoSemicolons in JS is how you differentiate between someone who understands that code ambiguity leads to pain, vs someone who shouldn't be allowed near multi-line statements.
- slimed 7y agoPlease 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
- pretty_lorelei 7y agoHonest question: how often does the code ambiguity due to lack of semicolons lead to non-obvious bugs? I'm nowhere close to being a JS expert, but I've done some JavaScript-heavy pet projects and I've got an ambiguous statement interpreted wrong exactly once, and the cause was pretty obvious within a minute of looking at the error.
- z3t4 7y agoIf you accidently write a comma instead of semicolon it can have interesting effects.
- fenomas 7y agoIn my experience, precisely as often as you write statements that start with a left bracket or paren. E.g. [1, 2, 3].forEach( ... ) (function(){ ... })() Statements like that are virtually always errors under ASI, but if you avoid them nothing else causes problems.
- DonHopkins 7y agoIt's not so much how often it happens, but how freaking hard it is to find the problem by inspection when it does happen.
- fenomas 7y agoLinters correctly highlight all the ASI-related that realistically happen. (Or eslint does anyway, can't speak for others.)
- DonHopkins 7y agoGood linters correctly highlight all missing semicolons as a symptom of sloppy, negligent, careless programming (or pointless syntactic showboating). It's also technically syntactically valid to omit braces around single statements after if, else, for, and while statements, but idiotic and dangerous to do that, too. https://wiki.c2.com/?FixBrokenWindows https://wiki.c2.com/?FixBrokenWindows https://www.rtuin.nl/2012/08/software-development-and-the-broken-windows-theory/ https://www.rtuin.nl/2012/08/software-development-and-the-br... https://blog.codinghorror.com/the-broken-window-theory/ https://blog.codinghorror.com/the-broken-window-theory/ https://medium.com/@matryer/broken-windows-theory-why-code-quality-and-simplistic-design-are-non-negotiable-e37f8ce23dab https://medium.com/@matryer/broken-windows-theory-why-code-q...
- detaro 7y agoImagine not understanding why bookmarklet compatibility is cool for something like this and putting people down for it with a cheap "it's 2020" meme.
- slimed 7y agoA bash one-liner solves this problem for you. If you think it's "cool" to execute JS as a bookmarklet you are free to distribute your own version of the library. The original commenter was the one "putting people down" by demanding the author do it "the right way".
- deleted 7y ago[deleted]
- wwweston 7y agoBookmarklets are situationally pretty useful; they’re the easy entry to custom cross-site userscripting, a test drive before going full extension. They’re as legit a use of JS as anything else is. (That said: if you use someone else’s code, putting up with their style is part of the bargain, even if I think leaving out semicolons is pointless aesthetic bikeshedding.)
- hombre_fatal 7y ago> even if I think leaving out semicolons is pointless aesthetic bikeshedding Bikeshedding isn't when you paint the bikeshed your favorite color, it's the bickering over the shade it should be painted which is actually what you did, especially since the damage has already been done. ;) This whole thread is insufferable bikeshedding.