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

Disable hugging when the argument is named #237

Merged
merged 1 commit into from
Feb 20, 2025

Conversation

DavisVaughan
Copy link
Collaborator

@DavisVaughan DavisVaughan commented Feb 19, 2025

Follow up to #228 after running this on dplyr code and talking to @lionel- about it

We remain confident that hugging is extremely useful in general, but we think that hugging is mostly useful when the argument in question is not named. For example:

# Very useful - and you almost read this in your head as `abort_paste0()`
abort(paste0(
  "bullet",
  "info"
))

# Feels a bit awkward - the `message` is in the way.
# The presence of `message` somewhat implies that you plan to add >1 arguments, so the naming of them would be useful,
# and if >1 arguments are present then hugging would be invalid anyways.
abort(message = paste0(
  "bullet",
  "info"
))

Hugging becomes particularly not-useful in functions that accept 1 or more key/value pairs, like mutate()

# before
storms <- storms %>%
  mutate(name = if_else(str_sub(name, 1, 3) %in% c("AL0", "AL1"), name, str_to_title(name))) %>%
  mutate(
    name2 = some_thing(),
    name3 = some_other_thing()
  ) 

# after, with hugging
storms <- storms %>%
  mutate(name = if_else(
    str_sub(name, 1, 3) %in% c("AL0", "AL1"), 
    name, 
    str_to_title(name)
  )) %>%
  mutate(
    name2 = some_thing(),
    name3 = some_other_thing()
  ) 

# after, without hugging
storms <- storms %>%
  mutate(
    name = if_else(
      str_sub(name, 1, 3) %in% c("AL0", "AL1"), 
      name, 
      str_to_title(name)
    )
  ) %>%
  mutate(
    name2 = some_thing(),
    name3 = some_other_thing()
  ) 

Hugging takes away from the symmetry a little bit, making it hard to scan down the pipeline and see the new variable names of name =, name2 =, and name3 =. It also makes it a little harder to add a 2nd column expression to that first mutate() call, because you have to place your cursor between the )) and then add the , there.

We do think that hugging can be somewhat nice in other dplyr verbs, like filter(), where named arguments are not used

# before
storms <- storms %>%
  filter(any(status %in% c("hurricane", "tropical storm", "tropical depression")))

# after with hugging - in this case you read it like `filter_any()` which is kind of nice
storms <- storms %>%
  filter(any(
    status %in% c("hurricane", "tropical storm", "tropical depression")
  ))

@DavisVaughan DavisVaughan requested a review from lionel- February 19, 2025 19:45
@DavisVaughan DavisVaughan merged commit 9f7f5ea into main Feb 20, 2025
4 checks passed
@DavisVaughan DavisVaughan deleted the feature/no-hugging-in-named-arguments branch February 20, 2025 13:36
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 this pull request may close these issues.

2 participants