top | item 19135552

Can You Find the Bug in This Code?

43 points| vzhou842 | 7 years ago |victorzhou.com

60 comments

order
[+] slyrus|7 years ago|reply
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.
[+] Jim_Heckler|7 years ago|reply
And perhaps we could repurpose those semicolons for comment syntax ;-)
[+] scandox|7 years ago|reply
The real bug is the failure to use semi-colons. I literally cannot comprehend the non-use of semi-colons in JS.
[+] adrianhel|7 years ago|reply
If so, the failure lies with the spec. (personally, I like ASI)
[+] ng12|7 years ago|reply
Close to 100% of semi-colons are noise in any given project. That's why.
[+] mrspeaker|7 years ago|reply
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.

[+] adrianhel|7 years ago|reply

    ;['but', 'why']
      .reduce((msg, word) =>
        msg + word,
        ''
      ).join(' ') +'?'
[+] Etheryte|7 years ago|reply
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.

[+] beering|7 years ago|reply
> 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.

[+] Legogris|7 years ago|reply
Semicolons are usually optional in JS. The 'usually' part if why it's a good idea to enforce usage of them to avoid situations like this. You don't even need to do IEEs for this - running a function on an inline-declared array has a high probability of creeping the array up as an index on the previous expression.
[+] adrianhel|7 years ago|reply
> Has a high probability of...

No. It either does or it doesn't. The spec is not ambigous.

Learn how ASI works, or you will run into trouble no matter if you prefer semis or not.

[+] czr|7 years ago|reply
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"))()
[+] tastyfreeze|7 years ago|reply
What kind of heathen omits semicolons in JavaScript

Thats like neglecting punctuation in English You can but the punctuation makes it much easier to read

[+] TeffenEllis|7 years ago|reply
I think what's more confusing than ASI are the mental gymnastics developers go through to justify typing semicolons daily, rather than installing a linter once, or reading the ASI spec.

Python and Ruby support semicolons for occasional statement termination; why is JavaScript any different? I can't help but think "semicolons by default" would be regarded as ritualistic if the practice was never popularized in the first place.

[+] kurtisc|7 years ago|reply
Does pressing a key on the home row really require a lot of justification? A project doesn't care how effective my tooling is if the next person doesn't have it.
[+] adrianhel|7 years ago|reply
Always prepend ( and [ with ;

I also prefer void function () {} for this. Void the result of this function expression which I immediately invoke.

[+] ezekg|7 years ago|reply
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.
[+] marshall313|7 years ago|reply
I agree. Those are the only two cases where a semicolon is needed. Other than that, I like my code to look clean :)
[+] adrianhel|7 years ago|reply
Correction: void function () {}()
[+] telekid|7 years ago|reply
I ran into this in the pre-webpack era where it was somewhat common to just concatenate JS together in a gulp build step. We imported code from two NPM modules, and each one was wrapped in an IFEE, but the first one didn't terminate with a semicolon. Not fun to debug.
[+] ezekg|7 years ago|reply
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.
[+] jakear|7 years ago|reply
My problem with semi-colons is that when you force semicolons in a tslint config, applying an autoformat on a malformed input (forgetting a closing curly brace, for instance) will introduce a bunch of erroneous semicolons throughout the file, giving you a ton of syntax errors to clean up. If you’re lucky you’ll be able to undo before you continue, but I sometimes don’t notice until I’ve done a bunch of other work
[+] kowdermeister|7 years ago|reply
When you assume the compiler reads it the same way your brain does :)
[+] jyriand|7 years ago|reply
“Must be a semicolon issue,” was my first thought, without actually knowing why.
[+] mikewhy|7 years ago|reply
Another instance where TypeScript has your back.
[+] kgwxd|7 years ago|reply
The bug is in the coder.
[+] gammateam|7 years ago|reply
I feel good that my first thought of fixing it would have created the solution.

There were obvious errors in my mind that I would fix without knowing the whole dissertation on why it happened.

So maybe if the question was, "Can you repair this code", I would excel.

[+] Someone1234|7 years ago|reply
Ditto. I didn't know it was a missing semicolon, but the code smells bad regardless and my solution also would have solved it another way (just by improving code readability/clarity).

It is likely one of these "trust your gut" situations.