News for hackers. Stuff that matters.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by slimed on Saturday February 15, 2020 @01:56PM (#22337129)
Imagine still using semi-colons in JS source in 2020 and "bookmarklet compatibility" being your reasoning.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by jarfil on Saturday February 15, 2020 @02:25PM (#22337328)
Semicolons 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.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by pretty_lorelei on Saturday February 15, 2020 @02:34PM (#22337399)
Honest 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.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by fenomas on Saturday February 15, 2020 @09:58PM (#22339554)
In 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.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by DonHopkins on Saturday February 15, 2020 @11:02PM (#22339713)
It's not so much how often it happens, but how freaking hard it is to find the problem by inspection when it does happen.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by fenomas on Sunday February 16, 2020 @12:33AM (#22339964)
Linters correctly highlight all the ASI-related that realistically happen. (Or eslint does anyway, can't speak for others.)
Re:Plink-plonk.js – hear webpages render with DOM observer a... by DonHopkins on Monday February 17, 2020 @05:12PM (#22351349)
Good 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://www.rtuin.nl/2012/08/software-development-and-the-br...

https://blog.codinghorror.com/the-broken-window-theory/

https://medium.com/@matryer/broken-windows-theory-why-code-q...

Re:Plink-plonk.js – hear webpages render with DOM observer a... by z3t4 on Saturday February 15, 2020 @03:09PM (#22337633)
If you accidently write a comma instead of semicolon it can have interesting effects.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by slimed on Saturday February 15, 2020 @02:48PM (#22337492)
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.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by DonHopkins on Saturday February 15, 2020 @11:06PM (#22339721)
Crockford on Bootstrap's semicolon omission: “insanely stupid code” (github.com)

https://news.ycombinator.com/item?id=3842713

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-...

    ;;;;;;;;;;;; // protective wall of semicolons
    ;;var a = 10, b = 20;;
    ;;
    ;;function add(a,b)
    ;;{;;
        ;;return a + b;;;;;;;; // this function MUST work
    ;;};;
    ;;;;;;;;;;;; // protective wall of semicolons
Re:Plink-plonk.js – hear webpages render with DOM observer a... by Etheryte on Saturday February 15, 2020 @06:06PM (#22338668)
Without opening your console, consider whether the below code throws or not and why:

  var a = []
  var b = ''
  var c = {}
  [].concat.apply(a, [b, c])
Re:Plink-plonk.js – hear webpages render with DOM observer a... by hombre_fatal on Saturday February 15, 2020 @08:51PM (#22339338)
;[]... 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(){};

Re:Plink-plonk.js – hear webpages render with DOM observer a... by DonHopkins on Saturday February 15, 2020 @11:19PM (#22339764)
I 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.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by gherkinnn on Saturday February 15, 2020 @04:16PM (#22338040)
A 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.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by fenomas on Saturday February 15, 2020 @10:23PM (#22339614)
Note 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.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by gherkinnn on Sunday February 16, 2020 @01:21AM (#22340129)
Quite right. As do other examples.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by slimed on Saturday February 15, 2020 @04:30PM (#22338123)
Thank you. I have yet to see an example in this thread or anywhere else that did not fall into this category.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by z3t4 on Saturday February 15, 2020 @03:22PM (#22337713)

    foo=arr[i]


    (counter++)
Re:Plink-plonk.js – hear webpages render with DOM observer a... by z3t4 on Saturday February 15, 2020 @03:16PM (#22337675)

    if(foo)
      doX()
      doY()
Re:Plink-plonk.js – hear webpages render with DOM observer a... by NoInputSignal on Saturday February 15, 2020 @04:51PM (#22338232)
Intuitively 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.

Re:Plink-plonk.js – hear webpages render with DOM observer a... by fenomas on Saturday February 15, 2020 @10:06PM (#22339574)
No 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.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by z3t4 on Sunday February 16, 2020 @03:35AM (#22340574)
My 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)
Re:Plink-plonk.js – hear webpages render with DOM observer a... by fenomas on Sunday February 16, 2020 @04:12AM (#22340665)
I 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.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by z3t4 on Sunday February 16, 2020 @07:14AM (#22341200)

    if(y) return
      "bar"
The linter will tell you there should be a semicolon, and you are like, - no it shouldn't.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by slimed on Saturday February 15, 2020 @03:28PM (#22337749)
For the benefit of the rest of us, can you explain what issues this causes and where/how you are running this code?
Re:Plink-plonk.js – hear webpages render with DOM observer a... by wwweston on Saturday February 15, 2020 @06:37PM (#22338815)
Bookmarklets 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.)

Re:Plink-plonk.js – hear webpages render with DOM observer a... by hombre_fatal on Saturday February 15, 2020 @09:09PM (#22339404)
> 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.

Re:Plink-plonk.js – hear webpages render with DOM observer a... by detaro on Saturday February 15, 2020 @02:33PM (#22337385)
Imagine not understanding why bookmarklet compatibility is cool for something like this and putting people down for it with a cheap "it's 2020" meme.
Re:Plink-plonk.js – hear webpages render with DOM observer a... by slimed on Saturday February 15, 2020 @02:40PM (#22337442)
A 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".