philosophy : ! vs false

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();
}

Chirping in once more to say that I find the way that XSLT will allow
one to negate a boolean to be the most readable:

<xsl:if test="not($var='true')">

</xsl:if>

It would be nice if you could extend the JLS with macros, or some sort
of pre-processor that would let you use:

if (not(isValidXMLDocument))
{
throw new Exception("WHAT");
}

which would translate into:

if (!isValidXMLDocument)
{
throw new Exception("WHAT");
}


Collin
 
S

Stefan Schulz

It would be nice if you could extend the JLS with macros, or some sort
of pre-processor that would let you use:

Please not. Macros are rightfully viewed as a pest by most C programmers.
They are actually automatic source code rewriting, after all, often with
effects you never intended, like evaluating function calls several times.
 
D

Darryl L. Pierce

That should never happen, since the assignment doesn't return a boolean
value so the code won't compile.
 
A

Antti S. Brax

That should never happen, since the assignment doesn't return a boolean
value so the code won't compile.

Maybe you should have tried to compile it.

Hint: it does compile and work as advertised.
 
B

Bent C Dalager

You were propably thinking about C-like if statements.

int i = 1;
if (i = 0) {
// Not valid Java code.
}

Of course, the reason Java isn't vulnerable to this is that it doesn't
auto-cast int to bool like C does. It _does_, however, "auto-cast"
bool to bool (heh) and so it is vulnerable in the case of boolean
conditionals/assignments.

The following demonstrates that the assignment operator does evaluate
to a value just as in C:

public class Test
{
public static void main(String[] args)
{
int i = 3;
System.out.println("It is " + (i = 5));
}
}

which outputs
It is 5

Cheers
Bent D
 
T

Tony Morris

Of course, the reason Java isn't vulnerable to this is that it doesn't
auto-cast int to bool like C does.

WTF?
Since when did C have a bool (or smantically equivalent) type?
 
B

Bent C Dalager

WTF?
Since when did C have a bool (or smantically equivalent) type?

It acquired the concept the instant someone defined a language
construct that depends upon the truth-value of some expression. "if"
would be one such construct.

The primary mistake with C in this regard was to allow just about
anything under the sun to evaluate to a truth-value. The mistake with
Java was to let assignments have a value at all. In both cases, the
end result is to let programmers create hard-to-find bugs by confusing
assignment operators with comparison operators.

Cheers
Bent D
 
T

Tor Iver Wilhelmsen

The primary mistake with C in this regard was to allow just about
anything under the sun to evaluate to a truth-value.

Not as long as it's consistent. Like "nil vs. non-nil" values in Lisp.
The mistake with Java was to let assignments have a value at all. In
both cases, the end result is to let programmers create hard-to-find
bugs by confusing assignment operators with comparison operators.

Java inherited the C curse there, yes. Other languages differentiate
better by using := for assignment. As for letting assignment return,
it's convenient the few times you want to set lots of variables at
once to the same value...

E.g. for using a local (stack) ref of a value assigned to a field:

String foo = name = "Test";
 
G

Grant Wagner

Tor Iver Wilhelmsen said:
Not as long as it's consistent. Like "nil vs. non-nil" values in Lisp.


Java inherited the C curse there, yes. Other languages differentiate
better by using := for assignment. As for letting assignment return,
it's convenient the few times you want to set lots of variables at
once to the same value...

E.g. for using a local (stack) ref of a value assigned to a field:

String foo = name = "Test";

Or when you want to write:

while ((line= br.readLine()) != null)
 
?

=?ISO-8859-1?Q?S=E9bastien_Auvray?=

ronron a écrit :
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();
}
Pfffffffffffffffff... Who cares ???
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top