Skip to content

Stop enforcing parameter name uniqueness #3986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bcherny opened this issue May 21, 2015 · 10 comments
Closed

Stop enforcing parameter name uniqueness #3986

bcherny opened this issue May 21, 2015 · 10 comments

Comments

@bcherny
Copy link

bcherny commented May 21, 2015

Currently enforced on this line -

node.error "multiple parameters named #{name}" if name in uniqs

This is already enforced by linters (eg. http://eslint.org/docs/rules/no-dupe-args), and is not CoffeeScript's responsibility. Instead, we should have a smarter variable naming scheme that doesn't rely on uniqueness of param names.

A side effect of enforcing this from within CoffeeScript is that pattern matching syntax doesn't work. Eg. I can't do:

foo = (_, _, count) -> count + 1
foo(1,2,3)

But the above does work in JS.

@vendethiel
Copy link
Collaborator

Nope, the above isn't allowed in strict mode, and that's why it was disallowed in CoffeeScript.

@vendethiel
Copy link
Collaborator

See #1002 and #1547.

@lydell
Copy link
Collaborator

lydell commented May 21, 2015

You might also be interested in #3972 and the following syntax:

foo = ({}, {}, count) -> count + 1
foo = ([], [], count) -> count + 1

@vendethiel
Copy link
Collaborator

(that syntax is also mentioned in #1002)

@bcherny
Copy link
Author

bcherny commented May 21, 2015

thanks for the links, i didn't realize it's not allowed in strict mode!

it would be nice to make an exception for pattern matching style (transpiling _ to non-repeated names), like what this commenter said. this style is idiomatic in many functional languages, and it would be nice to get support for it in cs.

@jashkenas
Copy link
Owner

The Party Line™ on this is that it's better to name your variables, even if you aren't going to use them. You might want to use them later, and it's better to document what they are, instead of being mystery meat free variables floating around.

@bcherny
Copy link
Author

bcherny commented May 21, 2015

@jashkenas many functional languages (scala, haskell, f#, ...) disagree. param names don't tell you much about what the variable is anyway. you need docs (jsdoc/jsig/flow), type annotations, or to look at context and consumers to get a good idea of what a param is for. the name by itself is not enough to conclude much, unless it's an idiomatic param name (like event, or error).

@vendethiel
Copy link
Collaborator

@bcherny just as you said – you don't have those annotations in coffee.

@bcherny
Copy link
Author

bcherny commented May 21, 2015

exactly. i'm arguing that param names are not usually useful otherwise.

# useful:
# (bar: number) => number
fn = (_, bar) ->
  bar + 1

# useful:
# (foo: number, bar: number) => number
fn = (foo, bar) ->
  bar + 1

# less useful:
fn = (foo, bar) ->
  bar + 2

# equally less useful:
fn = (_, bar) ->
  bar + 1

it's annotations that are useful, not param names. not only that but _ actually provides some more information (compared to foo): it indicates that the variable is unused in the function body, so you don't have to read through the body for usage sites.

@michaelficarra
Copy link
Collaborator

If you have so many positional parameters that you need to omit their names, it's time to use named parameters (passing an object and using destructuring).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants