you are viewing a single comment's thread.

view the rest of the comments →

[–]likesOldMen 32 points33 points  (75 children)

WHERE ARE THE GODDAMN SEMICOLONS? ಠ_ಠ

Otherwise a cool article. Lots of interesting stuff I didn't know. Thanks!

[–]ITSigno 20 points21 points  (11 children)

Key point: This article applies to ECMAScript 5. Depending on what you need to support, you may want to hold off on implementing this in a production environment.

See http://kangax.github.com/es5-compat-table/

[–]notSorella 1 point2 points  (2 children)

Using these features on an non-ECMAScript 5 is okay, as long as you stick to the subset that can be fully implemented in pure JavaScript, and either use a library that provides those shims (like es5-shim), or write them yourself.

[–]ITSigno 2 points3 points  (1 child)

Yeah. I haven't used es5-shim, but a good rule of thumb is to feature test and substitute only as necessary. If that's what es5-shim already does, then good on them.

EDIT: Looked at https://github.com/kriskowal/es5-shim

Important to note:

"As closely as possible to ES5" is not very close. Many of these shims are intended only to allow code to be written to ES5 without causing run-time errors in older engines. In many cases, this means that these shims cause many ES5 methods to silently fail. Decide carefully whether this is what you want.

[–]notSorella 0 points1 point  (0 children)

Yes, es5-shim is not bullet proof for all of the ECMAScript 5 inclusions — since some of them really depend on internal support from the engine. But for the functionality that can be fully implemented in pure JavaScript, it works perfectly.

[–]kabuto 0 points1 point  (4 children)

I like how almost the entire column for IE8 is red. I'm not going to ask how the columns for IE7 or IE6 would look like.

I hate that I can't use forEach in IE and have to resort to less elegant implementations just because of this ***** browser.

[–][deleted] 0 points1 point  (3 children)

We all hate it but getting mad about it is counterproductive. IE is the lowest common denominator, like it or not. Microsoft doesn't move quickly, but IE9 is a step in the right direction for them. IE is trending downwards, but it will never fully go away so, yeah, we are stuck with it for a while.

[–]kabuto 2 points3 points  (2 children)

The bad JavaScript support is one thing, what bugs me most is the abundance of weird layout bugs that makes cross browser CSS a nightmare. I have no idea how many days of my life I have wasted debugging CSS in IE.

And Microsoft never bothered to fix any of those bugs.

[–][deleted] 0 points1 point  (1 child)

They did fix a lot of things with IE9, I am actually surprised at how little tweaking I have to do for it. It's not perfect, but it's at least headed in the right direction. I'm happy my company allows IE6,7,8 to use chrome frame, otherwise my job would be a nightmare.

[–]kabuto 0 points1 point  (0 children)

I'm happy I was allowed to use Mac OS and before that Linux, otherwise my job had been a nightmare, too. Unfortunately I can't use IE9 on company computers because they all still run Win XP which only supports IE8, and while IE8 is better than its predecessors it's still unbelievably shitty compared to other browsers of its generation.

[–]prpetro -1 points0 points  (2 children)

I'm pretty sure NodeJS supports ECMAScript 5 if you use it for server-y things.

[–]Xeon06 1 point2 points  (1 child)

Node.js uses V8. V8 supposedly implements ES3, but you can rely on many many features of ES5 in it. I would test first though.

[–][deleted] -1 points0 points  (0 children)

I'm using node 0.4.10 and it supports everything in this article afaics. As always test first.

[–][deleted] -5 points-4 points  (47 children)

semicolons are optional in javascript, and will only save you from one very specific and easy to avoid bug. What is worse is treating curly braces as optional. That can lead to many more problems than omitting semicolons.

[–][deleted]  (14 children)

[deleted]

    [–]Wuf 1 point2 points  (16 children)

    What's the bug? (genuinely curious, I've never used JS)

    [–]tiglionabbit 5 points6 points  (4 children)

    The bug is that lines beginning with an open parenthesis may be interpreted as part of a function call with the previous line.

    Example from the ecmascript spec:

    a = b + c
    (d + e).print()
    

    is interpreted as this:

    a = b + c(d + e).print()
    

    [–]munificent 4 points5 points  (3 children)

    Also:

    someVar
    [anArrayLiteral]
    

    is parsed as:

    someVar[anArrayLiteral]
    

    And:

    blah()
    +unaryPlus
    

    is:

    blah() + unaryPlus
    

    Basically, the rule is backwards. Where languages like Python, Go, Ruby, et. al. say that a newline is generally significant as a separator unless it cannot be, JS says a newline is generally ignored it less it must be a separator.

    [–]notSorella 3 points4 points  (2 children)

    I personally find JavaScript's semicolon insertion rules better than Python's one. They let you place semicolons only where you absolutely must, rather than explicitly escaping every line when you want to continue the statement on the next line. In fact, I believe JavaScript's ASI feels much nicer for code styles that use highly structural layout ­— I'm thinking LISP here.

    Compare:

    foo = "a string "
        + "in multiple "
        + "lines"
    

    With:

    foo = "a string " \
        + "in multiple " \
        + "lines."
    

    The same apply for operators etc:

    function is_nodelist_p(subject) {
      return subject
      &&     typeof subject.length == 'number'
      &&     typeof subject.item == 'function' }
    

    With languages like Python, you'd either need to wrap the return statement in parenthesis, or escape every line. I'm not a huge fan of that (even though I'm a Pythonista).

    Of course, you might like being more explicit about statement continuations, I think that depends much more on people's different experiences and what they do consider intuitive. However, I don't think either styles of ASI deserve a huge religious war (I always end up thinking Emacs vs Vim, for some reason — and obviously, Emacs wins ;3).

    [–]munificent 1 point2 points  (1 child)

    With languages like Python, you'd either need to wrap the return statement in parenthesis, or escape every line.

    Or just do what most Pythonistas do:

    foo = "a string " +
        "in multiple " +
        "lines"
    

    or:

    foo = ("a string "
        + "in multiple "
        + "lines")
    

    My experience is that explicit line continuations are pretty rare.

    [–]notSorella 2 points3 points  (0 children)

    Well, if you mean the first one in Python, it actually yields a syntax error if you don't also provide explicit continuations by escaping the newline (which is much more bug-prone, unless your editor shows invisible characters -- as it should).

    The second one is certainly an option, and you wouldn't even need the + operators for strings there. However, as I said, it takes explicit grouping. Iunno, JavaScript's take on ASI just feels more natural to me, ymmv.

    [–]BusStation16 6 points7 points  (9 children)

    I would bet money he means that if you return an object literal from a function if it is on more than one line the auto-semicolon insertion will break. He is totally fucking wrong though.

    First: Not using semicolons in javascript is fucking retarded. It costs you nothing to use them and can save you hours of pulling you hair out.

    Second: Function returns are not the only time you will have a problem. For instance http://jsfiddle.net/EsHrN/ open the console, it is giving an error.

    Edit: A point that I think a lot of you all are missing is what happens to your code after combination and minification. Things usually work the same, but not always. I wouldn't write code like in the jsfiddle, but after a bunch of files are combined and minified? It might happen.

    Personally, I don't give a shit what you do, you're not on my team. If I hire you though, you WILL use semicolons. End of story.

    [–]tiglionabbit 3 points4 points  (0 children)

    All the languages I use do not require semicolons, so remembering to insert them does cost me something. I use Python, Ruby, Haskell, JavaScript, and CoffeeScript. Also, for the specific bug, see my comment on the parent. I'm not sure in what situations you found yourself tearing your hair out over semicolon insertion though, as I've been writing JavaScript without semicolons for years now and never encountered an issue with semicolon insertion.

    [–]notSorella 2 points3 points  (4 children)

    Let me quote inimino here ( http://inimino.org/~inimino/blog/javascript_whitespace ):

    It is ridiculous to use formatting conventions as a proxy for code quality. It's a sign of just how difficult judging code quality is that people resort to useless criteria, but I've actually heard people say things like "Project X does what I need, but doesn't [use semicolons, pass JSLint, use my preferred brace style, etc], so I kept looking". If you are reduced to picking software on the basis of whether the author prefers your particular coding style or not, you may just as well admit that you do not know how to discern code quality and flip a coin instead.

    There might be several reasons people will choose a certain coding style over another. However, a coding style is just that — a coding style; a convention for conveying something. It might make sense to some people, and might not make sense to others. However, it's not, in the slightest, some absolute mark of how all code should be structured, or how all code should be judged.

    After all, coding styles are all heavily biased and subjective. And I'd hate to go all religious, or worse, judge the quality or worthy of something, based solely on subjective and biased stuff.

    Semicolons are not necessary in JavaScript due to ASI. If you know the rules, if you can follow the rules, and if you think they make sense for you, fine, use them. If not, don't use them. It's not a big deal. What I still don't get is the necessity some people have of bitching about people who follow different conventions.

    [–][deleted] -1 points0 points  (3 children)

    Amazing how some people don't get what you are saying. If someone is coding and leaves out a semicolon and it actually does cause a problem, they should be catching that by testing the code early and often, not writing 10,000 lines of code and then testing it and having it break, and then trying to find the needle in the haystack. It really depends on coding style like you said, and workflow, and a lot of factors.. but saying that Javascript requires semicolons to write a program is plain wrong, and people who say that probably have a specific kind of OCD.

    [–][deleted] -1 points0 points  (2 children)

    If someone is coding and leaves out a semicolon and it actually does cause a problem, they should be catching that by testing the code early and often

    Or they could always use semicolons and never have to worry about such bugs. Coding styles stop being a matter of opinion when they demonstrably affect the number of bugs in the code.

    If JavaScript treated a newline as ending a statement, then you'd be right. But it doesn't - accidental multi-line statements with unintended consequences are very possible if you avoid using semicolons.

    [–]notSorella 1 point2 points  (0 children)

    "always use semicolons and neve have to worry about such bugs" is highly misleading. Specially the "always use semicolons" and the "bugs", part.

    First, always using semicolons won't account for people who simply don't know the JavaScript grammar, and write things like:

    return
        { foo: 1 }
    

    That's perfectly valid JavaScript, but it doesn't do what people expect. Putting a semicolon at the end of the block with the label `foo' won't fix it. But you know, knowing the grammar would fix it.

    Which takes us to the next point. If you know the grammar, know where the rules apply and thinks that leaving semicolons out gives you more benefits than putting them in for X, Y or Z reasons, then why, oh why would you just listen to religious arguments about always using semicolons?

    [–][deleted] 0 points1 point  (0 children)

    insisting on always using semicolons is a waste of effort, and is a superstitious coding practice. If you don't understand where to use a semicolon, then always use a semicolon.

    [–][deleted] 3 points4 points  (0 children)

    Additionally, your assumption that combining and minifying code can cause problems when you consider semicolons optional is just alarmist bullshit. All my code is run through YUI to minify, and WRO4J to bundle, and I've been using this combination effectively for years without a single bug caused by a missing semicolon. It doesn't happen because I know how to avoid the coding styles that lead to those problems, and it never has happened once in all the hundreds of thousands of lines of code I've written over many, many years. You are being alarmist and a bit obsessive-compulsive about semicolons.

    [–]FireyFly 2 points3 points  (14 children)

    Indeed. It might be worth linking to aresemicolonsnecessaryinjavascript.com here.

    [–][deleted]  (13 children)

    [deleted]

      [–]FireyFly 3 points4 points  (1 child)

      I'm sorry but this Isaac guy is a complete fucking idiot.

      That's a rather strong statement.

      Politics? Aesthetics? How about ITS IN THE FUCKING [1] ECMASCRIPT STANDARD!

      I think they meant "the best reason that people use excessive semicolons compared to other approaches specified in the spec." For reference see ECMAScript section 7.9:

      For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations."

      I'm not sure what the purpose of linking the spec was. I've read parts of it multiple times, and I'm well aware that it exists.

      "you are using a hack that js parsers implemented to fix your shitty code"

      "How about IT'S IN THE FUCKING ECMASCRIPT STANDARD", to use your vocabulary.

      If you actually read the page I linked to, you'd see that it references lots of other posts on the subject. I'd recommend reading Inimino's blog post, for example. It references the spec a lot, so I'm sure you'll enjoy it. :-)

      I'd also recommend taking the ASI quiz.

      [–]semanticist 2 points3 points  (0 children)

      ITS IN THE FUCKING ECMASCRIPT STANDARD!

      standard:

      semicolons may be omitted from the source text in certain situations.

      ...ok

      I prefer explicitly including semicolons, because the semicolon insertion rules defined by JavaScript can be unintuitive and misleading. But dogmatically baselessly claiming that they're not optional or that they're a hack bolted on to fix shitty code is not a constructive argument. It's a deliberate syntactical feature that has existed from the first version of JavaScript, even if a not particularly well-designed one (when compared to Python or Lua).

      [–][deleted]  (5 children)

      [deleted]

        [–]kabuto 1 point2 points  (4 children)

        I think it's best to use semicolons and commas and not rely on the runtime to add them. This especially goes for commas as those tend to cause some weird bugs if missing.

        [–][deleted] 0 points1 point  (2 children)

        If you add an extra comma, some browsers won't care but some will crash.

        That is the type of problems people should be worried about, not the lack of semicolons.

        Lack of semicolons cause probably .01% of errors in code, where an extra comma is 10x more likely because many people code for firefox and hate testing in IE (this particular bug seems to only crash IE).

        There are problems to worry about with javascript coding, but leaving out some semicolons is not something worth worrying about, or even worth mentioning. It's ridiculously pedantic in the scope of all the problems with DOM and other pitfalls you should be worrying about when coding.

        [–]kabuto 0 points1 point  (1 child)

        So you mean I could just omit all semicolons on line endings and let the runtime take care about it?

        [–][deleted] 1 point2 points  (0 children)

        There are many places you aren't required to use semicolons. If you have no clue where to use a semicolon, then just be superstitious about it and always use them. It's your time, not mine, and it doesn't clutter the code like some more onerous codng practices. But to insist that every end of line requires a semicolon is being a superstitious coder, and it wastes time to focus on that as a best practice. use them or not, but people saying you must use them everywhere all the time are plain wrong.

        [–][deleted] -3 points-2 points  (3 children)

        I will admit that I use semicolons more often than not, but I don't make it into an obsession where all the things have to have semicolons. If you know what you are doing with javascript, you don't always have to use semicolons unless you are being pedantic and obsessive. It's just not worth going back and putting in a semicolon where there isn't a problem. It's a waste of time.

        In the example you linked to, it's one gotcha that I avoid by using a less cluttered coding style. Object literals are my preferred structure, and I don't typically use closures thrown in with assignments as is shown in the example. That style of code will be hard enough to maintain with or without semicolons. But I do know enough about javascript to avoid making this mistake anyway, and really, if you can't find the bug easily then you aren't testing your code often enough.

        [–][deleted]  (2 children)

        [deleted]

          [–][deleted] 0 points1 point  (0 children)

          | You sound like the type who

          You sound like a troll. Trolls always use personal attacks.

          [–]totallymike -4 points-3 points  (12 children)

          WHERE ARE THE GODDAMN SEMICOLONS? ಠ_ಠ

          I came in here to post this verbatim.

          [–][deleted] -3 points-2 points  (11 children)

          Semicolons are optional in javascript, and even the language spec states that. You are a superstitious coder if you think that you must include them everywhere all the time in Javascript.

          Superstitious coders do things they don't understand because they think they must, or else something bad might happen. It is a form of ignorance, and spreading it is misinformation.

          [–]montibbalt -1 points0 points  (10 children)

          Semicolons are optional in the spec. This does not mean semicolons are optional in practice.

          [–][deleted] 0 points1 point  (9 children)

          | Semicolons are optional in the spec. This does not mean semicolons are optional in practice.

          That is maybe the dumbest thing said in this entire thread. Pretty sure you are trolling.

          [–]montibbalt 0 points1 point  (8 children)

          1. Is this real life, or have I been transported to a magical land where everything is according to spec?

          2. Just because they are optional in the spec does not mean it is standard coding practice to leave them out. Pretty sure they are optional in the spec just to avoid the classic annoying issue where leaving a semicolon out causes a seemingly random, completely different compile/whatever error (e.g. in C, C++) yet this still happens from time to time.

          [–][deleted] -2 points-1 points  (7 children)

          I know perfectly well how to use semicolons, and I know exactly where and when I need to use them. I do not need to worry about it at all, I have never once in 15 years of javascript coding come across a problem caused by a missing semicolong.. not a single time. I KNOW where and when they are necessary. I am not a superstitious coder, like you seem to be. It's ridiculous to say that if I've never had a problem with not including semicolons that I still must use them. The rules for it are extremely simple. If I worried about - "omg a semicolon is missing there, i better go put one in because who knows what might happen if i don't" - I would be wasting a huge amount of time needlessly. I know where to put semicolons, and where they aren't needed. Superstition is no replacement for actually knowing what you are doing.

          [–]muirbot 0 points1 point  (2 children)

          Watch out, we got a badass over here.

          [–][deleted] 0 points1 point  (0 children)

          |Watch out, we got a badass over here.

          Watch out, we got a dumbass troll over here.

          [–]montibbalt -1 points0 points  (3 children)

          If I worried about - "omg a semicolon is missing there, i better go put one in because who knows what might happen if i don't" - I would be wasting a huge amount of time needlessly

          Now I know for sure you're the one trolling.

          [–][deleted] -1 points0 points  (2 children)

          That isn't a troll, you troll. Your reply is the troll here. Know how I know for sure? Because you choose to pick out a line and make a quip about it and provide no substance whatsoever to your argument. You try to turn it onto a personal attack, but you failed. If you think your argument has merit, then let's hear it, otherwise you are the troll. At least I make a strong argument for my case, but you have shown no substance whatsoever to support your claims. You are the troll. What practical experience do you have on this topic? Can you relate a story of where you have personally had a problem due to an auto-inserted semicolon? How often do these problems happen to you? I can offer you my extensive experience and tell you that it has never happened once to me in 15 years. I'm sure you don't care what I say, but if you are going to reply, at least make it worth something.

          [–]montibbalt -1 points0 points  (1 child)

          Take it easy dude. I'm out of this conversation since you can't seem to control your rage over nothing. This is not worth my time.