-
Notifications
You must be signed in to change notification settings - Fork 192
Open
Labels
Description
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
if (!normal_case) {
...1
} else {
...2
}
Suggested
if (normal_case) {
...2
} else {
...1
}
2. Applying De Morgan's laws
Actual = Suggested
- (not A) or (not B) = not (A and B)
if (!has_fuel || !has_mirrors) {
can_be_rented <- FALSE
}
if (!(has_fuel && has_mirrors)) {
can_be_rented <- FALSE
}
- (not A) and (not B) = not (A or B)
if (!is_tank_empty && !is_mirror_broken) {
can_be_rented <- TRUE
}
if (!(is_tank_empty || is_mirror_broken)) {
can_be_rented <- TRUE
}
WDYT? Any other ways to simplify these?