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

Early exits #124

Open
hadley opened this issue Feb 2, 2021 · 2 comments
Open

Early exits #124

hadley opened this issue Feb 2, 2021 · 2 comments

Comments

@hadley
Copy link
Member

hadley commented Feb 2, 2021

If you have an if statement with a large if and small else:

if (cond) {
  # 
  # 
  # 
  # 
  # 
  # 
  # 
  # 
} else {
  something()
}

It's worth considering flipping the order and using an early exit:

if (!cond) {
  return(something())
}

# 
#
@hadley
Copy link
Member Author

hadley commented Feb 3, 2021

Example from dbplyr:

check_groups <- function(.groups) {
  if (!is_null(.groups) && !.groups %in% c("drop_last", "drop", "keep")) {
    abort(c(
      paste0(
        "`.groups` can't be ", as_label(.groups),
        if (.groups == "rowwise") " for lazy tables"
      ),
      i = 'Possible values are NULL (default), "drop_last", "drop", and "keep"'
    ))
  }
}

to

check_groups <- function(.groups) {
  if (is_null(.groups)) {
    return()
  }

  if (.groups %in% c("drop_last", "drop", "keep")) {
    return()
  }

  abort(c(
    paste0(
      "`.groups` can't be ", as_label(.groups),
      if (.groups == "rowwise") " in dbplyr"
    ),
    i = 'Possible values are NULL (default), "drop_last", "drop", and "keep"'
  ))
}

@hadley
Copy link
Member Author

hadley commented Oct 1, 2021

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

No branches or pull requests

1 participant