nested_ifelse_linter {lintr}R Documentation

Block usage of nested ifelse() calls

Description

Calling ifelse() in nested calls is problematic for two main reasons:

  1. It can be hard to read – mapping the code to the expected output for such code can be a messy task/require a lot of mental bandwidth, especially for code that nests more than once

  2. It is inefficient – ifelse() can evaluate all of its arguments at both yes and no (see https://stackoverflow.com/q/16275149); this issue is exacerbated for nested calls

Usage

nested_ifelse_linter()

Details

Users can instead rely on a more readable alternative modeled after SQL CASE WHEN statements.

Let's say this is our original code:

ifelse(
  x == "a",
  2L,
  ifelse(x == "b", 3L, 1L)
)

Here are a few ways to avoid nesting and make the code more readable:

Tags

efficiency, readability

See Also

linters for a complete list of linters available in lintr.

Examples

# will produce lints
lint(
  text = 'ifelse(x == "a", 1L, ifelse(x == "b", 2L, 3L))',
  linters = nested_ifelse_linter()
)

# okay
lint(
  text = 'dplyr::case_when(x == "a" ~ 1L, x == "b" ~ 2L, TRUE ~ 3L)',
  linters = nested_ifelse_linter()
)

lint(
  text = 'data.table::fcase(x == "a", 1L, x == "b", 2L, default = 3L)',
  linters = nested_ifelse_linter()
)


[Package lintr version 3.1.2 Index]