mapsapi-codestyle icon indicating copy to clipboard operation
mapsapi-codestyle copied to clipboard

Function declaration: space between function and parentheses

Open twirl opened this issue 11 years ago • 22 comments

There are two contradicting rules:

function foo() {
    // ...
}

var bar = function () {
    // ...
};

In my opinion, there should be always a space before parentheses in function declaration.

twirl avatar Dec 25 '14 10:12 twirl

Why?

dmikis avatar Dec 25 '14 11:12 dmikis

Because it's simply ‘spaces-before-and-after-paretheses’ rule.

twirl avatar Dec 25 '14 12:12 twirl

It's 'spaces before and after keywords' rule actually.

dmikis avatar Dec 25 '14 12:12 dmikis

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’.

twirl avatar Dec 25 '14 12:12 twirl

Define 'if applicable', please.

dmikis avatar Dec 25 '14 12:12 dmikis

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.

twirl avatar Dec 25 '14 13:12 twirl

And the hell is this better than 'spaces around keywords'?

dmikis avatar Dec 25 '14 13:12 dmikis

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’?

twirl avatar Dec 25 '14 13:12 twirl

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?

dmikis avatar Dec 25 '14 13:12 dmikis

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.

twirl avatar Dec 25 '14 13:12 twirl

function x () {} and Math.abs (x) looks weird and uncommon for me. :-1:

vtambourine avatar Dec 29 '14 21:12 vtambourine

We cannot change this rule because it breaks backward compatibility of yandex preset; so, developers have to change their code.

:-1:

tarmolov avatar Dec 29 '14 23:12 tarmolov

@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.

twirl avatar Dec 30 '14 06:12 twirl

But isn't in 'common text writing' braces always surrounded by spaces? Since we proclaimed this as Yandex codestyle, lets vote for changes.

vtambourine avatar Dec 30 '14 07:12 vtambourine

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

twirl avatar Dec 30 '14 09:12 twirl

Isn't it more complicated than 'spaces before and after keywords' rule?

vtambourine avatar Dec 30 '14 09:12 vtambourine

See https://github.com/yandex/codestyle/issues/21#issuecomment-68103407

twirl avatar Dec 30 '14 09:12 twirl

Yes, it's more complicated.

vtambourine avatar Dec 30 '14 09:12 vtambourine

Okay. Describe then all hidden rules regarding spaces, parentheses, and operators, and we'll compare.

twirl avatar Dec 30 '14 09:12 twirl

Codestyle isn't aimed to describe all hidden rules and caveats, but to provide general guides.

vtambourine avatar Dec 30 '14 09:12 vtambourine