If statement intentions/inspections

Issue #2562 open
Xander Victory created an issue

I’d really like to see inspections ported from other IDEA languages:

  • Invert If condition
  • Remove Redundant Else

These inspections would allow the following code

Integer calculate(Integer bottom, Integer top) {
    if (top > bottom) {
        Integer sum = 0;
        for (Integer number = 0; number <= top; number++) {
            if (number % 2 == 0) {
                sum += number;
            }
        }
        return sum;
    } else {
        return 0;
    }
}

To be translated to the following using the inspections one after another:

Integer calculate(Integer bottom, Integer top) {
    if (top <= bottom) {
        return 0;
    }
    Integer sum = 0;
    for (Integer number = 0; number <= top; number++) {
        if (number % 2 == 0) {
            sum += number;
        }
    }
    return sum;
}

Comments (2)

  1. Scott Wells repo owner
    • changed status to open

    Yup, I use these all the time in Java and was just thinking myself that they would be equally useful in Apex. In fact, the one that I use most often isn't even on your list, specifically the ability to split an if statement with multiple conjoined condiitons into multiple if statements, and conversely, the ability to join nested if (or else with nested if) statements when there isn't other logic in that block.

    It's on the backlog -- and part of my personal interest -- but not sure when it'll get full attention.

  2. Log in to comment