6 ms·
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-do
by evan_ 15y ago
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/ http://www.wordsbyf.at/2011/10/31/i-dont-write-javascript/
- damncabbage 15y agoAnd possibly some reasons why leaving semicolons off can make his or others' lives a bit annoying: http://bonsaiden.github.com/JavaScript-Garden/#core.semicolon http://bonsaiden.github.com/JavaScript-Garden/#core.semicolo...
- ricardobeat 15y agoThat'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...
- damncabbage 15y agoHow 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()
- idan 15y agoThis blog post accurately states the broken idea: > 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. Part of being a good steward to a successful project is realizing that writing code for yourself is a Bad Idea™. If thousands of people are using your code, then write your code for maximum clarity, not your personal preference of how to get clever within the spec. Code is harder to read than to write. To borrow a line from Jacob Kaplan-Moss, if I write the cleverest code that I am able to, then by definition I won't be able to understand it later. This semicolon approach isn't invalid, but it is clearly a source of confusion, and it requires that users comprehend the choice and the tradeoffs. This seems like a poor choice for a framework whose audience is largely people who don't grok this stuff and are bolting it on wholesale. <3 bootstrap, just my $0.02.
- dominicrodger 15y agoThe quote is Kernighan's I think: "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
- nahname 15y agoIt is a valuable and documented code quality. http://en.wikipedia.org/wiki/Principle_of_least_astonishment http://en.wikipedia.org/wiki/Principle_of_least_astonishment Principle of least astonishment (POLA) was also a core design decision behind ruby. http://en.wikipedia.org/wiki/Ruby_(programming_language)#Philosophy http://en.wikipedia.org/wiki/Ruby_(programming_language)#Phi...
- ricardobeat 15y agoLeaving semi-colons out is not clever, it's just understanding where you need them instead of shooting blindly.
- scooty-puff 15y agoDoes it hurt to have extra semi-colons? Maybe you have some empty statements. Does it hurt to have too few? Well, if you either do not know what you're doing, or someone maintaining doesn't understand what you did, etc., then yes.
- chc 15y agoExtra semicolons can cause problems. If "someone who doesn't know what they're doing" starts putting semicolons after randomly chosen tokens, it will absolutely break your program. You need to understand statement boundaries whether or not you explicitly mark them with semicolons.
- po 15y agoThis one bit me the other day and forced me to switch JavaScript compressors because the one I was using (it was python-based) didn't get the IIFE (http://benalman.com/news/2010/11/immediately-invoked-function-expression/ http://benalman.com/news/2010/11/immediately-invoked-functio...) right when appending files together. In this blog posting Jacob writes: 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. He then points to the izs blog posting about it: http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding http://blog.izs.me/post/2353458699/an-open-letter-to-javascr... Jacob calls it more 'minimalist' however it does require you to know (and think about) the rules of when Javascript accepts a newline as a line terminator and when it doesn't. In terms of cognitive minimalism 'semis-everywhere' wins. @izs even acknowledges that part of the advantage is forcing the developer to learn: To the extent that there is an objectively “better” choice, it appears to me that the minimal-semicolon/comma-first style is slightly superior, both because it is fundamentally more scannable and because it encourages programmers to better understand the language they use. and later: If you don’t understand how statements in JavaScript are terminated, then you just don’t know JavaScript very well, and shouldn’t write JavaScript programs professionally without supervision, and you definitely should not tell anyone else how to write their JavaScript programs. He excludes the possibility that you might understand the rules and still prefer to put them everywhere. So all people who disagree with his choice (which he admitted was only slightly better) tend to get categorized as people who don't understand JavaScript very well and we get a new holy war. The fact that all of the JS noobs are lumped on one side of the argument means it's not cool and we can dismiss a well reasoned opinion as ignorance. All this being said, I personally don't care strongly one way or the other.
- eloisius 15y agoMaybe this kind of reasoning explains why their API is such garbage.
- jesseemerick 15y agoI agree. As some-one who has just finished with a large-ish project using bootstrap, I can say that one of the worst parts of it is its js api.
- lukifer 15y agoCan you unpack that? At a glance, it looks okay, but I haven't actually done anything with the JS API yet.
- eloisius 15y agoI almost regret my incendiary comment above. I made it first thing upon waking up... However, I was referring to the actual Twitter API, which seems to have frequent issues. If the Twitter engineering culture is based on a bunch of "because I like it this way" solutions rather than "because this is the right way to do it," it may explain some of what I perceive to be their shortcomings. One should be self-expressive, but I don't think syntax is an opportunity to be so. Regardless of whether it works a certain way, there's very likely an objectively right way for it to be done.
- rhizome 15y agoOne should be self-expressive, but I don't think syntax is an opportunity to be so. Sure it is. Textual expression is a function of both form (syntax) and content (characters). Not only that, but TMTOWTDI enforces overlap between the right way to do something and the coding preferences of the implementor.
- eloisius 15y agoOkay, fine. It's some form of self expressions; but it's petty self expression. Writing in all lowercase is unlikely to establish you among the classics, even if your readers can still understand all of your sentences. That sort of "creative syntax" would be expected in graffiti, not Hemingway.
- thetrendycyborg 15y agoThis 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.
- insin 15y agoThe results aren't unpredictable - they're specified exactly, which makes it safe to omit semicolons except where necessary. You might not know that the return statement is a restricted production, but you do know when you shouldn't immediately follow it with a linebreak. Similarly, if you see a line beginning with ( or [ which isn't a continuation of the statement on the previous line, stick a semicolon in front of it, or in the case of a wrapped function() {}, use one of the other means of telling the parser it's a function expression. There are other restricted productions, but you just solved every non return-followed-by-linebreak ASI issue I've ever seen in the wild.
- thetrendycyborg 15y agoYes, it's specified, but you can avoid any confusion and make easier to read code by just using semicolons. It's what's called a "best practice".
- ricardobeat 15y agoThanks 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.
- thetrendycyborg 15y agoI understand JS just fine, and I know where semicolons are needed and where they aren't. But minimalism is not a reason to avoid using them. Feeling you're more clever because you leave them off doesn't make you so. JS interpreters use semicolon insertion: it's not that they're not needed, it's that they're being added for you if you don't use them.
- evan_ 15y agoReplying to myself because I decided I have more to say: Imagine if a coder wrote a big huffy blog post declaring that he was only going to use single-character variables everywhere because it was more "minimalist" than using descriptive variables. He'd be hanged. Obviously what "fat" is deciding to do with semicolons is less annoying than the strawman I just made up, but I don't think it's entirely dissimilar. The Javascript spec doesn't say that you must use descriptive variables, and using single character variables is certainly valid. However, it's an established convention that we just don't do that, because people find it irritating. Basically this just comes down to "plays well with others".
- rhizome 15y agoI have to wonder if this is just some invented crisis so this guy can place himself into the tradition of the byte-counting olden days.