Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old December 9th, 2007, 06:05 AM
-Lost
Guest
 
Posts: n/a
Default Where are these odd comparison usages useful?

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.



  #2  
Old December 9th, 2007, 07:15 AM
Peter Michaux
Guest
 
Posts: n/a
Default 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

  #3  
Old December 9th, 2007, 07:45 AM
AKS
Guest
 
Posts: n/a
Default 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


  #4  
Old December 10th, 2007, 04:25 AM
-Lost
Guest
 
Posts: n/a
Default 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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,414 network members.