Reputation: 2477
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?
Upvotes: 7
Views: 2121
Reputation: 709
I just want to say I learned C twenty years ago in school and have moving onto Perl and Java and now C# which all have the same syntax and...
I think (!myvar) is the most popular
I think (myvar==false) is just fine too
in 20 years i have NEVER EVEN SEEN
(false==myvar)
I think your boss is smoking something-- I'm sorry but I'd take this as a sign your boss is some kind of control freak or numbskull.
Upvotes: 1
Reputation: 838186
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.
Upvotes: 10
Reputation: 26856
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:
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.
Upvotes: 2
Reputation: 66156
Everybody recognizes this snippet:
if (isValid.toString().lenght() > 4) {
//code
}
I think your second example looks at the same direction.
Upvotes: 8
Reputation: 346290
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.
Upvotes: 8
Reputation: 301
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.
Upvotes: 1
Reputation: 82096
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.
Upvotes: 2
Reputation: 115741
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.
Upvotes: 4
Reputation: 22606
IMO the first one is much more readable while the second one more verbose.
I would surely go for the 1st one
Upvotes: 2