That is the first JS style guide where there isn't anything stupid in it and I actually agree with what it says. The closest thing to a disagreement is single quotes only for strings, and that is only because I tend to mix them up myself.
They even got var right which says less about them and more about the state of "JS culture". (I would have expected one var per line to be an obvious no brainer which didn't need to be stated.)
The closest thing to a disagreement is single quotes only for strings, and that is only because I tend to mix them up myself.
Same, actually.
One of the good things about this being on GitHub is that you can fork it to your project's / company's profile and modify it to put in place whatever standards you want. It makes a great starting point if nothing else.
Our team discovered and forked that repo sometime last year. Between that and Todd Motto's Angular styleguide for teams, I'm pretty happy with the code standards we have.
(I would have expected one var per line to be an obvious no brainer which didn't need to be stated.)
I don't get why people form such strong opinions about these things when software can automagically convert a list of variables into separate vars or convert one type of string quote into the other. Do what ever the hell you want and then convert your code to the team standard before commit.
Put beautification with all your style rules in either the build process, the unit test cycle, or in git hooks. If this were automated you shouldn't be going back to fix any of this madness.
So you'd even declare your loop iteration variables at the top of the function?
To be fair to your coworkers, hoisting is a crazy concept and the lack of a block scope is bizarre. Hoisting came from the same mindset that he gave us the dangerous with keyword.
That only works for valid code. One of the main arguing points is which is more error-prone - and faulty code will need to be fixed by hand before you run it through a tool.
Sure. Let's say you have a variable declaration like this:
var foo = 'foo',
bar = 'bar';
And modify it to this:
var foo = 'foo,
bar = 'bar';
baz = 'baz';
You can spot the error, but a tool to convert var styles won't. It will (likely, I haven't run it) correctly convert the first var statement and leave the implicit global intact because it doesn't want to modify behavior.
The above mistake is much less likely if you're using this style:
var foo = 'foo';
var bar = 'bar';
And because that is correct code, your tool can correctly convert it to the single-var style.
Your editor (or jshint or jscs) should be warning you that you have a global assignment. I prefer multiple vars myself, most minifiers handle them, but seriously this is a non-issue
Actually, the Pretty Diff tool I linked to told me what the error was, an unterminated string. This is something most automation tools would catch. They may not fix your broken unparsable code for you, but they will likely tell you its broken and where to look.
I would have expected one var per line to be an obvious no brainer which didn't need to be stated.
Doesn't Crockford say to use a single var statement with commas in The Good Parts? I seem to remember thinking that was the gospel for a while before I started noticing how many times small bugs like they describe occurred:
var items = getItems(),
goSportsTeam = true;
dragonball = 'z';
I think that many would agree Crockford has a tendency to allow his top priorities to completely overwhelm any secondary priorities. In this case he wants to ensure that hoisting doesn't muck things up, so he wants to ensure that every var statement is written (nearly) where it would be hoisted to, so he promotes a style that makes anything else impossible and accepts the fact that this style makes editing more time-consuming and error-prone.
40
u/sime Mar 27 '15
That is the first JS style guide where there isn't anything stupid in it and I actually agree with what it says. The closest thing to a disagreement is single quotes only for strings, and that is only because I tend to mix them up myself.
They even got
varright which says less about them and more about the state of "JS culture". (I would have expected onevarper line to be an obvious no brainer which didn't need to be stated.)