Skip to content

New linter: discourage the use of anonymous functions for trivial cases #1531

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
Bisaloo opened this issue Sep 6, 2022 · 3 comments · Fixed by #1541
Closed

New linter: discourage the use of anonymous functions for trivial cases #1531

Bisaloo opened this issue Sep 6, 2022 · 3 comments · Fixed by #1541

Comments

@Bisaloo
Copy link
Collaborator

Bisaloo commented Sep 6, 2022

Example

Discouraged

lapply(l, function(x) mean(x)))

Recommended

lapply(l, mean)

Scope

  • to limit the risk of false positive, I'm thinking to limit this to the apply() family function (+ replicate()), at least as a first step. Or do you think it wouldn't be much more difficult and riskier to do this in all contexts?

  • ideally, this should detect also both tidyverse (~ mean(.x)) and base (\(x) mean(x)) lambda functions

  • I'm thinking to limit it to cases with a single argument, i.e. exclude:

    lapply(l, function(x) mean(x, na.rm = TRUE))

    even though the anonymous function could be removed here as well by forwarding the ... because users might find it clearer to not forward them.

@Bisaloo
Copy link
Collaborator Author

Bisaloo commented Sep 6, 2022

Can you see any potential issues / pitfalls?

If you agree this would be a good addition, I can start working on a PR soon (now that I understand a little better how the package works).

@MichaelChirico
Copy link
Collaborator

Hmm it seems I never commented here.

We already have such a linter internally, so it'll be covered by #884

@MichaelChirico
Copy link
Collaborator

MichaelChirico commented Oct 11, 2022 via email

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

Successfully merging a pull request may close this issue.

2 participants