What is the preferred way to write boolean expressions in Java

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.


Absolutely the first. The second betrays a lack of understanding of the nature of expressions and values, and as part of the coding standard, it implies that the employer expects to hire very incompetent programmers - not a good omen.


Everybody recognizes this snippet:

if (isValid.toString().lenght() > 4) {
   //code
}

I think your second example looks at the same direction.