Live data from Hacker News

Can You Find the Bug in This Code?

victorzhou.com

21–30 of 64 posts

Re: Can You Find the Bug in This Code?

#21
Over the years I've submitted pull requests to 3 different projects that consist of a single semicolon in a semicolon-free codebase. They can be sneaky bugs to catch!

I think abusing ASI is a funny hack (and I did it for a while) but the more functional my programming style became the more ran into these cases, so I channeled my inner Douglas Crockford and reopened my bag of semicolons.

Re: Can You Find the Bug in This Code?

#22
post #7

The real bug is the failure to use semi-colons. I literally cannot comprehend the non-use of semi-colons in JS.

If so, the failure lies with the spec. (personally, I like ASI)

Cue Principle Skinner meme:

"Is the language specification too loose?"

"No, it's the developers who are wrong."

Re: Can You Find the Bug in This Code?

#23
Happy to say I caught this, although I would credit that as much to having taken a compilers course as any JS knowledge.

I'm not sure I understand why it's wrapped in an enclosing function, though; you can reproduce the error with just:

    (() => console.log("Hello"))()

    (() => console.log("World"))()

Re: Can You Find the Bug in This Code?

#24
post #13

Earlier quoted context omitted.

I'm not sure always prepending those with ";", but definitely when needed, sure. But most of the time you can simply refactor the code a little bit and improve readability with the addition of a single variable.

Always for ( and [ when they start a statement. It really is that simple.

And for template strings: `

I do agree that good use cases are few and far between.

Re: Can You Find the Bug in This Code?

#25

Always prepend ( and [ with ; I also prefer void function () {} for this. Void the result of this function expression which I immediately invoke.

I agree. Those are the only two cases where a semicolon is needed. Other than that, I like my code to look clean :)

Re: Can You Find the Bug in This Code?

#26

An even more common use-case that fails without semicolons by T.J. Crowder (2015): http://blog.niftysnippets.org/2015/09/automatic-semicolon-in... There are a number of cases where not having semicolons has its downsides, I'm yet to see a single case of an upside.

> I'm yet to see a single case of an upside.

It's a signal of coolness from inexperienced programmers. That's the value proposition offered by semicolon-free JS.

Re: Can You Find the Bug in This Code?

#29
post #17
post #10

I refuse to use semi-colons in TS/JS, unless that conflicts with existing code standards. I feel like it's as if I'm using semi-colons in Go. This can easily be avoided by writing clearer code.

Relevant: https://standardjs.com/rules.html#semicolons (hint: no semi-colons!)

Which also recommends

    ;(function () {
      window.alert('ok')
    }())
Which, just, eww.

Just use semicolons. Everywhere, always. Then you can treat JS's parsing rules as the same as every other language, and not normally like every other language except when a line begins with one of a select set of magical charaters that need to be prepended with `;` for unclear reasons.

Just end lines with `;`.

Re: Can You Find the Bug in This Code?

#30
It's too bad there isn't a less ambiguous way to write programming language expressions with, say, prefix notation, using a single set of characters (open and close parens maybe?) to describe the tree, etc..., and getting rid of semicolons and curly braces altogether.
Post reply on HN