Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are these statements equivalent?

Can I refactor like this, are these equivalent and therefore the simpler straighforward version of the code is preferred?

Before refactoring:

    if (!matcher.matches() && !matcher2.matches() && !matcher3.matches()
            && !matcher4.matches() && !matcher5.matches()
            && !matcher6.matches() && !matcher7.matches()
            && !matcher8.matches()) {
        return true;
    } else
        return false;

After refactoring:

    return (matcher.matches() || matcher2.matches() || matcher3.matches()
            || matcher4.matches() || matcher5.matches()
            || matcher6.matches() || matcher7.matches()
            || matcher8.matches()) 
like image 730
Niklas Rosencrantz Avatar asked Feb 20 '23 04:02

Niklas Rosencrantz


1 Answers

Actually, no. The first one is true only when all matchers don't match. If all matchers don't match in the second statement, you return false

return !(matcher.matches() || matcher2.matches() || matcher3.matches()
            || matcher4.matches() || matcher5.matches()
            || matcher6.matches() || matcher7.matches()
            || matcher8.matches()) 

This is correct

like image 69
wasyl Avatar answered Mar 05 '23 13:03

wasyl