Live data from Hacker News

Bootstrap's maintainer hates the semicolon

github.com

111–120 of 137 posts

Re: Bootstrap's maintainer hates the semicolon

#111
post #84

Not having to use semicolons sounds nice, but in practice it can have terrible effects. E.g.: var x = function() { // something } // avoid polluting global scope: (function() { // Some initialization. })() This will call the function x with the anonymous function as its argument, and then call the result of that. There are more examples to that point, and many of them really aren't all that straightforward. So don't…

First commandment of semi-colon-free: any line that starts with [,(,+,- should be prefixed with a semi-colon.

  var x = function() {
    // something
  }
  
  // avoid polluting global scope:
  ;(function() {
    // Some initialization.
  })()

  ;[1,2,3].forEach(...)
In practice those are the only places you'll see (and need) them. It has the extra benefit of disencouraging starting a line with a pre-increment, hacks or weird constructs (not talking of IIFEs of course), and making sure you're never calling a function or accessing properties by accident.

Re: Bootstrap's maintainer hates the semicolon

#112
post #9

He wrote a blog post about this a while ago, I don't really get his reasoning but I guess he's happy with his decisions: http://www.wordsbyf.at/2011/10/31/i-dont-write-javascript/

This is utterly stupid. And the example he cites for why he prefers an odd function syntax doesn't cause errors..... if you use semicolons . JavaScript interpreters insert semicolons. If you abuse this, you'll end up with unpredictable results like his function example.

Thanks for the kind words. I've been writing javascript like this for more than a year without any trouble. It's not confusing at all, you just need to sit for 5 minutes and understand ASI.

I'd bet with confidence that 90% of programmers out there don't know if a semicolon is needed after a function declaration. They just put it in there blindly, then some day they find a bug because they "forgot" a semi-colon somewhere.

Re: Bootstrap's maintainer hates the semicolon

#113
post #34

Check out the following issue, not just the matter of style.. https://github.com/twitter/bootstrap/issues/401

Even when using semi-colons freely, most people don't put a guarding ; at the beggining or end of the file. That's unrelated to this coding style.

And honestly, that should be done by the minifier. It breaks perfectly valid javascript.

Re: Bootstrap's maintainer hates the semicolon

#114
It's amazing (and funny) how this whole semi-colon thing is based on fear.

People should understand the role of semi-colons and ASI, just like they should understand the comma operator. It's not hard, takes 10 minutes (read Isaac's post).

Everyone saying that they don't need to understand it is doing a disservice to the community, there's no harm in wanting knowledge of the language to evolve. A few years back people seldomly used IIFEs, and I remember hearing the same argument (wtf is that, don't use it, I don't understand). Change is good.

Re: Bootstrap's maintainer hates the semicolon

#115
post #84

Not having to use semicolons sounds nice, but in practice it can have terrible effects. E.g.: var x = function() { // something } // avoid polluting global scope: (function() { // Some initialization. })() This will call the function x with the anonymous function as its argument, and then call the result of that. There are more examples to that point, and many of them really aren't all that straightforward. So don't…

First commandment of semi-colon-free: any line that starts with [,(,+,- should be prefixed with a semi-colon. var x = function() { // something } // avoid polluting global scope: ;(function() { // Some initialization. })() ;[1,2,3].forEach(...) In practice those are the only places you'll see (and need) them. It has the extra benefit of disencouraging starting a line with a pre-increment, hacks or weird constructs (n…

Good advice, and interesting because languages and syntax are interesting to me. But I would never introduce this in my team as a replacement for semicolons at the end of each statement. It seems too random a rule to just follow, hence you'd need to understand the specific problem that it solves. At that point your mental model has become slightly more complex, which doesn't seem worth it.

Re: Bootstrap's maintainer hates the semicolon

#116

Earlier quoted context omitted.

First commandment of semi-colon-free: any line that starts with [,(,+,- should be prefixed with a semi-colon. var x = function() { // something } // avoid polluting global scope: ;(function() { // Some initialization. })() ;[1,2,3].forEach(...) In practice those are the only places you'll see (and need) them. It has the extra benefit of disencouraging starting a line with a pre-increment, hacks or weird constructs (n…

Good advice, and interesting because languages and syntax are interesting to me. But I would never introduce this in my team as a replacement for semicolons at the end of each statement. It seems too random a rule to just follow, hence you'd need to understand the specific problem that it solves. At that point your mental model has become slightly more complex, which doesn't seem worth it.

It's absolutely not random. You only put semi-colons where needed. There is no optional use.

'semicolons at the end of each statement' is much more random. They are unnecessary after function declarations or conditionals, and you might end up forgetting them somewhere that actually matters, just because you're not paying attention. The mental effort for this change is severely overestimated.

Re: Bootstrap's maintainer hates the semicolon

#117

Earlier quoted context omitted.

And possibly some reasons why leaving semicolons off can make his or others' lives a bit annoying: http://bonsaiden.github.com/JavaScript-Garden/#core.semicolo...

That's a part of JS Garden I don't agree with. Leaving semi-colons only changes behaviour if your code is wrong in the first place...

How is the following code "wrong"?

  var foo = function() {
  } // parse error, semicolon expected
  test()
For completeness' sake, this example works:

  var foo = function() {
  }; // no error, parser continues
  test()

Re: Bootstrap's maintainer hates the semicolon

#118

Earlier quoted context omitted.

Or because people simply don't want to have to think about when statements end. I know the semicolon insertion rules, because I'm obsessive-compulsive like that. In any team I lead, any software I write, and any open-source project I maintain, the coding standard will be "Terminate all your statements with semicolons." Why? Because my job is to make less work for people, not more. I could give them a list of 4 rules,…

Please read http://blog.izs.me/post/2353458699/an-open-letter-to-javascr... again, especially the restricted production part. Using semicolons everywhere doesn't cover your ass in those cases. Granted, I've never ever encountered these, but still -- it's not as simple as you make it out to be.

That's covered by other really common coding standards, eg. "put braces and brackets on the same line as the token that comes before them", "don't add linebreaks unless necessary to stay under 80 chars", and "break after binary operators rather than before them".

The point's to minimize the amount of additional complexity that developers have to memorize. The set of rules above is very similar to what you see in pretty much any Algol-derived language; most developers already have it burned into their fingers. The set of rules necessary to code safely without semicolons is very specific to JavaScript, and to a very idiosyncratic style of JS at that.

Re: Bootstrap's maintainer hates the semicolon

#119

It's amazing (and funny) how this whole semi-colon thing is based on fear. People should understand the role of semi-colons and ASI, just like they should understand the comma operator. It's not hard, takes 10 minutes (read Isaac's post). Everyone saying that they don't need to understand it is doing a disservice to the community, there's no harm in wanting knowledge of the language to evolve. A few years back people…

There's no should about it. I know ASI exists, and I also know I need never concern myself with it, because I prefer the simplicity and consistency of finishing each line with a semi-colon.

Re: Bootstrap's maintainer hates the semicolon

#120
Really formatting wars are really stupid. ASI is like indentation, it is trivial to write a program that converts between the two representations. The last time we had an issue about this at work, someone just wrote a pre-commit hook that sets the indentation to be standard without changing the parse tree (it actually asserts the parse tree is the same).

Now you can code any way you want, but no more white-space changes cluttering up the history of files. This is the worst type of holy war because it not only doesn't matter much (which is true of any) but there is a technological solution that the engineers involved are all ignoring.

Post reply on HN