views:

289

answers:

7

Possible Duplicate:
What is the preferred way to write boolean expressions in Java

Today, me and my colleague raked up an argument. Which is a better way to use the boolean variables in Java code along with if statements.

boolean foo=true
//1. 

if(foo == false)
    // do something
else
    // do something else

//2.

if(!foo)
    // do something
else
    // do something else

I support [1], since I think it's more readable. What do you guys think?.

A: 

I go for 2 because as a programmer even 2 is readable.

Imagine you have hundreds of such validation check do you always check with false.

No

ckv
+11  A: 

Number 2, along with "foo" having a descriptive name, so that the code reads well:

if (!hasPurple) ...

Jeffrey
Yes, better named variables is definitely the way to go. Becomes more readable.
Derek Clarkson
A: 

If you are the only one who is going to maintain your code then you are free to use whatever style you like.

Having said that !foo is favored by most of the developers I know.

Derek Clarkson
A: 

Its a matter of opinion. I prefer number 2 because its less code to write and I think it is just as readable.

Conceited Code
+5  A: 

I find #2 more readable. I think every Java dev (I'm a C# dev) would know what ! means. I

While probably not the point here, I prefer to put my "true" block as the statement. If the condition is generally going to be false, then I name my variable to represent

if (notFoo) 
  // do something when
else
  // do something else
rob_g
I disagree. Boolean variables should be named assertively whenever possible. You should be able to always read ! as "not", and if you do need foo, you avoid the confusing double negative !notFoo. The only exception is when foo has an antonym (like convex vs concave).
ILMTitan
Agreed double negatives are evil :) But then, if using purely if-else, !notFoo would just be named Foo :)
rob_g
+4  A: 

I find it a good idea to avoid things like

if (foo == true){}

because occasionally you might write

if (foo = true){}

as a typographical error. Often times it's an easy mistake to spot, but it just seems to lend itself well to making that quick mistake.

JBirch
`if (foo = true) {}` is not legal Java and won't compile (and should stand out as bad syntax in your IDE). Being afraid of `if (var = constant)` statements is a bad holdover from C, where it was legal (and sometimes encouraged) to assign in the conditional. Stop worrying about it, and instead worry about what is more readable.
Avi
@Avi: It does indeed compile under JDK6. I gave you the benefit of the doubt and wrote it up myself. It compiles with no warnings on both command line on a FreeBSD box and through Eclipse, and Eclipse shows no warnings of its own.
JBirch
@JBirch - you are correct. I apologize. I was confused, because most assignment expressions are illegal in conditionals, unless they are of type boolean (since there is no automatic int-to-boolean or pointer-to-boolean conversion in Java). I would still go with the more readable though. In the case of integers, I would write `if (foo == CONSTANT)`; in the case of booleans I would just write `if (foo)` which is both more readable and avoids this issue.
Avi
A: 

When using boolean variable as a condition in statement, don't compare it with true.

Not a mistake, but bad style, As it's already a boolean value, so just use it.

Reasons why "!foo" is better than "foo == false". Referenced from

  • Conciseness: assuming that you are in a context where a boolean is
    required, and "x" is a boolean, it is less characters to write "x" than "x
    == true", or "!x" than "x == false".

  • Convention: seasoned programmers in Java (or C, C++, C# and most other
    languages) expect to see "x" rather
    than "x == true", and "!x" rather
    than "x == false".

  • Robustness: in Java the conditional and loop statements all require a
    boolean valued expression in the
    condition. If "y" is not a boolean,
    then a typo of the form "if (y = foo) {" will give a compilation error in
    Java. But if "y" is a boolean then
    "if (y = foo) {" does not give a
    compilation error. Hence, by avoiding "==" for booleans you avoid setting
    yourself up for a whole raft of bugs
    resulting from typos.

YoK
-1 for **integral** copy paste of [Stephen's answer](http://stackoverflow.com/questions/1451152/difference-between-while-x-false-and-while-x-in-java/1453172#1453172). First, even if there is a link, you make the content appear like it is yours, or rephrased, while it isn't. Format it as quoted so there is no ambiguity. Second, if the pasted answer is appropriate, then the question is very likely a duplicate and should be closed as such. At the end, this is just bad practice.
Pascal Thivent
@Pascal Thivent For my answer I added my view (top 2 lines in bold) as well as added referenced section from Stephen.I also added link where it was referenced from. I didn't intent to show that referenced section was my view. I am sorry if it felt so. I have removed ambiguity as well as quoted it clearly.
YoK
Thank you for removing the ambiguity. I really think it's important to maximize transparency and I'll remove my downvote.
Pascal Thivent
Thanks Pascal Thivent
YoK