You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following two code snippets are equivalent in many scenarios. But the value matching operator (%in%) approach is generally preferable for its readability (more concise) and maintainability (as values to check grows, the vector can easily be expanded) over the chained equality checks approach. Of course, this is relevant only if there are more than one comparison in the chain.
Example that should produce lint
Using chained equality checks
library(lintr)
'if (x == "a" || x == "b" || x == "c") { message("abc")} else { message("not abc")}'->code
lint(text=code, linters= all_linters())
Preamble
The following two code snippets are equivalent in many scenarios. But the value matching operator (
%in%
) approach is generally preferable for its readability (more concise) and maintainability (as values to check grows, the vector can easily be expanded) over the chained equality checks approach. Of course, this is relevant only if there are more than one comparison in the chain.Example that should produce lint
Using chained equality checks
Created on 2023-12-02 with reprex v2.0.2
Example that shouldn't produce a lint
Using value matching operator
Edge cases
x
is missing (NA
),%in%
will returnFALSE
, whereas the chained equality approach will fail.x
can be a vector in the chain (e.g.if (any(x == c("a", "b")) || any(x == c("c", "d"))) { ... }
). Such cases won't be relevant for this linter.The text was updated successfully, but these errors were encountered: