Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Actual drawbacks to omitting semi-colons? (groups.google.com)
69 points by stevefink on Oct 6, 2010 | hide | past | favorite | 47 comments


I think this style ("npm style") is trying too hard to be clever without much benefit. Classic JavaScript style (see: Crockford) has done just fine and is familiar to everyone who knows C-style syntax. No reason to change it now.

I'd rather consistently have semicolons at the ends of lines than crap like this:

    ;(x || y).doSomething()
    ;[a, b, c].forEach(doSomething)
It's easier to always include the semicolons at the end of statements rather than try to figure out the edge cases where missing a semicolon would change the behavior.

I'm also not a fan of the "comma-first" style, especially for var declarations...

    var foo
      , bar
      , baz
      ;
I prefer:

    var foo;
    var bar;
    var baz;
It's fewer lines, consistent, symmetrical, and it's impossible to introduce errors through copy/paste or reordering.


I've grown to prefer:

    var foo,
        bar,
        baz;
It seems less cluttered. You can of course introduce errors through copy/paste/reordering, but I think it's a reasonable tradeoff. I agree about comma/semicolon-first being strange though.


I hate semicolons too, but I wouldn't recommend removing them in your javascript. Douglas Crockford says to use semicolons, so use semicolons. JS lint is a useful tool and its worth having your code conform to it.

If you really hate the syntax quirks of javascript like me, a better solution is to just preprocess it. CoffeeScript has made me really enjoy writing frontend ui code again. http://jashkenas.github.com/coffee-script/


True fact - CoffeeScript did the same for me even though it was heavily frowned upon by others.

Typically I omit them if I can. And I have no reason to do so other than I think it makes the code look cleaner (I am often quite zealous on proper indentation / return lines).

I figure if using them works for you, rock on. If you hate them and don't use them, rock on. I really don't get bothered by debugging others' code either way as long as it is "readable-formatted" and works.

I've found that usually you fall into a situation where you have a company standard and have no choice in the matter though, and it doesn't even make a crap what your personal preference is.

Edit: To be fair though I only got "Eagle" on the test, but I chalk that up to only giving the test about 5 minutes of my time, too.


One note about this is that CoffeeScript generates JS source that passes JavaScript Lint. You can use `coffee --lint` to verify this, and make sure you get "0 error(s), 0 warning(s)"...


Speaking of, thanks for the introduction to CoffeeScript on Sunday. I think you made a convert out of me. I'm actually kind of enjoying the idea of writing a front end in JS without Sproutcore or Cappuccino solely because of CoffeeScript.


Well, there are a few narrow examples where omitting semicolons will lead to incorrect code. If you're a superhuman javascript whiz who never makes mistakes, then sure, leave out the semicolons. But for the rest of us mortals, it would be nice if you left them in.


Typical pant-wearer.


"Coding styles are like assholes. Everyone has one, and nobody likes to look at anyone else's." -- credited to Eric Warmenhoven


The analogy falls apart where people like to look at their own coding styles, but nobody likes to look at their own asshole. So the rule is more like “nobody likes to look at assholes at all”, which is a pretty different situation from coding styles. That’s a relevant enough distinction that I would call Warmenhoven’s analogy a bad one, good for provoking a laugh but not for discussing coding styles.


Actually, I looked up Warmenhoven's quote on the net after posting, and the original was "nobody likes anyone else's". That eliminates that little semantic problem.


The one place where this really bites me is return statements. For example:

    function x() {
       return 
    	   2+3;
    }
    alert(x());
Every now and then I'll reformat a line by breaking it in two and it takes me 10 minutes to figure out why my code is not working.


damn it I forgot that - does anyone know how to make vim highlight this?


In your after/syntax/javascript.vim:

    syntax match javascriptReturnError /return\s*\n\r\?\s*\w.*$/

    ...

    highlight link javascriptReturnError Error
Obviously you can play around with the regex to tweak what matches.


If you prefer not to use semi-colons in JavaScript but need to add them for other’s sake, you could try programming in CoffeeScript (http://jashkenas.github.com/coffee-script/), which is a language meant to be a bunch of small improvements to JavaScript that compiles into JavaScript. It doesn’t require semi-colons. You can show others just the compiled JavaScript, and they need never know you are using something else.

Edit: ninja’d by railsjedi: http://news.ycombinator.com/item?id=1762677


I realize semicolons aren't required for javascript, but I put them in anyways merely because I do so much coding in other languages where semicolons are required that it's silly to try and modify my habits just because it's not a requirement to put in.

Just because you don't wear pants at home doesn't mean you're not going to feel silly showing up at work without them.


There are numerous cases where lack of semicolons create ambiguous code which introduces bugs.

Here are some: http://gist.github.com/612814

Edit: Er, tried to paste code in-line but formatting got messed up.


Well, the code isn't actually ambiguous (since the spec states well defined behaviour), just very easy to misinterpret.


Ambiguous to a human, vs. a compiler.


Ambiguous to a human who doesn't understand the rules of automatic semicolon insertion. Maybe such a human shouldn't be using javascript in the first place, instead of working in fear.


Human doesn't understand complex rule with counterintuitive corner case. Film at 11.


On the "if we were to design a programming language" level, it seems that a feature like ASI (Automatic Semicolon Insertion) is not only useless but even harmful. Every programmer is required to learn the rules of the mechanism in order to understand others' code. Furthermore, it needlessly increases the amount of possible programming styles, which, in my opinion, is a bad thing.

C programmers have always used semicolons and never wasted a second thinking whether they should.


> C programmers have always used semicolons and never wasted a second thinking whether they should.

And, as one who primarily writes in Python and Ruby, I always find myself omitting them, and spending more than a second wondering why they're necessary.


I always use semicolons in js because I usually compress the code down by removing superflous whitespace -- which would make this style of code not function at all.


The major compressors are syntax aware. When they compress your JS, they'll put in semicolons where needed.


A newline takes up just as many bytes as a semicolon.


But if you have a newline you generally have an indent. Furthermore, there are places in JS where semicolons are absent and newlines are not (after braces).


It's moot, anyway. Format your code for readability, and let a minifier handle making it smaller. Then let gzip finish the job.


Unless you're using CR+LF as your newline delimiter.


I leave semicolons out in my code. But it's python so I think that's ok.


I sometimes use semicolons in Python for convenience when writing short debug code. Fewer lines, fewer pound signs I have type to comment it out later.

e.g., print somevar, othervar; exit()


import pprint; pp = pprint.pprint


    SyntaxError: not a chance!


yep, and roughly speaking:

javascript - var - ';' - '{' - '}' | s/function/def/ ~= python


I understand the pants thing.


I'm fine with coding conventions that aren't anal. Do I wear pants to my friends' houses? Yes. However, I won't go over there many times if the rule is that I have to wear black khakis I bought at Wal mart and have exactly 5 pockets.

Same for coding conventions. Want me to use camel casing for global variables even though I think it's hideous? Fine. Want me to put closing parenthesis on a newline by themselves on a multiline function call and I put it at the end of the last line? Find something better to do with your time.

Unfortunately, I don't know enough JavaScript to know where this falls.


One related issue is how should one mark up code that is intended to flow over multiple lines? Consider doing a long chain like:

MyObj.item.childItem.getAll()[0].properties... // etc.

It's common to want to split this over several lines. I am in favour of placing the the period at the end of the line to indicate that the expression continues. Other people prefer the dot to start the next line instead. I don't like the latter since the first line then looks like a mistakenly omitted semi-colon.


The dot is the operator.

It is customary to stack operators by indenting.

Why are lines without semi-colons at the end bad?


Um, ok, the ASI quiz is clearly lame: out of 41 I missed two and wasted one, and I know no Javascript whatsoever. So I'm not sure what that was supposed to prove.


The problem is that there is an actual drawback to making semicolons optional: adding a newline between tokens can change the meaning of the code!

This "feature" is really just a bad idea. I would hope that it gets thrown out in future versions of the standard (is there one?), and use semicolons in the meantime.


I am certain there are words of wisdom in The Pragmatic Programmer book that correlate to this post. I'll update my comment if I find it, but I'm sure someone here will remember it before me.


I always use semicolons, because I also code in several other languages, that require them. But I have no problem, if someone leaves them out. This is how JavaScript was designed.


I know this is about semi-colons.. but it's more the comma at first that I found weird.

Example of what I mean:

- "Do a, b, c, ..." vs - "Do a ,b ,c ,..."


Considering semicolons are needed or optional in most languages why not just get in the habit of typing them? Sometimes that means I slip them in my Python code, but everything still works. Just makes more time for thinking about what the code is actually doing.


Sometimes I wonder how many gigabytes of flamewars about "coding style" C-like syntax has caused.


Indeed. Something to be said for line oriented languages like Ruby, Python, shell-script, xBASE, BASIC (I said that?!?), et al.

This is how it is, now use your time to understand the bigger structure, not punctuation.


What? You should use in semi-colons.




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

Search: