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:
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.
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.
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.
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.
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.
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.
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.
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).
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.
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.
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.
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.
I'd rather consistently have semicolons at the ends of lines than crap like this:
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...
I prefer: It's fewer lines, consistent, symmetrical, and it's impossible to introduce errors through copy/paste or reordering.