There's something horribly wrong with everything about this. Why is such a smart person as Crockford wasting his time arguing about semicolons in 2012? Why is this at the top of the most popular hacker website? Why are people writing detailed opinions about semicolons in this thread (with surely more to come?)<p>One would hope that at least over time the bike sheds being argued about would start to at least evolve into arguments about bike garages and eventually bike factories. But I guess the bike shed is forever a bike shed, as long as we're making bikes.<p>Whatever the next disruption is in software engineering, please dear God let it be immune-by-design to this type of distraction.
Jacob is an idiot for writing code with a ridiculous fragile syntax that takes <i>more effort to write</i> in a vain effort to obtain "coolness" via winning a pointless language war that nobody on earth cares about.<p>Crockford is an idiot for writing an JS minifier which explicitly promises to <i>not</i> break legal code but actually does, and then refusing to fix it because he's proud of his reputation for being an asshole.<p>And everyone commenting on this post is an idiot for thinking that any of this is worth the pixels it takes to argue about it. And yes, that includes me.<p><i>sigh</i> This whole thing just makes my head hurt.
I'm new to JavaScript, but from what I can tell, the no-semicolons style<p>- is incompatible with existing tools<p>- is incompatible in places with upcoming versions of JavaScript<p>- is harder to refactor, since you might need to add additional tokens to the beginning of lines depending on the previous lines<p>- is confusing to new JavaScript programmers, since<p><pre><code> + it goes against the recommendations of many of the most popular books
+ it requires adding tokens to the _beginning_ of lines in places, making the entire enterprise more challenging to learn and of dubious utility
+ it discards one of the attractive qualities of JavaScript for programmers of other languages: familiar syntax
</code></pre>
And further, the use of constructs like<p><pre><code> !somethingHappened && otherwiseDo(x)
</code></pre>
instead of<p><pre><code> if (!somethingHappened) {
otherwiseDo(x)
}
</code></pre>
is bad practice in any language.<p>"Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?"
- Brian Kernighan, "The Elements of Programming Style"<p>Although everyone has the right to maintain their projects with whichever style they prefer, given the above, I wonder why the chosen style was chosen.
Frankly this is less about semicolons and more about two people who both have strong opinions and feel that they need to act in a certain way.<p>It doesn't take a lot more effort to rephrase fat's response to be more cordial.<p><pre><code> "Hi @englishextra, those two lines are valid JavaScript. We try not to use semicolons whey they aren't necessary. See (insert link to the wiki).
"JSMin doesn't perform well on bits of JS like this. Maybe you can discuss it with @douglascrockford ?"
</code></pre>
Flamewar avoided (probably) and more stuff getting done.
Learning the minutiae of a language is important, sure, but I can't fathom why you'd willingly introduce unnecessary fragility or overthink into your codebase.<p>Ideally, your goal as a hacker is to make cool shit. Your goal is not syntax.
I think older programmers will side with Crockford and for good reason.<p>The bootstrap code makes assumptions about the future of the js spec and browser implementations that aren't safe. Young people don't remember the dark days of people writing js to leverage IE/Netscape specific bugs which then had to be turned into features because so much code existed in the wild that could be broken.<p>Crockford is a scarred veteran of the spec wars. I'm sure that when he sees young people trying to game spec/implementation ambiguities, he thinks, "Never again! Not on my watch!"<p>I think best practice is explicit, future-proofed code that doesn't tie the hands of spec authors or browser vendors. I know that Crockford's tool doesn't accept the current spec or browser behaviour, but his choice is informed by past experience and concern for the future. It's not just some random bug that he's too stupid or stubborn to fix.
I am not experienced with javascript, so I don't understand this argument. Can someone please confirm the following, or explain what's going on?<p>Here is what I understand:<p>1. Javascript will add a phantom semicolon at the end of each line if it seems like you need it, for some definition of seems like. To me, it sounds like this is so that new HTML writers in '98 wouldn't see their pages blow up when they forgot a semicolon at the end of a script. It doesn't sound like professional programmers are expected to leave semicolons out on purpose (this is the opposite from python). The bootstrap authors took advantage of this because somehow screwing with the compiler is in vogue.<p>2. Crockford's tool broke when a semicolon was left out somewhere. I guess he didn't implement a proper js compiler that covers the entire language spec.<p>3. There's some business about unary and binary ! operators? Something about sticking a ! in front of your line lets you leave the semicolon off the end? This sounds utterly braindead to me. It's no fewer characters, and the thing you're typing is going really far away (at the beginning of a big block of code) from where it's actually meaningful to you (at the end). Also, it looks like it relies on some fairly convoluted unintended consequences of the spec, that may not be future-proof.<p>Here is my analysis, if the above understanding is true:<p>1. Bootstrap needs to fix their code. This business of the compiler sticking in semicolons where it thought you meant them is a crutch to prevent stupid errors from being fatal errors in the average case, but that doesn't mean they aren't errors. Be explicit.<p>2. Crockford should fix his tool soon, not complain about other people's code, and say "I'll fix it soon, but for now, here's a workaround: just put a semicolon in your code".<p>3. What the hell. Are people really doing this? Is the web one big obfuscated javascript competition? What happened to writing simple, explicit, readable, maintainable code? Are we really bickering over single characters and trying to play subtle tricks with language esoterica to save them like a bunch of 14-year old perl newbies? Man, I hope these morons aren't in charge of my bank account.<p>Overall, I'm with Crockford, but he doesn't need to be this much of a jerk about it.<p>Further analysis, now that I've read it with a decent (non-cell) data connection: allowing gifs in a public issue tracker is a terrible decision.
I wonder if the "insanely stupid" comment isn't about the omission of the semicolon but about the omission of "if". Since when is a good old-fashioned if-statement out of style?
Update: Bootstrap fixed their code as Crockford prefers. From <i>fat</i>'s comment circa 11 am PDT:<p><i>fwiw, this was patched in bootstrap way before i even encountered this issue - otherwise i wouldn't have closed it outright.<p>Sorry for the confusion everyone.<p>You can see the code here in 2.0.3: <a href="https://github.com/twitter/bootstrap/blob/2.0.3-wip/js/bootstrap-dropdown.js#L63" rel="nofollow">https://github.com/twitter/bootstrap/blob/2.0.3-wip/js/boots...</a><p>I still maintain this is a bug in jsmin, but as others have pointed out - mark and I do our best to make bootstrap flexible with other great tools out there (like jsmin). This includes adding semicolons where necessary or changing minor stylistic things.</i>
I happen to agree with Crockford on the omission of semicolons. I also have a strong opinion on clever code: you should always optimize for readability over aesthetics.<p>But... Crockford could have said it in a much more tactful way that could elicit a more positive response, spare the world from this sad discussion, as well as channel the man-hours dedicated to this into more useful endeavors.
While this "argument" has prompted several lol's on my part, JS really needs some equivalent of PEP8<p>And if you don't use semicolons you are an asshole.
> <i>! is not intended to be a statement separator. ; is.</i><p>Which is why there is a line break there. He's far too stubborn to realize that JSMin is at fault for not being able to correctly parse JavaScript.
“…Explicit is better than implicit…
Readability counts…
In the face of ambiguity, refuse the temptation to guess.
There should be one—and preferably only one—obvious way to do it…”
— The Zen of Python<p>Ironically, the Zen of Python supports using semicolons in JavaScript. Also, both sides come off like children, but at least Crockford's opinion seems a little less selfish. It's like politics, with the difference being in their premises so they'll never agree.<p>Also, why can't I (or anyone else) resist commenting on this rather pointless debate? I am disappointed in my weakness.
It is hard for me to understand how one could make so many things in a programming language completely arbitrary - I mean it is natural for a language designer to have some vision of how the language will be used by other people, how will the common structure of the programs look like, but JavaScript looks like such a vision never existed, there are often several ways of doing the most basic things (writing vs. omitting semicolons mentioned here, writing vs. omitting "var", but above all else 1000 of ways of doing OO, all with it's own problems) and no easy ways at all of doing other very basic things (modules/namespacing). As a result you can find 10 or 20 programs written in JavaScript where each looks like it was written in a quite different language. Maybe it was an attempt to make an "easy" language for non-programmers (I doubt even this goal was in the end achieved), but as a primary language for programming client-side applications it is a utter disappointment. Not that I necessarily blame the JavaScript creator, I am sure there were plenty of corporate pressures involved and other factors of corporate/economic nature.<p>I wish we could get some other programming language implemented in all the major browsers, then at least in the perspective of the next 10 years we could completely get rid of JavaScript, I am not sure if it possible to really improve the situation in a radical way just via incremental changes to the existing design.
For a good explanation of why it's a good idea to always use semicolons in your JavaScript, take a look at: <a href="http://lucumr.pocoo.org/2011/2/6/automatic-semicolon-insertion/" rel="nofollow">http://lucumr.pocoo.org/2011/2/6/automatic-semicolon-inserti...</a>
Example when a semicolon is <i>required</i> in non-minimized javascript.<p><pre><code> alert(1) // add semi-colon here to make this code work
(function(){alert(2)})()</code></pre>
Excellent - another internet cock-fight, with both sides managing to act like total cocks..<p>What would be better is for both sides to fix their code:
Crockford to fix minify for the edge cases like this (result: better, more robust code equals a win), and the twitter boys to add a semicolon for these edge cases (result: better, more robust code equals a win).<p>But instead, as usual on the internet, it's just devolving into lots of "waah waah waah".
What is insanely stupid is abusing the logical bitwise && operator rather than signifying their intent using a plain old if statement like the language intended.
For those that agree with leaving semi-colons out of JavaScript that they write for projects, can you explain what the benefit of doing so actually is?<p>Seems like people are valuing aesthetics over producing code that works for 100% of the users/situations.
Why is this discussion about semi-colon insertion and not about the use of short-circuiting as a replacement for specific 'if' statements and general inconsistency in the project?<p><a href="https://github.com/twitter/bootstrap/blob/master/js/bootstrap-carousel.js#L91" rel="nofollow">https://github.com/twitter/bootstrap/blob/master/js/bootstra...</a><p><a href="https://github.com/twitter/bootstrap/blob/master/js/bootstrap-modal.js#L61" rel="nofollow">https://github.com/twitter/bootstrap/blob/master/js/bootstra...</a><p>(Incidentally, would lines 54/56 of bootstrap-modal suffer from the same problem? Or does the fact that the last function call on line 54 is already in an && expression change things)<p>I'm kind of surprised that the bug wasn't fixed by changing the line to `isActive || $parent.toggleClass('open')` as is done here <a href="https://github.com/twitter/bootstrap/blob/master/js/bootstrap-alert.js#L52" rel="nofollow">https://github.com/twitter/bootstrap/blob/master/js/bootstra...</a>
The use of semicolon as best practice was sorted out years ago in the js community. Crockford has all his rights to tell these guys to sharpen up because that "fat" character is obviously a python smuch with no knowledge of JS history or respect for elders.
My comment from the thread, buried under many pages of meme gifs:<p>>>><p>@douglascrockford If TC-39 makes ! an infix operator, which causes \n to not terminate the statement, then that will be a syntax change that is incompatible with current code in the wild, which would be an insanely stupid move on their part.<p>The fact of the matter is that JSMin isn't parsing JavaScript correctly. I thought the separation of concerns was that JSLint tells you about stupid code, and JSMin correctly parses JavaScript according to the ES standard and minifies it safely.<p>>>>
Am I missing something? Aren't Bootstrap and JSMin open source? Instead of focusing on coding philosophies, someone can simply fork JSMin's source and make the appropriate patch, or make a fork of Bootstrap that has JSMin friendly code.<p>Regardless of our many diverse opinions, Crockford and Fat are entitled to maintain their own libraries in whatever fashion they desire. The fact that they made the libraries open source means "if you don't like my code or the way it works, here's my code, customize it and leave me alone".
Should JSMin being enforcing the spec or good practice?<p>My vote: Good practice<p>Upvote replies to vote. Please don't down vote the replies so we get a clear picture.
"Be liberal in what you accept, and conservative in what you send." <a href="http://en.wikipedia.org/wiki/Robustness_principle" rel="nofollow">http://en.wikipedia.org/wiki/Robustness_principle</a><p>Its like the golden rule for software. It generally stops nonsense like this cold.
Real question is... why not just use UglifyJS or any one of the other js minifiers? So what Crockford is opinionated about JS, we all know this. Don't use his products if you don't like his opinions.
The obvious thing to do is:<p>1) JSmin should be modified to handle Bootstrap-style semi-colon-ing<p>2) Bootstrap should adopt the more commonly understood semi-colon-ing
you don't have to use linebreaks either but people use it. There are lots of good practices making code easy to use, read and maintain. So just put f.. semicolon.
That's really stupid. There's no plausible reason that the end of that method shouldn't have a semi-colon. I honestly wouldn't advise anyone use Twitter Bootstrap. You should always use semicolons in Javascript even if you're hippie. Hippies use coffeescript.<p>And I think Google's style guide[1] is prevail over Github's style guide[2].<p>[1] <a href="http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Semicolons" rel="nofollow">http://google-styleguide.googlecode.com/svn/trunk/javascript...</a>
[2] <a href="https://github.com/styleguide/javascript" rel="nofollow">https://github.com/styleguide/javascript</a>
Crockford's stance is annoying purely because it's pedantic and because there are <i>many</i> libraries that embrace this style. We use django_compressor and found bugs from the compressor that uses Jsmin. So I have to use something else to compress them before hand.<p>Are there compressors that use the syntax tree rather than transforming the source?
What a child... Has anyone read the GitHub style guide? 'Do your best to never use a semicolon. This means avoiding them at line breaks and avoiding multi-statement lines.'<p>Grow up Crockford. Do research before you post. Stop assuming because you supposedly once knew how to do something, you still do, and always will. Times change.<p>However, this is all very exciting. It gives Twitter, or whatever other start up, an opportunity to replace an outdated piece of software by a nut case!