10

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?.

Community
  • 1
  • 1
pavanlimo
  • 4,122
  • 3
  • 32
  • 47
  • Exact duplicate of [What is the preferred way to write boolean expressions in Java](http://stackoverflow.com/questions/2409378/what-is-the-preferred-way-to-write-boolean-expressions-in-java) – Pascal Thivent Aug 30 '10 at 05:35
  • Ok, now I have to concede to my friend's view(and you guys). Thanks all you guys for quick participation. :) – pavanlimo Aug 30 '10 at 05:38
  • 2
    If you support 1, why stop there? Reduction ad absurdum demands that you use `if ((((foo == false) == true) == true) == true) ...`. – paxdiablo Aug 30 '10 at 05:54
  • Ok!! I give up!. My hands are up in the air! – pavanlimo Aug 30 '10 at 05:58
  • See also http://stackoverflow.com/questions/2661110/is-it-bad-to-explicitly-compare-against-boolean-constants-e-g-if-b-false-in – polygenelubricants Aug 30 '10 at 08:04
  • just googled it, I say it depends, if the variable is very long as it can be the case in java, option 1 is more readable : thisIsALongClassName.IsAllowedToDoSomething == false is more readable than !thisIsALongClassName.IsAllowedToDoSomething, however if the distance between the ! and the variable to be tested is short, option 1 better – Yvon Huynh Sep 13 '18 at 15:42

7 Answers7

13

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

if (!hasPurple) ...

Jeffrey
  • 1,681
  • 3
  • 13
  • 23
6

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 Gray
  • 3,186
  • 4
  • 33
  • 34
  • 1
    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 Aug 30 '10 at 15:06
  • Agreed double negatives are evil :) But then, if using purely if-else, !notFoo would just be named Foo :) – Rob Gray Aug 30 '10 at 23:49
5

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
  • 639
  • 1
  • 4
  • 12
  • `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 Aug 30 '10 at 05:41
  • 1
    @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 Aug 30 '10 at 05:48
  • 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 Aug 30 '10 at 06:32
1

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.

Community
  • 1
  • 1
YoK
  • 14,329
  • 4
  • 49
  • 67
  • 3
    -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 Aug 30 '10 at 05:58
  • @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 Aug 30 '10 at 06:06
  • Thank you for removing the ambiguity. I really think it's important to maximize transparency and I'll remove my downvote. – Pascal Thivent Aug 30 '10 at 06:25
0

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
  • 10,539
  • 20
  • 100
  • 144
0

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.

drekka
  • 20,957
  • 14
  • 79
  • 135
0

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
  • 4,517
  • 3
  • 29
  • 32