9 ms·
The infernal semicolon (2012)
- serve_yay 12y agoI see these warnings a lot, but I work on a team that does not ever use semicolons, and hasn't for years. It is simply not a problem, I don't know what else to say. And I have come to really dislike the syntactic noise the semicolons add.
- BrendanEich 12y agoDo you start files with a leading ; just in case (of concatenation, where the file otherwise would start with one of `([+-/`)? Do you use ; at end of var declarations? If not, beware; if so, do you use comma-first? Thanks for answers, just curious.
- fakhruddin 12y agoI prefer to use it like this -> var a = 2 ,b = "tan" ,c = "gent" ; Keeps the var block distinctly visible in a function and in case you add a new variable there's only one line diff.
- anon4 12y agoI like putting a semicolon on every third and fifth line, but omitting it on every fifteenth line and on leap years.
- serve_yay 12y agoWe don't use semicolons at all. We do use comma-first! About the concatenation, we use Browserify which bundles our code for us. The beginning of a Browserify bundle is a definition for the `require` function.
- mavdi 12y agodon't use a leading ; always var foo = 'bar'; If you do comma first, I will automatically assume you're an idiot.
- b_hell_t 12y agoI use var foo = 'bar'; but at my place of work they insist on no semi-colons and comma first. I want to tell them this is stupid but I need solid facts. anyone care to expand.
- BrendanEich 12y agoIf you happen to write var foo = 'bar', baz = 'bletch' (function (global) { //module pattern code here ... })(this); then you have trouble. Semicolons are not optional (meaning always optional, you can leave them out at will). Please read my blog post.
- jedschmidt 12y agoI wish `void` would get more love for IIFEs instead of just sitting there in a dark corner of JS, unloved. var foo = 'bar' var baz = 'bletch' void function (global) { //module pattern code here ... })(this)
- Havvy 12y agoAlmost perfect, but you left an unmatched close parenthesis before the call operator.
- mavdi 12y agoThis is a good example! It will try to run 'bletch' as a function. awesome.
- ttty 12y agoWho still writes this in 2015? Now is: module.exports = function(){};
- joliss 12y agoI use a semicolon-free style (only protecting leading ([+-/) with var declarations like so: var foo = 1 var bar = 2 Is there an issue with semicolon-free var declarations that I should be aware of?
- samatman 12y agoI never tire of comparing Lua with Javascript, and this is another one of those cases. A semicolon in Lua is simply a character which is illegal to place anywhere but the end of a statement. There is no insertion stage, the language parses just fine on a single line, or any whitespace you choose. Semicolons are vanishingly uncommon in Lua code, but if you want to compress a couple statements onto a line, they signal your intention. Lua is like what Javascript would be if it were: based on Pascal, written by resource-constrained Brazilians, and capable of making backwards-incompatible changes. Though with the de-facto fork between Lua and LuaJIT, the latter has become much more difficult, as it should at some point in the maturing of a language.
- outworlder 12y agoRegarding the resources, we have companies with ridiculously large pockets using Lua, which could contribute more. Think Blizzard. WoW uses Lua heavily. Though I suspect that niche will eventually be taken away from Lua too, which is unfortunate.
- ioddly 12y agoIf anything, Javascript suffered because it had large corporations invested in it. The guys at PUC-Rio were never beholden to corporate sponsors as far as I know, and thus free to break compatibility.
- outworlder 12y agoThat may be true. But without corporate backing, we wouldn't have had so many amazing implementations, which helped with the adoption. Even if Lua (or Lua-JIT) can still beat the crap out of them sometimes. Back then there wasn't even a contest.
- BrendanEich 12y agoWhy does corporate sponsorship have anything to do with it? Any browser that gained non-trivial market share would have felt the same "don't break the web, or you lose market share" pain that the bigs felt. KHTML only started getting adoption via the Safari fork because Dave Hyatt and others implemented real-world quirks (e.g., residual style), prior to webkit.org. Gecko had the benefit of `if (document.all) { IE code here } else { Netscape code here }` patterns from the '90s, but we had to add quirks to gain share with Firefox (m/b, then Phoenix) in 2002 and onward: innerHTML, undetected document.all emulation, and of course XHR. Corporations have nothing to do with it. Any evolving ecosystem with a huge corpus of popular content begets terrific selection pressure against breaking backward compatibility.
- rgrannell1 12y agoNot using semicolons has never be a problem for me. Just like everyone else I put them where they are needed. I don't put them where they aren't needed, because they aren't needed and adding pointless syntax noise is dumb. If I forget to put them in somewhere they are needed, as anyone might do, I add them. No problems.
- IshKebab 12y agoThe problem is that the rules for remembering where they are needed are hard to remember and error-prone.
- serve_yay 12y agoSure, but in practical terms I feel like this never comes up. I just never have to deal with it and I write JS all day.
- BrendanEich 12y agoSee http://inimino.org/~inimino/blog/javascript_semicolons http://inimino.org/~inimino/blog/javascript_semicolons, hope it helps. The minimal semicolon style has only a few rules and many people find it easier to use and remember. I've used it but my C hacker habits go against it. It's a fine style. /be
- aikah 12y agoWow neat,yeah, js devs are going to call anyone out that doesnt use semicolons. Question : If you ever wanted 1 breaking change in the language, and somehow it would be possible (I know it wouldn't) what feature would you change ? Again just 1 feature.
- BrendanEich 12y agoI would undo the implicit conversion madness around == and !=.
- panic 12y agoPreviously: https://news.ycombinator.com/item?id=3844302 https://news.ycombinator.com/item?id=3844302
- tel 12y agoOCaml's approach to semicolons is nice—they are clearly nothing more than sequence markers between side-effecting expressions. a ; b (* a THEN b *) a b (* a APPLIED TO b *) You also can basically just desugar them to let bindings a ; b == let _ = a in b a ; b ; c ; d e == let _ = a in let _ = b in let _ = c in d e Due to static typing it's vanishingly unlikely that a forgotten semicolon will properly check. let this = x ; y (* if x : 'a and y : 'b then this : 'b *) let this = x y (* if x : 'a -> 'b and y : 'a then this : 'b *) So forgetting a semicolon only check if `x` is an unapplied function of type `'a -> 'a` which the value you intended to completely ignore.
- DonPellegrino 12y agoThe desugar'd version should be let () = ... instead of let _ = ...
- tel 12y agoHuh. Certainly true stylistically, although OCaml accepts things like `1 + 1; print_endline "hello";;` with just a warning.
- WorldWideWayne 12y agoA while back I think there was a github issue where the authors of some popular library refused to use semi-colons and it was causing all sorts of problems for people. I can't remember what library, but it was funny watching hundreds of people comment on the issue to say "Just use the damn semi-colons!"
- swang 12y agoBootstrap. The flamewar wasn't over semicolons initially, the minifier used by Bootstrap was wrong but a work around was to add semicolons.
- tedunangst 12y agoThe first link from the article in question?
- WorldWideWayne 12y agoHaha, yeah I guess should have followed that link :)
- slig 12y agoI believe this is what you're talking about https://github.com/twbs/bootstrap/issues/3057 https://github.com/twbs/bootstrap/issues/3057
- dsp1234 12y agoThis has never really struck me as a good argument for using or not using semi-colons. 1.) The non-use of semi-colons was perfectly valid JavaScript in that case. 2.) The parser portion of the minifier from Douglas Crockford did not account for this particular case. 3.) It became a flamewar because Douglas Crockford called it out as an error in a very ham fisted way, and the developer responded in kind. If a c++ compiler chokes on valid input, then a bug is sent to the compiler maintainer, and the developer rewrites their code until the bug is fixed. 4.) And that's exactly what happened. The minifier parser was fixed, and the Bootstrap code was updated to work around the parser error. 5.) The reason for not using semi-colons was legitimate. This is a library with a large audience, and not everyone has a minifier/compresser available. It was an easy win for something that wasn't needed. That's exactly the type of stuff that I want when I lean on 3rd parties. So for me this could have gone a completely different way had the two parties been more civil: BS: Hey DC, can you fix your minifier, it looks like it's busted on this valid input. DC: Hrm, looks like your right. I'll fix it up, why don't you add a semi-colon in there until I can get it fixed BS: Hey, no problemo. Done. I look forward to your fix! So this is really more of a story about being professional to your fellow developers than anything that has to do with semi-colons or not.
- TheAceOfHearts 12y agoEvery time someone brings up semicolons I think it's worth posting Isaac Schlueter's blog post on the topic: http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding http://blog.izs.me/post/2353458699/an-open-letter-to-javascr... You can get by perfectly fine without semicolons. Going further, if you're using a linter like eslint, I think you can even configure it to give you a warning when the missing semicolon could cause problems.
- lbotos 12y agoThe biggest problem I have is the "leaders" forgetting the beginners. The whole semicolon thing took JS from "end a statement with a ;" to: THE RULES: In general, \n ends a statement unless: 1. The statement has an unclosed paren, array literal, or object literal or ends in some other way that is not a valid way to end a statement. (For instance, ending with . or ,.) 2. The line is -- or ++ (in which case it will decrement/increment the next token.) 3. It is a for(), while(), do, if(), or else, and there is no { 4. The next line starts with [, (, +, *, /, -, ,, ., or some other binary operator that can only be found between two tokens in a single expression. You have a junior dev now that's 2 steps behind because he's worrying about self-imposed "beatifying" edge cases instead of getting his code to run.
- ScottBurson 12y agoSomewhere, Alan Perlis is looking down on this and laughing his head off. (For those who don't know the reference: [0], #3.) [0] http://www.cs.yale.edu/homes/perlis-alan/quotes.html http://www.cs.yale.edu/homes/perlis-alan/quotes.html
- implicit 12y agoIs ASI really worth the amount of time that has been spent discussing it? It would be nice if it were deleted from the language entirely.
- pcwalton 12y agoCan't do that. Web sites rely on it.
- ripter 12y agoI wouldn't be upset if those websites failed.
- pcwalton 12y agoUnfortunately, it wouldn't be the Web sites failing; it'd be the browsers that decided to break backwards compatibility as users flee them on account of their favorite Web sites not working.
- implicit 12y agoEven as a pragma, a la "use strict"? "use stricter"?
- BrendanEich 12y agoNo, because: 1. Modes are bad for implementations (this feeling was strong from V8 folks such as Erik Corry, but the new crew in Munich may not feel the same way), due to code branching burdens in testing and optimizing while keeping the complexity down. 2. Worse, what you propose would fork the language to double the testing burden for web devs (apart from engine implentors, item 1), since older browsers would ignore the useless expression statement. This already happened with ES5 "use strict", and created real world bugs (e.g., https://bugzilla.mozilla.org/show_bug.cgi?id=631043 https://bugzilla.mozilla.org/show_bug.cgi?id=631043, there are more like this). Developers do not test all the combinations. Notice how a tower of M modes implemented via these string-expression-statement prologue directives can make a 2^M-fold testing burden.
- cpeterso 12y agoWhich programming language first used a semicolon as a statement delimiter? Did they choose the semicolon because it was easy to type on a QWERTY keyboard? Why not end a statement with a period like a sentence?
- marijn 12y agoThe real surprise here is that people still use JSMin, written by an opinionated grump who apparently does not understand the concept of using a real parser. Several superior alternatives exist. UglifyJS is better in every respect.