I have been reading the book Crafting Interpreters by Robert Nystrom and came across the following code:
if (!scopes.isEmpty() && scopes.peek().get(expr.name.lexeme) == Boolean.FALSE) { // <- Focus here
// some code here
}
In other places where we declare variables, the book uses the more usual syntax boolean a = false notation. Stack Overflow has many answers explaining what is Boolean.FALSE but there is none stating when it should be used.
Is it a common/good practice to write Boolean.FALSE (or Boolean.TRUE) in the context of equality operators? If yes, then why?
Or, is it a matter of personal preference and code readability?
It is not common/good practice to use
Boolean.FALSEin general, but there are specific situations in whichBoolean.FALSEcan be useful. One such situation is the code in your question.Basically, that code means the same thing as
if (!scopes.isEmpty() && scopes.peek().get(expr.name.lexeme) != null && !scopes.peek().get(expr.name.lexeme))but without callingpeek().get(...)twice.If you wanted the exact same results, but without using
Boolean.FALSE, you would have to write it like this:So, using
xxx == Boolean.FALSEcan have its uses in certain specific situations.