Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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 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.


The 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."


It is a valuable and documented code quality. 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)#Phi...


Leaving semi-colons out is not clever, it's just understanding where you need them instead of shooting blindly.


Does 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.


Extra 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.


Replying 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".


I 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.


This 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-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-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.


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()


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.


The 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.


Yes, 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".


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.


I 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.


It's not about being clever. As I've said before, it takes all of 10 minutes to understand it, but nobody cares. There is nothing magic about ASI, a line break ends a statement in 99% of the cases.


The question is, why should I care to not end a statement in a semicolon? I gain no advantage. Any speed in coding advantage comes from my editor, not omitting semicolons. Most people commenting here understand ASI. Simply understanding something doesn't make it better. I could write code like I'm a bizarre minifier-human hybrid if I really wanted to. What would that gain me?

Indenting isn't necessary either. Less necessary even than semicolons. We do it for a reason. Maintainability, understandability.

The less ambiguity the better.

The only advantage you get is you can see a thread like this and say I DON'T USE SEMICOLONS CUZ I BE SO SMART I GET JS SO GOOD. But you're not coding to work with other people. It helps no one else, it's esoteric and unnecessary.


I don't see the need for personal offense here. You don't like it, ok, but it's a perfectly reasonable, rational and valid approach. NPM, the node.js package manager with +7000 packages published, is written in ths style.


I'm not taking personal offense, I'm just saying 1. It's not perfectly reasonable. 2. It's not rational 3. It is valid, but it is not sound. It's not an advantageous approach. It is annoying to other coders.


Maybe this kind of reasoning explains why their API is such garbage.


I 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.


I 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.


One 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.


Okay, 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.


Can you unpack that? At a glance, it looks okay, but I haven't actually done anything with the JS API yet.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: