Skip to content

Multi-argument function call as argument to another function #238

@david-romano

Description

@david-romano

Here are two examples (based on first example from documentation for case_when()):

# Bad?
tibble(int = 1:70) |> 
  mutate(
    response = case_when(
      int %% 35 == 0 ~ "fizz buzz",
      int %% 5 == 0 ~ "fizz",
      int %% 7 == 0 ~ "buzz",
      .default = as.character(int)
    )
  )
# Good?
tibble(int = 1:70) |> 
  mutate(
    response = 
      case_when(
        int %% 35 == 0 ~ "fizz buzz",
        int %% 5 == 0 ~ "fizz",
        int %% 7 == 0 ~ "buzz",
        .default = as.character(int)
      )
  )

In the first case, the name responseobscures the fact that the arguments that follow belong to case_when(). This becomes much worse as the number of arguments and levels of nesting increase, so I would far prefer

    response = 
      case_when(

to

    response = case_when(

Thoughts? Could this be added to the guide?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions