Hacker News new | ask | show | jobs
by ricardobeat 5185 days ago
So in this case:

    var value = x + fn
    (y).burp()
Is it obvious that there is a missing semi-colon at the end of line 1? The code is technically correct without it (not that I approve of it). You can assume it's wrong, but it's just a guess.

If your code is in no-semi-colon style, there is no ambiguity, you can be 100% sure that this is a mistake: there always should be a semi-colon guarding that parenthesis, regardless of intent:

    var value = x + fn
    ;(y).burp()
It's about removing ambiguity and visual clutter with a simple set of rules.

How many JS programmers know the difference between a function expression or declaration, and the semi-colon that should follow or not? You're dependent on a linter. Following the no-semi-colon rules makes your intentions clear in every case, without machine validation.

1 comments

I don't follow this argument. Putting a semicolon at the end of every statement, regardless of what follows, seems to be an even easier way to avoid making this bug.
I think the usual semi-colon-less style is to put a semi-colon in front of lines beginning with parentheses (and only those lines). This is the one potential problem area if you don't have semi-colons everywhere, so you can avoid almost all (or maybe actually all) problems just by prepending a semi-colon to those lines.

I think Ricardo's point is that this style (having a semi-colon only before parentheses at the beginning of the line) makes your intentions more explicit.

The full list is [(+*/-. (basically parenthesis, array/property acessor, and any math)
> Putting a semicolon at the end of every statement

Sounds like a viable strategy - but you have to parse where every statement ends in your head. The situations where you don't end a line with a semi-colon are more numerous than the rules you need to write semi-colon-free code. Besides, if it's so simple, why not leave it to the interpreter?

It's trivial to parse when statements end in my head, because I write code in a structure that makes it unbelievably clear where statements begin and end (with or without semicolons.) Knowing that is of prime importance to human readability, so it has to be dead obvious or the code sucks.

I'd love to leave it to the interpreter, but the interpreter doesn't put a semicolon at the end of every statement for me, as I write them naturally. It puts a semicolon at the end of... most statements.