views:

282

answers:

8

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?

+9  A: 

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.

Mark Byers
Actually, there is no compile error for that mistake if isValid is a variable (unless isValid is final). Which is really, *really* bad. Obviously, less of a problem if isValid a method or an expression, but as a habit...
Carl
I believe that's the reason for the order reversal. "if (false=isValid)" is an error.
DJClayworth
+2  A: 

IMO the first one is much more readable while the second one more verbose.

I would surely go for the 1st one

Diego Dias
+1 for readability.
Martin Wickman
+4  A: 

It was discussed for C# several hours ago.

The false == isValid construct is a leftover from C-world, where compiler would allow you to do assignments in if statement. I believe Java compilers will warn you in such case.

Overall, second option is too verbose.

Anton Gogolev
Boolean assignment in an expression doesn't present me any compiler error or warnings in Eclipse.
Carl
General boolean assignment doesn't, but an attempt to assign a value to the builtin constant 'false' does - hence the ordering. In the old C codes (and I mean OLD) FALSE was typically an assigned variable and could be overwritten.
DJClayworth
+2  A: 

You are evaluating the variable, not false so the latter is not correct from a readability perspective. So I would personally stick with the first option.

James
+1  A: 

The second style doesn't require you to negate the expression yourself (which might be far more complicated than just "isValid"). But writing "isValid == false" may lead to an unintended assignment if you forget to type two ='s, hence the idiom is to put on the right side what can't be an rvalue.

The first style seems to be preferred among people who know what they're doing.

Philip
+8  A: 

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.

Michael Borgwardt
You're killing me. But at least it means I'm not crazy.
Tim Drisdelle
+6  A: 

Everybody recognizes this snippet:

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

I think your second example looks at the same direction.

Roman
eschew obfuscation?
Tim Drisdelle
+2  A: 

I'm going to attempt a comprehensive answer here that incorporates all the above answers.

The first style is definitely to be preferred for the following reasons:

  • it's shorter
  • it is more readable, and hence easier to understand
  • it is more widely used, which means that readers will recognize the pattern more quickly
  • "false==..." rather than "...==false" is yet another violation of natural order,which makes the reader think "is there something strange going on that I need to pay attention to", when there isn't.

The only exception to this is when the variable is a Boolean rather than a boolean. In that case the second is a different expression from the first, evaluating to false when isValid is null as well as when it is Boolean.FALSE. If this is the case there are good arguments for using the second.

DJClayworth