-
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 to simplify negatives in conditional expressions? #1831
Comments
Honestly, I prefer Also, control flow should be ordered in such a way that the surrounding code reads naturally. Therefor, it depends on context whether |
this is a Google linter IINM -- `IfNotElseLinter()` :)
…On Sat, Dec 10, 2022, 3:21 PM AshesITR ***@***.***> wrote:
Honestly, I prefer !has_fuel || !has_mirrors because parentheses use
additional brainpower.
Also, control flow should be ordered in such a way that the surrounding
code reads naturally. Therefor, it depends on context whether if (!cond)
or if (cond) is more readable.
—
Reply to this email directly, view it on GitHub
<#1831 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AB2BA5JBM6NSAY2SPIYS66TWMUF7XANCNFSM6AAAAAAS2QEHLU>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Fair enough. But my preferred approach here would be to extract the conditional expressions to a new variable so that the parentheses can be avoided. Additionally, this introduces a reusable abstraction with an informative name. # actual
if (!has_fuel || !has_mirrors) {
can_be_rented <- FALSE
}
# refactored
is_car_in_good_condition <- has_fuel && has_mirrors
if (!is_car_in_good_condition) {
can_be_rented <- FALSE
} |
The first part of this issue was addressed by #2071. I sense there is no consensus about whether the second part should be supported. |
I think consensus is more important for setting defaults -- it seems like a reasonable option to add, though I wouldn't enable it by default. |
Sounds good to me. Maybe we can call this parameter |
|
Preamble
There ain't nobody that doesn't struggle with negatives, and it would be nice to have a linter that suggests a way to simplify boolean tests with negatives.
Here are a couple of ways to do this in a single linter.
1. Re-order
if
/else
Actual
Suggested
2. Applying De Morgan's laws
Actual = Suggested
WDYT? Any other ways to simplify these?
The text was updated successfully, but these errors were encountered: