Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does IntelliJ IDEA think my Boolean method is always inverted when it is not?

I'm new to using IntelliJ Idea and I have the following code:

private boolean verifyToken(TokenTypeEnum expectedTokenType, Token token) {
        return token != null &&
               token.getTokenType() == expectedTokenType &&
               token.getExpiryDate().isAfter(Instant.now());
}

Now IntelliJ-IDEA complains that this Boolean method is always inverted. But I specifically wrote the method so it returns a positive, I do not want to make a method isInvalidToken or something like that. I believe the point of this warning is actually to avoid negatives such as those.

What's more, if I actually let IntelliJ invert the method automatically, it makes the expression like this which is exactly NOT what I would want as it is much harder to reason about (even if I would rewrite the date comparison)

private boolean newNegativeMethod(TokenTypeEnum expectedTokenType, Token token) {
        return token == null ||
               token.getTokenType() != expectedTokenType ||
               !token.getExpiryDate().isAfter(Instant.now());
}

Why is it doing this? I can disable the warning of course, but I'd rather not.

like image 989
Sebastiaan van den Broek Avatar asked Feb 27 '18 10:02

Sebastiaan van den Broek


1 Answers

"Always inverted" means that in all places where the method is called, the ! operator is applied to the call expression. IntelliJ IDEA has no clue whether this is hard or easy to reason about from the semantic point of view; it just performs syntactic analysis. If you believe that inverting the method will make the code harder to understand, simply suppress the inspection for this method.

like image 86
yole Avatar answered Oct 01 '22 05:10

yole