Function declaration: space between function and parentheses
There are two contradicting rules:
function foo() {
// ...
}
var bar = function () {
// ...
};
In my opinion, there should be always a space before parentheses in function declaration.
Why?
Because it's simply ‘spaces-before-and-after-paretheses’ rule.
It's 'spaces before and after keywords' rule actually.
Yes, it stated in a codestyle this way, and function declaration is the only thing when parentheses go after identifier, not keyword. In my opinion, this rule should be broader: any kind of parentheses (including square brackets and braces) go with spaces on each side (if applicable). Then we will have a uniform rule for all these cases:
a = {x: 'y'};
b = [0, 1];
function x () {
};
It's quite simple rule which just says ‘use spaces around parentheses just like you're writing text’.
Define 'if applicable', please.
There is no space after brace in a = {x: 'y'}; since other punctuation marks ([,\.;]) stick to nearest parenthesis and there is a line break after them.
There is no space in function calls a(); and array element fetching a[1] since that's unary operators.
And the hell is this better than 'spaces around keywords'?
Are you asking why rule ‘write code just like you're writing text’ is better than bunch of rules like ‘in this case use spaces, in this case not’?
Rule ‘write code just like you're writing text’ is an extremely ambiguous one.
There are only six occurrences of the word 'space' in the code style (which isn't a lot for a 13kb document). One of them is about using spaces instead of tabs, so it doesn't count. Five left are:
- 2 about spaces around keywords;
- 1 about block-enclosing curly braces;
- 1 about comments;
- 1 about single-line array-literals.
Since we aren't discussing comments and, I believe, single-line arrays, we left with 3.
Where's aforementioned bunch?
There are more rules which are not explicitly stated, but supposed to be followed (judging by examples and .jscs):
- No spaces inside parentheses, braces and brackets
- Spaces after commas, colons, semicolons
Rule ‘use common text writing rules’ would cover all these gray zones, and would automatically apply to new ECMAScript6 symbols (like =>) when they're shipped.
function x () {} and Math.abs (x) looks weird and uncommon for me.
:-1:
We cannot change this rule because it breaks backward compatibility of yandex preset; so, developers have to change their code.
:-1:
@tarmolov Developers had to change their could since the moment you'd proclaimed your codestyle as ‘yandex’, guys.
@vtambourine Nobody offers to write like functionCall (arg). I'm just talking about declarations.
But isn't in 'common text writing' braces always surrounded by spaces? Since we proclaimed this as Yandex codestyle, lets vote for changes.
There is no space in function calls a(); and array element fetching a[1] since that's unary operators.
https://github.com/yandex/codestyle/issues/21#issuecomment-68102678
Isn't it more complicated than 'spaces before and after keywords' rule?
See https://github.com/yandex/codestyle/issues/21#issuecomment-68103407
Yes, it's more complicated.
Okay. Describe then all hidden rules regarding spaces, parentheses, and operators, and we'll compare.
Codestyle isn't aimed to describe all hidden rules and caveats, but to provide general guides.