Improve condition check

You can just combine all four conditions into one using logical operators. For example by using logical and && and logical or ||. It could then look like:

if ((first && second) || (third && fourth)) {
    return true;
}

Or with all conditions substituted:

if ((Obj.getSource().equals("abc") && Obj.getDest().equals("bcd"))
        || (Obj.getSource().equals("abd") && Obj.getDest().equals("gdc"))) {
    return true;
}

Note that you may memorize the result of Obj.getSource() and Obj.getDest() in a variable in order to save some performance and make the condition more readable by using that variable instead.

And if you would return false; in the other cases you could even completely leave out the whole if as you could directly return the boolean resulting after evaluating the condition (it evaluates to either true or false, no need to return true and false hardcoded then):

return (first && second) || (third && fourth);

Leave a Comment