Skip to content
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

Line breaks in function calls with named arguments #39

Closed
lorenzwalthert opened this issue Aug 23, 2017 · 5 comments
Closed

Line breaks in function calls with named arguments #39

lorenzwalthert opened this issue Aug 23, 2017 · 5 comments

Comments

@lorenzwalthert
Copy link
Contributor

In the styler project, we came across the question whether the rule to

break the line after ( if a function call does not fit on one line

applies to all functions, or whether switch(), ifelse() and friends are an exception to it In other words, whether

switch(on_same_line,
  next_line,
  another_new_line
)

Is preferred over

switch(
  on_next_line,
  next_line,
  another_new_line
)

Another (potential) candidate is case_when().

Reference: krlmlr/rlang#1 (comment), r-lib/styler#152.

@hadley
Copy link
Member

hadley commented Aug 23, 2017

I don't think your example is quite right. I think the question has to involve named arguments.

switch(on_same_line,
  option_a = next_line,
  option_b = another_new_line
)

And I think the principle is that it's ok to put all the un-named arguments on the first line, if they fit. If they don't fit on one line, then they should be spread over multiple lines. I think this convention is nice for lapply()/map():

map(x, f,
  extra_argument_a = 10,
  extra_argument_b = NA
)

If no arguments are named, and it doesn't fit on one line.then each argument always gets its own line.

@lorenzwalthert
Copy link
Contributor Author

Ok, thanks for the clarification. cc: @krlmlr

@krlmlr
Copy link
Member

krlmlr commented Aug 24, 2017

@lorenzwalthert lorenzwalthert changed the title Line break after switch and friends Line breaks in function calls with named arguments Jan 6, 2018
lorenzwalthert added a commit to lorenzwalthert/tidyr that referenced this issue Jan 6, 2018
This version handles line breaks in function calls according to tidyverse/style#39.
hadley pushed a commit to tidyverse/tidyr that referenced this issue Jan 6, 2018
* styling with strict = FALSE and scope = "tokens"

* styling with styler#318. strict = TRUE, scope = "tokens").

This version handles line breaks in function calls according to tidyverse/style#39.
@lorenzwalthert
Copy link
Contributor Author

To close this issue, can we include such a paragraph in the style guide that describes the principle outlined above? I can get us started with PR if @hadley does not want to do it himself.

@hadley
Copy link
Member

hadley commented Jan 6, 2018

The principle should mention something about unnamed arguments being special - you only omit the names of arguments when they are used sufficiently commonly (i.e. with almost every invocation of that function) that you can assume most readers will know know what they are.

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

No branches or pull requests

3 participants