Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the preferred way to write boolean expressions in Java

I have always written my boolean expressions like this:

if (!isValid) {
  // code
}

But my new employer insists on the following style:

if (false == isValid) {
  // code
}

Is one style preferred, or standard?

like image 261
Timothy Avatar asked Mar 09 '10 13:03

Timothy


1 Answers

I prefer the first style because it is more natural for me to read. It's very unusual to see the second style.

One reason why some people might prefer the second over another alternative:

if (isValid == false) { ... }

is that with the latter you accidentally write a single = instead of == then you are assigning to isValid instead of testing it but with the constant first you will get a compile error.

But with your first suggestion this issue isn't even a problem, so this is another reason to prefer the first.

like image 79
Mark Byers Avatar answered Sep 23 '22 03:09

Mark Byers