11 ms·
Brendan Eich: The infernal semicolon
- overshard 15y agoThis whole argument is silly (the semicolon in JS argument, not this post itself). JavaScript was built for semicolon use, use them. If you don't want to use semicolons then at least make sure you aren't breaking major libraries that are in mass use. If it does break something many people use then work with the creator to fix it. If he refuses to fix it then ignore everyone using these libraries, provide a weird workaround, or just use a semicolon! Personally I've created a branch off of bootstrap and another library (can't remember off top of my head) that adds in semicolons as to not break JSMin which is critical to my deployments of websites.
- tomjen3 15y agoJsmin is broken if it can't handle missing semicolons. Googles closure tools actually do parse the Javascript and can properly minify them better.
- drawkbox 15y agoThe eternal fires of high level bike shedding: semi-colons, whitespace and curly braces, no programming discussion or language can escape all three. Be liberal in what you accept and conservative in what you send (i.e. just use semi-colons) and your systems will work and play well with others being interoperable.
- akkartik 15y agoBut Postel's law doesn't really help: http://www.joelonsoftware.com/items/2008/03/17.html http://www.joelonsoftware.com/items/2008/03/17.html
- VMG 15y agowell JavaScript was liberal in accepting statements separated by newlines and look what happened
- drawkbox 15y agoTrue, but that is a language, products and implementations sometimes need to be more this way since they are the front lines/interface/api. Even falling back on broken standards or less clean implementations (url over version header on restful apis for one example) to work in some cases where absolutely needed but make it harder to do so. Shipped software should try to work or be usable in the shipped format, get funky behind the product layer if you want. Here the minified javascript that everyone uses, should just work with the current infrastructure. If it doesn't, provide the tool that fixes the problems for people to ship with.
- mnutt 15y agoBut it seems that the entire incident was caused by jsmin being overly conservative in what it accepted, thus breaking bootstrap's js. While I use semicolons myself, it seems like jsmin should conform to the js spec as long as the spec allows the behavior.
- benblodgett 15y agoI can't believe that I woke up this morning and more semicolon arguments are at the top of hackernews..
- nikcub 15y agoYou haven't been around for long enough or just aren'y cynical enough, I went to bed knowing that there would be at least 5 more posts about it in the morning
- groovy2shoes 15y agoYeah, standard procedure is 1. "X is stupid" post 2. "But I like X" posts
- RegEx 15y ago3. "X is stupid, but I like it anyway!"
- ceol 15y agoIf it's anything like the "Email is dead" one, we've got some more coming. We need at least two arguing for Crockford (one with a liberal amount of "hipster" name-calling), someone arguing for @fat, and @fat eventually explaining himself (because it would be a cold day at Oracle before Doug Crockford said anything other than "you suck").
- drivebyacct2 15y agoIf I see people errantly slinging around "hipster" for any reason, let alone to pettily attack people who prefer a certain style, well, I'm going to be even more annoyed than seeing more god damned "email is(n't) (the best thing/going to screw your mother)" posts.
- Terretta 15y agoAnd you thought one of those five would be from JS's inventor, participating in this thread? I didn't expect that -- makes me feel HN is still a great resource.
- raganwald 15y agoThe key point: "ASI [“Automatic Semicolon Insertion”] is an error correction procedure. If you start to code as if it were a universal significant-newline rule, you will get into trouble."—Brendan Eich
- enneff 15y agoThat doesn't sound right to me. Making s language's syntax more liberal is not a sane way to correct errors. If anything, JavaScript's ASI is an error creation mechanism.
- magicalist 15y agoYes, he's acknowledged many times (including in this post) that it was a mistake. That was the intention, but it wasn't successful. It should have gone all in (no semicolons at all) or not have existed.
- tolmasky 15y agoI think it was a reasonable decision at the time. The idea was that JavaScript was not going to be some language people wrote hundreds of thousands of lines of code in, and that the target audience would become frustrated if the language didn't "help" them. For example, if JavaScript truly were to always require semicolons at the end of lines then that means this would be a syntax error: <a onClick = "someFunc()" > ... > Error, missing semicolon after ")" on line 1 That could get pretty annoying pretty fast, and simple little event handlers like these were probably the main use of JavaScript for the first x years. It also matched the existing philosophy of HTML which was to really go out of its way to make sense of whatever was in the file. Now of course, the constraints are different: people want to use JavaScript for huge codebases and serious projects, so it makes sense that these decisions are no longer appropriate.
- masklinn 15y ago> For example, if JavaScript truly were to always require semicolons at the end of lines then that means this would be a syntax error: Depends whether the semicolon is defined as a terminator or as a separator. This would be valid code with statement separator semicolons.
- carsongross 15y agoMoar downvotes! Can we reconsider using this thing as the assembler for the web? Can we come up with a sane, cross-platform bytecode standard?
- chc 15y agoFeel free to come up with a sane, cross-platform bytecode standard if it sounds doable to you. It doesn't to me.
- ssp 15y agoWhy not?
- BrendanEich 15y agohttp://news.ycombinator.com/item?id=1893686 http://news.ycombinator.com/item?id=1893686 Mentioned in http://www.aminutewithbrendan.com/pages/20101122 http://www.aminutewithbrendan.com/pages/20101122. JS is becoming the compiler target "bytecode", without needing verifiers or new and complex standards to be adopted by multiple browser vendors. JS has gaps as a target language, for sure. We are working on filling them (e.g. 64-bit and other int types). These are easy bugs to fix compared to creating a new, portable, and future-friendly bytecode standard in addition to keeping up with a competitive JS engine. Since JS is incumbent, it's hard for any browser vendor to justify a new thing with zero users at first, and too much risk of non-standardization or JVML-like albatross status.
- deleted 15y ago[deleted]
- JeremyBanks 15y agoIndeed, it is a fatal flaw when a language is unable to handle syntax that doesn't correspond to its defined grammar.
- petercooper 15y ago
- gcb 15y agoif ; (x3B?) and \n (x0A) are the same size, why use ; in minified JS if \n has more "features"
- deleted 15y ago[deleted]
- dave1010uk 15y agoSome reasons: \n can easily get turned into \r\n Whitespace is harder to see when debugging (especially with word wrap) Convention
- gcb 15y agoYou're not supposed to touch your minified js after you generated it
- dave1010uk 15y agoAnd most people won't. Using semicolons means things are less likely to break when other people use your code.
- philh 15y agoOn the input side, I would guess the problem is that the minifier in question uses a parse tree with a not-quite-perfect grammar. It sees clearMenus() !isActive && $parent.toggleClass('open') and parses it the same way it would have done clearMenus() !isActive && $parent.toggleClass('open') On the output side, using ; is simpler to program (A\nB only means A;B in some cases, depending on B). And you'll receive fewer flames for it.
- edwinnathaniel 15y agoIf a major framework breaks a major toolchain and they both disagree on a fundamental principal, then they both don't promote pragmatism or "Get Things Done".
- orangecat 15y agoI wish I had made newlines more significant in JS back in those ten days in May, 1995. I wish Brendan was not as skilled a hacker as he was. It's an amazing achievement that he produced Javascript in such a short time. Unfortunately it ended up having the sorts of flaws that are inevitable with such a timeframe, yet it was "good enough" so that everyone decided it was easier to live with the flaws than break compatibility and fix them. If the initial version of Javscript had been worse, we might be using a much better version today.
- moonboots 15y agoIf javascript were any worse, it may have been sidelined entirely before it had a chance to improve, and we would be using proprietary application stacks like iOS even more heavily for client side development.
- wickedchicken 15y ago> If javascript were any worse, it may have been sidelined entirely before it had a chance to improve And we should keep using it in 2012, 17 years later?
- BrendanEich 15y agoJS has improved a lot in 17 years. It's still incumbent, and with ES6 and further Harmony work, it is still improving (much of ES6 are in V8 and SpiderMonkey, or coming very soon). Good luck displacing JS. I mean that sincerely, especially if you work in the open. Dart had a rough start but even ignoring that, I don't think it will succeed. We'll see.
- gruseom 15y agoThis looks like a good place to fulfill a promise I made to report back if my hopes were dashed about Dart (http://news.ycombinator.com/item?id=2989686 http://news.ycombinator.com/item?id=2989686). They were dashed pretty quickly. I've seen nothing compelling enough to prefer Dart to a fixed-as-much-as-is-practical JS. Especially if such a JS becomes more suitable as a compilation target (better numerics and so on).
- deleted 15y ago[deleted]
- ronaldj 15y agoJust because you can doesn't mean you should. A majority of the JS code out has semicolons. Key individuals on the committee responsible for furthering the development of the language recommend always using it. Not including semicolons has shown to result in code errors. It can also introduce build errors when your code is minified. It can also create mental overhead when other developers try to read your code. I just don't get it. Why fight it? Then again, I'm coming from a C/Java background and they just seem so natural to me.
- skymt 15y agoI suspect it is a matter of background, as you imply. Twitter has its roots in Rails, and I believe they also use Scala heavily. Both Ruby and Scala use newline as a statement terminator. GitHub is another Rails shop that avoids semicolons in JS. Zepto.js recently removed the semicolons from its code base, and its author was a core Rails developer.
- cletus 15y agoHeh, I was waiting for this post. Coming in the next 24 hours: - "Why I use minimal semi-colons in Javascript" - "ASI is broken but I like it" Honestly I'm shocked at the defense of this practice (of ASI "abusage"). It speaks loudly to Jacob's (fat@githib) ranking of ego-stroking and showboating over creating readable code, particularly for a library "Designed for everyone, everywhere" [1]. I'm confounded that such an attitude can survive in a large engineering organization like Twitter. Google's style guide [2] specifically prohibits this ("Always use semicolons.") and yes, this is the same standard we use internally for writing Javascript. Just because you can doesn't mean you should. FFS, just get over yourselves and your hipster bullshit and use semi-colons. [1]: http://twitter.github.com/bootstrap/ http://twitter.github.com/bootstrap/ [2]: http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Semicolons http://google-styleguide.googlecode.com/svn/trunk/javascript...
- dlitz 15y ago> I'm confounded that such an attitude can survive in a large engineering organization like Twitter. I'm not surprised. A bit disappointed, but not surprised. Have a look at the original OAuth specification sometime. Note how they misuse the word "key" to mean "identifier". I can't imagine that anyone writing a crypto specification wouldn't know that this would be confusing; I think it's more likely that the person just didn't care. Also, there's the fact that the whole thing could have been written as just a way of getting Basic authentication credentials, rather than making up a whole new authentication scheme. And then there's the hashbang thing...
- Terretta 15y ago> then there's the hashbang thing Thank you. I agree both of these show a casual disregard for "how the real world works", where not everyone is a "rock star", some are lowest common denominators using the web as best they can (and good for them for even trying!), and some just need to get things done. Every extra exception you require in a user's mind makes your work and the web in general just that little bit less accessible. That it is being done for "style" in an arena of notoriously unsophisticated users, really feels like a giant middle finger. Kudos to Google for their style guide freeing up this particular "exceptions" pigeonhole from a JS programmer's mind, so she can use the synapse to get something done instead. // Using JS in the real world since Netscape 2.0.
- ender7 15y agoIs it me, or did the author tack on a new topic regarding the use of && and ||? The fact that they return the controlling operand is quite useful. Consider a situation where you want to check the property of an object, but don't know if that object is null: var foo = obj && obj.bar; I find this much more readable than: var foo = null; if (obj) { foo = obj.bar; } You can accomplish the same thing with a ternary operator, but it's longer and, to my eyes, less scannable: var foo = obj ? obj.bar : null; Also useful when setting optional parameters: function myFunc(foo, bar) { foo = foo || {}; bar = bar || 'default_value'; } (take care however: the empty string evaluates to false, as does [] and 0)
- BrendanEich 15y agoIt's just you ;-). I love && and || being both short-circuiting (as in C) and value-preserving (as in LISPs, Perl, etc.). That's not the issue. The issue is abusing && or || as the whole of an expression statement, instead of using if.
- jayferd 15y agoI can't speak for Brendan, but I generally think && / || are great for assignment, but otherwise it's code-smell. var foo = obj && obj.bar; // great !isActive && $parent.toggleClass('open'); // smelly if (!isActive) $parent.toggleClass('open'); // better
- RegEx 15y agoI believe HTML5 boilerplate implements the smelly pattern. https://github.com/h5bp/html5-boilerplate/blob/master/index.html#L44 https://github.com/h5bp/html5-boilerplate/blob/master/index....
- astrec 15y agoif (!isActive) { $parent.toggleClass('open'); } // best Damian Conway of Perl fame wrote an excellent style guide for C in the early/mid 90s which I'm pretty sure explained why (I can't find my copy): Programmer A: if (!isActive) $parent.toggleClass('open'); Programmer B: if (!isActive) $parent.toggleClass('open'); doSomethingElse(); Oh dear.
- pbreit 15y agoI suppose it's too late for Fat to do the right thing and insert the semi-colon since he's ridiculed anyone making such a request?
- prodigal_erik 15y ago> edit: The code had already been changed to an if when i suggested the jsmin issue be filed as a bug. They worked around the bug some time before Crockford refused to fix it.
- thedeuce 15y agoAnother sunday wasted by nerds in dungeons circle jerking themselves over stupid shit. LETS ALL BLOG ABOUT IT!
- robwgibbons 15y agoJust use a fucking semicolon. Seriously, it's like people are embarrassed to write JavaScript. If you ever work with other people this shit doesn't fly very far.
- thezilch 15y agoFor what it's worth, both libraries in question realigned their egos, despite trolling from the likes of madrobby. https://github.com/twitter/bootstrap/commit/575f18aaf49abb0289185f6409bee031947ccf69#L7R57 https://github.com/twitter/bootstrap/commit/575f18aaf49abb02... https://github.com/douglascrockford/JSMin/commit/5ca277ea452beae1c25db3bc0ef5c81309a3daf4#jsmin.c https://github.com/douglascrockford/JSMin/commit/5ca277ea452...
- EvilTerran 15y agoSo the fuse has burned out, but the fire's already burning. Ah well. At least some good has come of this, even if it did come with the baggage of a massive Internet Argument.
- ricardobeat 15y agoWhat still surprises me the most is the amount of hostility in here. I don't have enough fingers to count names called in both threads. Why is this subject harboring so much emotion?
- sjwright 15y agoThe purpose of the semicolon in javascript is to eliminate ambiguity. Choosing not to avail yourself is like driving without a seatbelt -- of course it's possible, but I always thought the job of a programmer was to be as unambiguous as possible. You're not the only person reading your code. Your code isn't just being parsed by five wholly different javascript engines, it's also being parsed by the brains of every programmer who chooses to read your code. Expecting others to learn a few stupid rules and lame syntax hacks in order to understand and match your style seems moronic. And defending the decision by noting that "the spec permits it" is doubly so.
- ern 15y agoWhen I program in C-syntax languages, I always surround loop and conditional blocks with braces, even if they have a single line of code. Even though these languages allow braces to be omitted, I consider it bad style, and error prone (although I have never seen a real bug where a programmer made an error of including multiple statements without braces). The languages do allow this style, and however distasteful I personally find it, I would expect compilers to handle it properly. I think the same should apply here, the minifier should handle the language as specified, if it can't then it is has a bug.
- wyuenho 15y agoIn case anyone needs some context, sometime ago I posted about the problems with leaving out semicolons had caused for users of the Bootstrap project. http://news.ycombinator.com/item?id=3584635 http://news.ycombinator.com/item?id=3584635 https://github.com/twitter/bootstrap/issues/search?utf8=✓&q=semicolon https://github.com/twitter/bootstrap/issues/search?utf8=✓...
- chad_walters 15y agoIt looks like the right thing might have happened here (at least in the small). Crockford is making a change in JSMin: https://github.com/douglascrockford/JSMin/commit/5ca277ea452beae1c25db3bc0ef5c81309a3daf4 https://github.com/douglascrockford/JSMin/commit/5ca277ea452... Jacob changed the code to be compatible with existing JSMin: https://github.com/twitter/bootstrap/issues/3057#issuecomment-5140916 https://github.com/twitter/bootstrap/issues/3057#issuecommen... There, now don't we all feel better?
- VolatileVoid 15y agoThis entire debate is sad to me. It's sad - as a JS outsider - because it truly smacks of religious dogma. People have these debates about The One True Brace Style as well, but it all amounts to masturbation. The amount of hair-pulling and whining over 1 byte really turns me off from the JS community as a whole. I respect BE's weighing in on this, and I respect people for disregarding his advice. The bottom line is: do whatever the hell you want and don't let anyone shove their opinions down your throat. Why can't we be happy with that? The little JS I've written has been filled with semicolons. Yours may or may not be. If I work on your project and you don't use semicolons, I'll adapt. If you work on mine, you'll adapt. But I won't try to convince you to use my style on your project or vice versa. Can't we move on from this pointless debate? Nobody will win, but in the end everyone will lose.
- jyap 15y agoGuys, more background from 'Fat' on why he doesn't use semicolons written back in October 31st 2011: http://wordsbyf.at/2011/10/31/i-dont-write-javascript/ http://wordsbyf.at/2011/10/31/i-dont-write-javascript/ Quotes: On the use of '&&' instead of an 'if' statement: "If you were really having fun with it you could lose the if all together... Each is perfectly valid. Each behaves the same. It’s just a matter of preference and finding the style that makes most sense to you." JSLint is described as a "unnecessarily strict linter". "The majority of lines however don’t end with semicolons because they simply aren’t necessary and I prefer the minimalist aesthetic. For me, \n character is enough and the semicolon character is redundant." ... IMHO: Don't be a JavaScript hipster. Add semi-colons.
- cabalamat 15y agoLanguages should either (1) treat newline as a statement delimiter (as in Python) or (2) not do so and instead treat some other character (e.g. ";") as a statement delimiter (as in C etc). JavaScript falls between two stools and that's bound to cause problems.