Connecting Tech Pros Worldwide Forums | Help | Site Map

Where are these odd comparison usages useful?

-Lost
Guest
 
Posts: n/a
#1: Dec 9 '07
I read on a blog where someone was baffled by the outcome of code such
as:

if (a == b == 0) { /* if a AND b == 0, execute */ }
if (a && b == 0) { /* if a AND b == 0, execute */ }

It just depends on how you evaluate this code as to what the outcome is
-- for example the author's comments do not reflect an understanding of
this at all. But I've no clue why someone would consider such code
elegant or even useful. Seems like chaotic or unexpected behavior to
me.

Can anyone give me an example where code like this is beneficial?

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.

Peter Michaux
Guest
 
Posts: n/a
#2: Dec 9 '07

re: Where are these odd comparison usages useful?


On Dec 8, 10:00 pm, "-Lost" <maventheextrawo...@techie.comwrote:
Quote:
I read on a blog where someone was baffled by the outcome of code such
as:
>
if (a == b == 0) { /* if a AND b == 0, execute */ }
if (a && b == 0) { /* if a AND b == 0, execute */ }
Those /**/ comments are not great.

var a = 0;
var b = 0;

if (a == b == 0) {
alert('first'); // doesn't execute
}

if (a && b == 0) {
alert('second'); // doesn't execute
}

var a = 1;
var b = 0;

if (a == b == 0) {
alert('first'); // does execute
}

if (a && b == 0) {
alert('second'); // does execute
}

Quote:
It just depends on how you evaluate this code as to what the outcome is
You don't choose how to evaluate the code. Hopefully, in any
particular host, the ECMAScript standard has specified the outcome.

Quote:
-- for example the author's comments do not reflect an understanding of
this at all. But I've no clue why someone would consider such code
elegant or even useful. Seems like chaotic or unexpected behavior to
me.
>
Can anyone give me an example where code like this is beneficial?
I would say the first code is a bad practice. The second code could be
written perhaps more clearly as the following.

if (a && (b == 0)) {/* ... */}

--
Peter
Code Worth Recommending Project
http://cljs.michaux.ca

AKS
Guest
 
Posts: n/a
#3: Dec 9 '07

re: Where are these odd comparison usages useful?


On 9 ΔΕΛ, 11:00, "-Lost" <maventheextrawo...@techie.comwrote:
Quote:
>But I've no clue why someone would consider such code
elegant or even useful. Seems like chaotic or unexpected behavior >to me.
I was writing in the same way until I'd been explained (and I got it
right) that it 's too bad.
So the reason is an absence of experience


-Lost
Guest
 
Posts: n/a
#4: Dec 10 '07

re: Where are these odd comparison usages useful?


Response to Peter Michaux <petermichaux@gmail.com>:
Quote:
Quote:
>It just depends on how you evaluate this code as to what the
>outcome is
>
You don't choose how to evaluate the code. Hopefully, in any
particular host, the ECMAScript standard has specified the
outcome.
That's my inexperience showing in the way in which I word things.

I meant the outcome was dependent on the values of "a" and "b."

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Closed Thread