tidyverse / style

The tidyverse style guide for R code
https://style.tidyverse.org
Other
298 stars 104 forks source link

Allowing call-like function definitions if delineated properly #173

Closed MichaelChirico closed 3 years ago

MichaelChirico commented 3 years ago

Filed first as https://github.com/r-lib/styler/issues/829, but in writing that I realized the style guide itself could weigh in here as well.

Currently in the style guide we have (https://style.tidyverse.org/functions.html#long-lines-1):

# Bad
long_function_name <- function(a = "a long argument",
  b = "another argument",
  c = "another long argument") {
  # Here it's hard to spot where the definition ends and the
  # code begins
}

I basically see the point about c = having the same indentation as the function body and thus needing to scan for { at the end of a line to find the start/end of the definition.

However I think this is nicely avoided by just dropping ) { to the next line:

long_function_name <- function(
  a = "a long argument",
  b = "another argument",
  c = "another long argument"
) {
  # function body
}

I would prefer to allow this latter usage, but either weigh it would be helpful for the style guide to be explicit.

hadley commented 3 years ago

@MichaelChirico how do you feel about the proposal in #174:

long_function_name <- function(
    a = "a long argument",
    b = "another argument",
    c = "another long argument") {
  # As usual code is indented by two spaces.
}

The main difference is that we double indent the argument list and keep ) { on the same line — this matches Google's advice for C++ and was the consensus vote amongst the tidyverse team.

hadley commented 3 years ago

Closed by #174 (I know this isn't exactly what you were looking for but it's the closest I could get and still maintain rough consensus)