Home Bad boolean zen

Do not compare to 'true'

Never directly compare a value against true – doing so is redundant. That is, never do someExpression == true. Instead, do someExpression instead, which is exactly equivalent.

After all, since someExpression is a boolean, comparing it to a boolean will just produce yet another boolean, which is redundant.

We call this kind of comparison bad boolean zen.

Use the "!" negation operator instead of comparing to false

As an extension of the previous rule, you should never do value == false or value != true – this would also count as bad boolean zen. Instead, do !value instead.

Be careful when returning or setting a boolean

When writing a method that returns a boolean be very careful to make sure that your code has good boolean zen. If you need an if statement to decide if something should be true or false, you can often simplify away the entire if statement.