Live data from Hacker News

Can You Find the Bug in This Code?

victorzhou.com

31–40 of 64 posts

Re: Can You Find the Bug in This Code?

#31
post #17

Earlier quoted context omitted.

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

>Just end lines with `;`.

But that's not always the solution, you still need to know when ASI kicks in and when it doesn't.

"just end lines with ;" isn't true in multi-line arrays, in multi-line objects, and a ton of other situations where a semi at the end of the line is a syntax error. And while i'm sure you know that and didn't actually mean "every" line, it just goes to show that you already have the knowledge of when to add them and when not to. But that knowledge is learned, and it isn't a simple rule by any means.

For example, the following code is probably not going to do what many beginners think it will do:

    return
      [
        1,
        2,
        3,
      ];
Because the JS engine will insert a semicolon right after the return, and you will basically get `return undefined;` as the result.

Semicolons at the end of lines or not, you still need to understand when they are inserted and when they aren't. The real solution is to use a linter which will check these cases for you. I don't care if you use them or don't, just make sure something else enforces it.

Re: Can You Find the Bug in This Code?

#32
post #17

Earlier quoted context omitted.

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

Which also states this, directly under that "bad" example:

Note: If you're often writing code like this, you may be trying to be too clever.

Clever short-hands are discouraged, in favor of clear and readable expressions, whenever possible.

Instead of this:

    ;[1, 2, 3].forEach(bar)
This is strongly preferred:

    var nums = [1, 2, 3]
    nums.forEach(bar)

Re: Can You Find the Bug in This Code?

#33
post #17

Earlier quoted context omitted.

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

Yeah, use ";" to denote the end of a statement.

Or if you're serious, switch to BuckleScript and reduce all your side-effecting expressions down to unit. (Fully eliminates unhandled promise bugs, for one thing.)

Re: Can You Find the Bug in This Code?

#34
post #17

Earlier quoted context omitted.

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

Or just don't use classic IIFEs

    void function () {
      window.alert('ok')
    }()
So much cleaner.

Or better yet, stop using IIFEs. Unless you need IE11 support and can't transpile your code there is absolutely no need.

    {
      let x = 42
    }
    console.log(typeof x) // undefined

Re: Can You Find the Bug in This Code?

#36
post #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.

And perhaps we could repurpose those semicolons for comment syntax ;-)

Re: Can You Find the Bug in This Code?

#37

Earlier quoted context omitted.

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

>Just end lines with `;`. But that's not always the solution, you still need to know when ASI kicks in and when it doesn't. "just end lines with ;" isn't true in multi-line arrays, in multi-line objects, and a ton of other situations where a semi at the end of the line is a syntax error. And while i'm sure you know that and didn't actually mean "every" line, it just goes to show that you already have the knowledge of…

You're absolutely correct. Jey provided the better phrasing of my comment: Use `;` to end a statement. The point I'm making is more bias toward using `;`, even when not strictly "necessary". This brings JS's statement parsing in line with most other languages. And yes, absolutely use a linter.

Re: Can You Find the Bug in This Code?

#38
post #32

Earlier quoted context omitted.

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

Which also states this, directly under that "bad" example: Note: If you're often writing code like this, you may be trying to be too clever. Clever short-hands are discouraged, in favor of clear and readable expressions, whenever possible. Instead of this: ;[1, 2, 3].forEach(bar) This is strongly preferred: var nums = [1, 2, 3] nums.forEach(bar)

The irony is that building a language which avoids semicolons by automatically inserting them is arguably taking a clever short-hand in favor of unclear and ambiguous expressions.

Re: Can You Find the Bug in This Code?

#39

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.

    ;['but', 'why']
      .reduce((msg, word) =>
        msg + word,
        ''
      ).join(' ') +'?'
Post reply on HN