Hacker News new | comments | show | ask | jobs | submit login

http://blog.izs.me/post/2353458699/an-open-letter-to-javascr...

That is a pretty good rundown of the rules. The only problem with not having semicolons is cases like this, where someone is using a bad tool to minify your code. None of the tools that currently do the best job will break on that code.




Yes, tool writers need to understand this behavior and handle it.

But I still don't understand why the hell you would want to do this. These are a lot of rules to remember, and I don't think it makes sense to think about them for each statement I write. Nothing here says "here is what you gain with this technique", all I see is "it's perfectly valid so quit whining". This doesn't satisfy my curiosity.

Then we have this:

For example, instead of this:

    foo();
    [1,2,3].forEach(bar);
you could do this:

    foo()
    ;[1,2,3].forEach(bar)
The advantage is that the prefixes are easier to notice, once you are accustomed to never seeing lines starting with ( or [ without semis.

So now the argument is "don't worry, you get used to writing stupid looking code"? This is getting depressing.


starting a line with a semicolon means "start a new expression", if it is done where it is meaningful. By putting it at the start of the line, it is easier to associate it with the statement that makes the character important, and calls out that you are paying attention to one of the biggest gotchas in the language

putting a semicolon at the end of every line is basically just noise. You get used to it, and I find semi-colon bugs end up happening much more frequently. More then that, you are essentially just typing an arbitrary character 99.99999% of the time. When you need a semi colon is essentially a corner case, so the vast majority of code is just getting appended with a character which has no meaning.

Its like the javascript community decided to surround all expressions with parens, since it makes things more explicit, and there are a few cases (much more common then semi colons) where you need it in the language. Or if they decided to end all lines with //, just in case someone wanted to follow it with a comment. Those two examples make as much sense as advocating semicolons at the end of each line.


No. Semicolons are statement separators. Every other language that uses them as such puts them at the end of every expression, but it's fine if you want to put a semicolon at the beginning of every expression instead, except that that will confuse people coming from other languages.

What's not fine is pretending like javascript has significant whitespace and then...oops! it doesn't work here so we need to add something in this one special case. That is guaranteed confusion. If your only argument is that most of the time it's unnecessary, all you're really trying to accomplish is compression (guess what, JSMin does that!) and you may as well write APL. "You get used to it" is meaningless. You get used to banging your head against a wall too, if you do it long enough. Doesn't mean it's a good idea. It's actually worse than that though, because other people need to get used to it too, so you're wasting their time as well.

Surrounding all expressions (not statements) with parens does make sense, in many languages (because there are cases where you need them, and it pays to be explicit up front). That is something good style guides often dictate (like surrounding all if/else blocks with braces, even if they're only one line). Starting a comment after every line doesn't seem like a good analogy, I have no idea where you're going with that.

Let me be clear, and I've now wasted a bunch of time reading up on this so it's not just opinion: ASI is a crutch added to the language in order to minimize the effect of silly errors. It is not a language feature. Python was designed to have significant newlines, and they resolved the ambiguous cases. Javascript was designed to have semicolons between statements when written correctly. Abusing ASI is error-prone and not future-proof (against programmers, apparently language spec changes are going to maintain current behavior). If you think its abuse is a good idea, you should be quarantined from the rest of the programming community. Or write your own language and design it around this.


My argument is that ending every line with // is just noise, () is just noise, and ; is just noise, except for the one in 1000loc time that it isn't. And filling your code with noise not only makes your code harder to read and longer to type (if even by a little bit), but it trains you to ignore semi-colons, especially since you do not have a compiler complaining when you miss them. And when semi-colons fade into the background, it is MUCH harder to catch the one case where they are important.

If you were to write ruby, smalltalk, perl, google go, python, coffeescript or any of the other languages with asi, you find that people omit the semicolons in every situation unless for the few they need them, and never run into a problem. For some reason, that is not the case with js. Do all of those other languages "abuse" ASI? I work on a 500k loc ruby app, and I feel very confident in saying there has never been a single case of a bug from an ambiguous statement from asi (although there have been from the difference of && and 'and'), and there is only about 5 semi colons in the whole code base.

Now, js is different then those other languages in that it has a quirk where it treats whitespace and newlines the same for all types of brackets. Realistically (if you are writing for the browser), this only comes up in lines starting with an open parenthesis, since that is the one case where starting a new statement on a line with any kind of bracket makes sense. And even then, the one time you are likely to be doing that is for an immediately invoking function (function(){}()), which isn't exactly you do every few lines of code. So you have a choice on how to deal with this one, fairly uncommon case; either handle it in a special fashion when it occurs, or put a semi colon on every other line. I don't care which one you choose, all I ask is you afford me the same respect :)

My intent isn't to get into a silly language argument over this, but it seemed like you genuinely missed what I was trying to say. If you feel it would personally help you write better code, then more power to you. But I write a lot of semi-colonless code in various languages, and it has literally never caused a problem for me.


Ending every line with // is noise because it means nothing to the compiler or to the human. If I could write comments without prefixing them with //, I would (but the compiler won't let me). () and ; are not noise because they simplify the rules of the language. They make it so someone reading your code needs to remember fewer esoteric rules to understand it. That is a big win. Saving a single character of typing per line, for your fingers and your disk, is a miniscule win. I hope you're writing code that's more interesting than a typing contest (but if you aren't, maybe you care about the semicolons). The disk and the network don't care about one more semicolon per expression. They know about compression, they will deal with it. If your compiler doesn't complain when you miss them, you need a better compiler. Maybe a linter, or google's closure. Or maybe there's a market for a decent javascript compiler. I don't understand what you mean by "when semi-colons fade into the background, it is MUCH harder to catch the one case where they are important". If you use them everywhere and you miss one, it's easy to spot. If you have to examine each line with a set of rules in order to figure out if you actually needed one there, that's a more expensive task.

Ruby, python, and coffeescript were designed with significant newlines, and therefore designed around the corner cases. The only reason you'll need semicolons in those languages is to have two statements on the same line (coffeescript excepted, I'm not sure about it but my guess is that they did it right). These languages went to great lengths to reduce the cases where you need statement separators, and document this behavior widely and clearly. Perl requires semicolons, dunno where you got the idea it didn't. I don't know about smalltalk and go, I don't use them.

I probably did miss what you were trying to say.

In languages that weren't designed with significant newlines, everybody uses the statement separator at the end of a line, so it seems to me that moving them to the beginning of lines is just confusing. In any case, ASI is not significant newlines, it's a hack. I don't know what more to do than to just beat it into your head. It is not a feature you should rely on.

The important part of what I'm trying to express is that, in any language, the most important thing you need to do is to write explicit code. This means typing all the characters so that not only does the compiler (and tools) not get confused, but future human readers don't get confused either. Javascript intends for you to separate your statements with semicolons. If you start playing tricks with them, you are going to confuse people that try to understand (or, heavens, learn from) your code. You will get burned.

Consider this example (I'm a C guy):

    if (a && b || c && d | e || f && g)
How does this resolve? Does it help you if I write this?

    if ((a && b) || (c && (d | e)) || (f && g))
It sure as hell does. Now, these are semantically identical. But when you read the second one, you don't need to know that bitwise OR binds tighter than logical AND, and logical AND binds tighter than logical OR. These are details you should not need to remember when you read my code. Are parentheses just line noise here? I think not. I think they are quite meaningful. Not to the compiler, but to the human consumers of my code. It's costs the reader a lot more to read the first one than the second. The first one is mentally expensive. (In fact, modern C compilers will warn you about not having these parens here because they know that adding them is a good idea. If you turn on warnings.)

The same applies for ASI. If you rely on extra compiler tricks a human needs to remember, that's expensive. It is a lot better to have a simpler rule: just put a semicolon after every statement, and don't make anyone think about it again. Or put one before, I don't really care. Just do something consistent. Try to code to a simpler subset of the language, to make other programmers' lives easier.

I really don't mean this to be an attack against you or the javascript community. Well, I sort of do, because abusing ASI is stupid, but I actually mean this to be an educational experience. Let me help you mature as a programmer.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: