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

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.




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

Search: