-
Notifications
You must be signed in to change notification settings - Fork 187
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
New linter for complex conditional expressions #2676
base: main
Are you sure you want to change the base?
Conversation
R/complex_conditional_linter.R
Outdated
#' | ||
#' @param threshold Integer. The maximum number of logical operators (`&&` or `||`) | ||
#' allowed in a conditional expression. The default is `1L`, meaning any conditional expression | ||
#' with more than one logical operator will be flagged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MichaelChirico, @AshesITR WDYT?
I can't make up my mind if the threshold should represent the number of operators or operands.
I have chosen the former because I feel that it's an easier and user-friendly way to detect the complexity of the conditional expression, but I am not wedded to the idea.
I don't think I'm more comfortable calling Defining the metric for "complexity" here is going to be quite hard -- are there other languages that have implemented something similar, or is there some CS literature we can turn to for some more generic definitions? |
You are correct that this is a difficult question, but I don't think we need to have a consensus definition of what counts as "complex" conditional because that's an inherently subjective notion. The only thing we need to figure out is a good default (the same way we did for cyclocomp linter). And, since this is a configurable linter, even if we adopt a threshold that users feel is too restrictive, they can easily change this in config. I think the current default of lint(
text = "if (a && b || c) NULL",
linters = complex_conditional_linter(2)
)
#> ℹ No lints found. So the question for us to resolve is what should be the default threshold? I would vote for Here is an example from our codebase: if (inherits(e, "lint") && (is.na(e$line) || !nzchar(e$line) || e$message == "unexpected end of input")) {
...
} which lints with this new linter and can be modified something along these lines: is_expression_valid <- (is.na(e$line) || !nzchar(e$line) || e$message == "unexpected end of input")
if (inherits(e, "lint") && is_expression_valid) {
...
} P.S. As for linters in other programming languages, I can't think of any (neither |
closes #1830
Draft PR for early feedback.
Examples
Created on 2024-11-01 with reprex v2.1.1