Skip to content

New linter to recommend using %in% #1875

Open
@Bisaloo

Description

@Bisaloo

I sometimes see code like the following:

x == "a" | x == "b"

It would be good to recommend using %in% in this situation. %in% is more readable and more efficient.

l <- sample(letters, 1)

microbenchmark::microbenchmark(
  l == "a" | l == "e" | l == "i" | l == "o" | l == "u" | l == "y",
  l %in% c("a", "e", "i", "o", "u", "y"),
  times = 1e4,
  check = "identical"
)
#> Unit: nanoseconds
#>                                                             expr  min   lq
#>  l == "a" | l == "e" | l == "i" | l == "o" | l == "u" | l == "y" 1337 1448
#>                           l %in% c("a", "e", "i", "o", "u", "y")  853  938
#>      mean median     uq   max neval
#>  2214.408   1518 2129.5 29833 10000
#>  1526.725    989 1268.0 37533 10000

l <- sample(letters, 10)

microbenchmark::microbenchmark(
  l == "a" | l == "e" | l == "i" | l == "o" | l == "u" | l == "y",
  l %in% c("a", "e", "i", "o", "u", "y"),
  times = 1e4,
  check = "identical"
)
#> Unit: microseconds
#>                                                             expr   min    lq
#>  l == "a" | l == "e" | l == "i" | l == "o" | l == "u" | l == "y" 1.685 1.856
#>                           l %in% c("a", "e", "i", "o", "u", "y") 1.143 1.240
#>      mean median    uq    max neval
#>  2.583897  1.931 2.450 39.678 10000
#>  1.799001  1.295 1.493 43.415 10000

Created on 2022-12-23 with reprex v2.0.2.9000

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions