Skip to content

New linter to simplify negatives in conditional expressions? #1831

@IndrajeetPatil

Description

@IndrajeetPatil

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions