philosophy : ! vs false

R

ronron

I tried to search the usenet for the same question...

Well I would like to know which is the coding style that you prefer

boolean booleanValue=getABooleanValue();
if (!booleanValue) {
doSomething();
}

Or

boolean booleanValue=getABooleanValue();
if (booleanValue == false) {
doSomething();
}
 
C

Collin VanDyck

ronron said:
I tried to search the usenet for the same question...

Well I would like to know which is the coding style that you prefer

boolean booleanValue=getABooleanValue();
if (!booleanValue) {
doSomething();
}

Or

boolean booleanValue=getABooleanValue();
if (booleanValue == false) {
doSomething();
}

I prefer (!booleanValue) because it reads more naturally to me.
 
A

alan

I prefer (!booleanValue) because it reads more naturally to me.

Seconded.

if (booleanValue == false) {
doSomething();
}

Implies the corollary:

if (booleanValue == true) {
doSomething();
}

Which is silly.
 
C

Collin VanDyck

Seconded.

if (booleanValue == false) {
doSomething();
}

Implies the corollary:

if (booleanValue == true) {
doSomething();
}

Or does it imply

if (booleanValue == true)
{
doSomethingElse();
}

?
 
T

Thomas Schodt

ronron said:
boolean booleanValue=getABooleanValue();
if (!booleanValue) {
doSomething();
}

I prefer

if (!isSomeCondition()) {
// doSomeAction();
}
 
D

Dimitri Maziuk

ronron sez:
I tried to search the usenet for the same question...

Well I would like to know which is the coding style that you prefer

boolean booleanValue=getABooleanValue();
if (!booleanValue) {
doSomething();
}

Or

boolean booleanValue=getABooleanValue();
if (booleanValue == false) {
doSomething();
}

I can't think of a reason to write ( FALSE == x ) in Java.
There are reasons for doing it in C but Java != C so they
don't apply here.

Dima
 
T

Tom Dyess

Thomas Schodt said:
I prefer

if (!isSomeCondition()) {
// doSomeAction();
}

Ditto on if (!isSomething()) {} If for no other reason, it's less
characters.
 
B

Bent C Dalager

I tried to search the usenet for the same question...

Well I would like to know which is the coding style that you prefer

In the specific case of boolean, Java is vulnerable to the
unintentional assignment bug. For this reason, either

if (!booleanValue)

or

if (false == booleanValue)

is preferred by a defensive programmer.

The following demonstrates the unintentional assignment bug. Its
output is

someBool starts as true
someBool is now false

while the programmer probably intended for it to be

someBool starts as true
someBool is now true

public class Test
{
public static void main(String[] args)
{
boolean someBool = true;
System.out.println("someBool starts as " + someBool);

// The following should have read "someBool == false" but
// the programmer had ordered pizza and the doorbell rang
// just as he was typing this.
if (someBool = false)
{
// This block never actually runs, that is not a
// relevant point to the example.
}
System.out.println("someBool is now " + someBool);
}
}

Cheers
Bent D
 
X

xarax

Thomas Schodt said:
I prefer

if (!isSomeCondition()) {
// doSomeAction();
}

I prefer using the local variable, because it helps
during debugging. I change the value of the variable
after getABooleanValue() returns and before the if()
statement that tests its value. Thus, I can alter
the path of control for testing. Plus, it leaves a
footprint that I can inspect after the if() block.
 
A

Antti S. Brax

Well I would like to know which is the coding style that you prefer
if (!booleanValue) {
Or
if (booleanValue == false) {

I prefer the one that sounds more natural when spoken out loud.
Saving 7 characters is not a reason to sacrifice readability.

"if not goodValue": ok

"if goodValue equals false": ok too, but previous is better

"if returnValue equals false": ok

"if not returnValue": sounds like it's testing if the return
value exists or not. Not good.
 
S

Stefan Schulz

I tried to search the usenet for the same question...

Well I would like to know which is the coding style that you prefer

boolean booleanValue=getABooleanValue();
if (!booleanValue) {
doSomething();
}

Absolutely the former. There is no risk of involuntary assignment, and it
also "speaks to me" much better then the later form. Especially when used
with a isXYZ() method.
 
C

Chris Smith

Antti S. Brax said:
I prefer the one that sounds more natural when spoken out loud.
Saving 7 characters is not a reason to sacrifice readability.

"if not goodValue": ok

"if goodValue equals false": ok too, but previous is better

"if returnValue equals false": ok

"if not returnValue": sounds like it's testing if the return
value exists or not. Not good.

Seconded. A lot of people are coming out in favor of the shorter form
because it sounds more natural. In most cases, it does. However, this
thread is about picking a universal rules; and as a universal rule,
there are cases where "if (!expression)" is going to sound quite the
opposite of natural.

The answer is to hire programmers you trust, inform them of the
importance of readable code in this position, and then trust them.

(Regarding "defensive programming", I find that to be a particularly
poor reason to settle for less than readable code; especially when there
are dozens of tools to choose from to catch potential bugs without
convoluting the program code.)

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
T

Tony Morris

ronron said:
I tried to search the usenet for the same question...

Well I would like to know which is the coding style that you prefer

boolean booleanValue=getABooleanValue();
if (!booleanValue) {
doSomething();
}

Or

boolean booleanValue=getABooleanValue();
if (booleanValue == false) {
doSomething();
}

It is generally accepted that using a boolean as an operand in an expression
that evaluates to a boolean is poor practice.
It is also generally accepted that redundancy in code is poor practice.
boolean x = true;
boolean y = x == true;
boolean z = y == true == true;
boolean a = x == true == (false == false && !true == (true == false))
 
P

Patricia Shanahan

Tony said:
It is generally accepted that using a boolean as an operand in an expression
that evaluates to a boolean is poor practice.

Huh? That rule would make the logical operators, such as &&,
useless. They use boolean operands and evaluate to boolean.
It would also make it impossible to assign booleans, because
the right hand side of the assignment is a boolean operand
and the assignment expression evaluates to a boolean.

Could you clarify?
It is also generally accepted that redundancy in code is poor practice.

I think I prefer aiming for readability and maintainability.
From that point of view, redundancy is usually, but not
always, bad. As indicated in other messages in this thread,
for some boolean variable names, a==false may be more
readable than !a.

Of course, it is better whenever possible to pick a variable
name that reads well whether or not it is negated.
 
O

opalpa

OK, here I go stating something that the first time I saw it I thought
stupid, but alas I've used it a bunch of times:

if (booleanValue) {
} else {
doSomething();
}

You hate the empty block? It's useless? It's dumb? Why would you do
that?

Exclamations don't look like they negate and the equals false statement
isn't a pleasent read. Also I rarely get in this situation (three out
of two hundred class files I estimate).

I like the rules specified in "The art of computer programming" by
kernighan and pike (or was it plauger?). It's a thin book filled with
advice on individual statements.
 
T

Tony Morris

Could you clarify?

Apologies.
I meant when using the equality comparison operator, not just any operator -
I just forgot to "say" it.
I think I prefer aiming for readability and maintainability.
From that point of view, redundancy is usually, but not
always, bad. As indicated in other messages in this thread,
for some boolean variable names, a==false may be more
readable than !a.

I disagree.
There is a never case where it is preferred to provide redundancy for a
boolean expression - not even an obscure one.

boolean x = true;
boolean y = x == true;
boolean z = y == true == true;
boolean a = x == true == (false == false && !true == (true == false))

At what point do you "draw the line" and say it is "excessive" instead of
"redundant in the name of readability"? I draw the line back at 'x'.
In any case, this one has been argued to death. I agree with the majority
in this case.
 
C

Chris Smith

Tony Morris said:
There is a never case where it is preferred to provide redundancy for a
boolean expression - not even an obscure one.

boolean x = true;
boolean y = x == true;
boolean z = y == true == true;
boolean a = x == true == (false == false && !true == (true == false))

At what point do you "draw the line" and say it is "excessive" instead of
"redundant in the name of readability"? I draw the line back at 'x'.

I think the whole point is that there isn't a calculatable rule. The
exception I and others take is not with where you draw the line, but the
fact that you're even trying to draw a line without seeing the code
you're referring to. I'd take just as much exception to someone who
argued that you should always put "== true" at the end (probably more,
since you are at least right 98% of the time).
In any case, this one has been argued to death. I agree with the majority
in this case.

Ooh, I know this one! The correct answer is "ad populum", right? :)

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top