6 ms·
Can You Find the Bug in This Code?
- kowdermeister 8y agoWhen you assume the compiler reads it the same way your brain does :)
- gammateam 8y agoI 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 8y agoDitto. 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.
- adrianhel 8y agoAlways prepend ( and [ with ; I also prefer void function () {} for this. Void the result of this function expression which I immediately invoke.
- adrianhel 8y agoCorrection: void function () {}()
- ezekg 8y agoI'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.
- Klathmon 8y agoMore correctly it's: Always prepend any line that begins with ( and [ and ` with ;
- adrianhel 8y agoAlways for ( and [ when they start a statement. It really is that simple.
- adrianhel 8y agoAnd for template strings: ` I do agree that good use cases are few and far between.
- dvlsg 8y agoI honestly can't remember a recent time I had to start a line with a ( or a [. 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.
- marshall313 8y agoI agree. Those are the only two cases where a semicolon is needed. Other than that, I like my code to look clean :)
- Legogris 8y agoSemicolons 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 8y ago> 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.
- Legogris 8y agoYou misinterpreted my statement - it will or will not make a difference depending on the line before. The spec is not ambiguous but for someone who does not know this there is probability at play.
- scandox 8y agoThe real bug is the failure to use semi-colons. I literally cannot comprehend the non-use of semi-colons in JS.
- adrianhel 8y agoIf so, the failure lies with the spec. (personally, I like ASI)
- umvi 8y agoCue Principle Skinner meme: "Is the language specification too loose?" "No, it's the developers who are wrong."
- nobleach 8y agoI 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 was a question... but why avoid the actual punctuation that assures that I communicate the intention 100%? The explanations that have been given to me typically fall in line with, "well, to new comers, it won't make sense why you don't add a semicolon after a function declaration or if statement. I consider that a teaching moment. But, I'll admit, this debate has little merit. Folks whom are dead set against using them are rarely convinced until they get bitten hard by a 2 hour hunt for an obscure bug. And folks that use them will probably end up with RSI or Emacs Pinky™
- deleted 8y ago[deleted]
- ng12 8y ago> Folks whom are dead set against using them are rarely convinced until they get bitten hard by a 2 hour hunt for an obscure bug. Honestly -- then they'll never be convinced. I haven't hit an ASI bug in the last decade.
- notafrog 8y ago;)
- tastyfreeze 8y agoWhat kind of heathen omits semicolons in JavaScript Thats like neglecting punctuation in English You can but the punctuation makes it much easier to read
- programmarchy 8y agoLot's of people! https://standardjs.com https://standardjs.com A better analogy might be the Oxford comma.
- ezekg 8y agoI 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.
- ezekg 8y agoRelevant: https://standardjs.com/rules.html#semicolons https://standardjs.com/rules.html#semicolons (hint: no semi-colons!)
- joshuamorton 8y agoWhich 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 `;`.
- Klathmon 8y ago>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.
- tniemi 8y agoDon't use "gonad" javascript, or just use semicolons. With dangling balls: `(function(){})()` No dangling balls: `(function(){}())` https://gist.github.com/newswim/4668aef8a1f1bc0dabe8 https://gist.github.com/newswim/4668aef8a1f1bc0dabe8
- Etheryte 8y agoAn even more common use-case that fails without semicolons by T.J. Crowder (2015): http://blog.niftysnippets.org/2015/09/automatic-semicolon-insertion.html 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 8y ago> 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.
- ng12 8y agoI 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.
- wayneftw 8y agoThe 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 easily the past 15 years. 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.
- jbredeche 8y agoReminds me of https://github.com/twbs/bootstrap/issues/3057 https://github.com/twbs/bootstrap/issues/3057
- jyriand 8y ago“Must be a semicolon issue,” was my first thought, without actually knowing why.
- mrspeaker 8y agoOver 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 8y ago;['but', 'why'] .reduce((msg, word) => msg + word, '' ).join(' ') +'?'
- czr 8y agoHappy 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"))()
- slyrus 8y agoIt'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 8y agoAnd perhaps we could repurpose those semicolons for comment syntax ;-)
- mikewhy 8y agoAnother instance where TypeScript has your back.
- telekid 8y agoI 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.
- jakear 8y agoMy 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
- kgwxd 8y agoThe bug is in the coder.
- TeffenEllis 8y agoI 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 8y agoDoes 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.