Earlier quoted context omitted.
If so, the failure lies with the spec. (personally, I like ASI)
I feel the same way about ASI as I do about forgiving HTML parsers. They're built to handle mistakes made by your average weekend warrior Grandpa building a website about his grandkids and pets. ASI is built to handle an "oops", not an entire codebase. That's why there are these odd edge cases. The same is true with the English language. If one were to send me a text with "do ya think" I'd probably figure out that it…
Can You Find the Bug in This Code?
51–60 of 64 posts
Re: Can You Find the Bug in This Code?
#52Earlier 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.
I don't really write IIFEs since let and const. I'm sure the outputs from babel/typescript/etc use them, but I don't really need to write them myself, now that we have proper block scoping.
And if I want to declare an array and immediately iterate over it, I just put it in a variable first. That gives it a name, too, which tends to improve readability anyways.
I work with Javascript / Typescript projects on both sides of the argument, and I have to say not using semicolons never seems to cause any problems. Especially not with auto formatting / linters around.
Re: Can You Find the Bug in This Code?
#53I 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 fir…
Re: Can You Find the Bug in This Code?
#54Semicolons 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.
> 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.
Re: Can You Find the Bug in This Code?
#55Earlier quoted context omitted.
If so, the failure lies with the spec. (personally, I like ASI)
I feel the same way about ASI as I do about forgiving HTML parsers. They're built to handle mistakes made by your average weekend warrior Grandpa building a website about his grandkids and pets. ASI is built to handle an "oops", not an entire codebase. That's why there are these odd edge cases. The same is true with the English language. If one were to send me a text with "do ya think" I'd probably figure out that it…
Honestly -- then they'll never be convinced. I haven't hit an ASI bug in the last decade.
Re: Can You Find the Bug in This Code?
#56Earlier quoted context omitted.
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.
Or you can do what a past coworker did. We settled on standard.js for our linting in a project, and they don't use semicolons. The coworker hated this, so he setup his editor to auto-format into his preferred style on load, then on save use the auto-format option in standard.js to save it as the format that we agreed on in the repo. He only ever worked with his favorite layout, and we worked with ours! I'm not a big…
Re: Can You Find the Bug in This Code?
#57Earlier quoted context omitted.
> 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.
I disagree, I think people who really care about semicolons in JS are people who just don't write a lot of JS. I can honestly say in the last 5 years of only writing front-end I have never been bit by ASI.
So, sorry but you couldn't possibly be more wrong. The great majority of the other commenters here disagree with you as well. You got lucky if you have in fact never had an ASI bug as you claim, but in this thread there is at least one link to a famous bug that has been caused by it and mention of others. The fact that you anecdotally gambled and "won" doesn't hold a lot of weight.
There is one much less popular style guide which actually made headlines for recommending against semi-colons and they got roundly criticized for it by many, many famous JS programmers including the likes of Dan Abramov from Facebook. The author of this style guide tried to weasel more popularity out of it by calling his guide "Standard" since he knows for a fact that the vast majority of JS coders use a de facto style that includes semi-colons. That's exactly the type of behavior that I expect from young, inexperienced coders who make poor decisions based on vanity. I see it all day long from junior programmers. Luckily for everyone in my company, it's my job to set them straight.
Re: Can You Find the Bug in This Code?
#58Earlier quoted context omitted.
I disagree, I think people who really care about semicolons in JS are people who just don't write a lot of JS. I can honestly say in the last 5 years of only writing front-end I have never been bit by ASI.
The most popular JS style guide from AirBnB uses semi-colons. Google and Facebook uses semi-colons. I'm pretty sure they collectively write way more JS than you do.The most popular code formatter also defaults to having semi-colons because they know it's the de factor standard in JS. I myself have been writing JS since the late 90s (around the time when XHR was introduced) and it's been my most used language for easi…
I'm sure they love being told they're wrong because they're young and vain.
I'm aware styleguides recommend semis but I'm more referring to people who have strong opinions about what is a minor semantic. For my part it's simple: the effort involved using semicolons correctly (and in my experience many devs who like semis use them inconsistently) is significantly greater than the effort to fix/prevent ASI bugs. The only possible caveat to this is if you're using a code-formatter like Prettier to handle semis for you -- but then Prettier will catch and fix ASI bugs for you even if you don't.
Re: Can You Find the Bug in This Code?
#59Earlier quoted context omitted.
The most popular JS style guide from AirBnB uses semi-colons. Google and Facebook uses semi-colons. I'm pretty sure they collectively write way more JS than you do.The most popular code formatter also defaults to having semi-colons because they know it's the de factor standard in JS. I myself have been writing JS since the late 90s (around the time when XHR was introduced) and it's been my most used language for easi…
> Luckily for everyone in my company, it's my job to set them straight. I'm sure they love being told they're wrong because they're young and vain. I'm aware styleguides recommend semis but I'm more referring to people who have strong opinions about what is a minor semantic. For my part it's simple: the effort involved using semicolons correctly (and in my experience many devs who like semis use them inconsistently)…
I wouldn't say that, but it is a decision based on vanity and it does tend to come from younger/junior coders in my experience. Making one bad decision based on vanity doesn't make you vain though IMO. It's not personal vanity either, it's "code vanity" so maybe a better word would be: hasty or risky...
Anyway, the effort that it takes to setup and use Prettier (which we use) or before that, "eslint --fix", is the least amount of effort of all the options.
You're expending more effort to memorize edge-case rules than my teams are if you're not using Prettier and if you are using Prettier but without semis, then you risk running into a bug like the one that broke Twitter Bootstrap due to lack of semis.
It's just a bad decision.
> I think people who really care about semicolons in JS are people who just don't write a lot of JS...
> I'm aware styleguides recommend semis but I'm more referring to people who have strong opinions about what is a minor semantic.
It's not just some guides, it's the majority of the most popular style guides and the biggest JS-using companies that write and use those guides, which recommend semis.
It's also not a minor semantic. The language requires them and if you make the error of omitting them, the runtime will try and correct your error. Depending on this runtime behavior is the kind of hasty and shallow decision that costs real money. That's why the most popular style guides recommend them and that's why the de fact standard is to use them. Googles style guide requires them company wide. The people who wrote these guides really cared about semicolons, that's why they put it in the rules.
Re: Can You Find the Bug in This Code?
#60Earlier quoted context omitted.
> Luckily for everyone in my company, it's my job to set them straight. I'm sure they love being told they're wrong because they're young and vain. I'm aware styleguides recommend semis but I'm more referring to people who have strong opinions about what is a minor semantic. For my part it's simple: the effort involved using semicolons correctly (and in my experience many devs who like semis use them inconsistently)…
> I'm sure they love being told they're wrong because they're young and vain. I wouldn't say that, but it is a decision based on vanity and it does tend to come from younger/junior coders in my experience. Making one bad decision based on vanity doesn't make you vain though IMO. It's not personal vanity either, it's "code vanity" so maybe a better word would be: hasty or risky... Anyway, the effort that it takes to s…
You're not wrong, but using it as an argument has a counter: people like semis because they're the old-guard curmudgeons afraid of change :)
> then you risk running into a bug like the one that broke Twitter Bootstrap due to lack of semis.
The bug was due to a JS minifier removing a needed semi, which they did to save space. I still don't know of an example of somebody actually shipping code that broke due to a missing semi in source.
> It's also not a minor semantic. The language requires them and if you make the error of omitting them
That's not true at all. ASI is intentionally a part of the language spec, hence why there's not a single runtime that allows you to turn it off.
> The people who wrote these guides really cared about semicolons, that's why they put it in the rules.
The people who wrote those guides were writing them for hundreds of developers with varying levels of experience and a wide variance in tech stacks. I probably would have put it in the spec too, but that doesn't mean I don't set `semi: false` in every React project I start.