From: Thomas 'PointedEars' Lahn on
beegee wrote:

> On Oct 20, 8:16 am, Stevo <n...(a)mail.invalid> wrote:
>> Gregor Kofler wrote:
>> > It's only an alternative in this very particular case. Hence calling it
>> > an "alternative if syntax" (wich implies "general alternative") is
>> > completely wrong.
>> [...]
>>
>> But as an explanationary description to help out someone learning
>> JavaScript, it's a perfectly fine description.
>
> Yes, it's a fine description but one gets the impression that the book
> the op read actually calls it "the alternative if syntax" which as
> stated by Gregor and Thomas, is wrong. The name in this language and
> many others is the ternary statement.

But at least in the languages discussed here (ECMAScript implementations),
the name is _not_ ternary statement, but Conditional Operation.

> Even as a description, "alternative if-else clause" would be better.

That still suggests that a statement and an expression would be equivalent,
which they are not.

> I agree with Pointed
> Ears. Dump the book.

Without the space, please, bee gee ;-)


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
From: dsmithy on
On Oct 20, 9:17 am, beegee <bgul...(a)gmail.com> wrote:
> On Oct 20, 8:16 am, Stevo <n...(a)mail.invalid> wrote:
>
> > Gregor Kofler wrote:
> > > It's only an alternative in this very particular case. Hence calling it
> > > an "alternative if syntax" (wich implies "general alternative") is
> > > completely wrong.
>
> > > Gregor
>
> > But as an explanationary description to help out someone learning
> > JavaScript, it's a perfectly fine description.
>
> Yes, it's a fine description but one gets the impression that the book
> the op read actually calls it "the alternative if syntax" which as
> stated by Gregor and Thomas, is wrong.

Actually it's my mistake in wording my OP -- I used the definite
article "the", incorrectly assuming that this "alternative" (if we can
call it that!) syntax was the *only* alternative to if. I had no basis
for making such an assumption, and, in fact, the book refers to it as
*an* alternative syntax. Furthermore, the book does refer to the
character ? as the ternary operator (without providing further
elaboration).

> Ears.  Dump the book.

The book is called _Object Oriented JavaScript_ by Stoyan Stefanov,
and it's received very favorable reviews on Amazon.com compared to a
lot of the other JavaScript books. I'm not ready to dump it just yet,
but I'm all ears if anyone wants to suggest an alternative. I was
thinking about the books mentioned in the groups FAQ (_JavaScript: The
Definitive Guide_ and _JavaScript The Good Parts_), but I was a bit
concerned they were too advanced for me at this point.
From: SAM on
Le 10/20/09 3:17 PM, beegee a �crit :
>
> res += (i * j) % 8 ? " " : "*";
>
> seems a hellish exercise.
>
> I would write it:

I do not like write indermediate variables used only for cutting things

> var appendStr = ((i * j) % 8 )? " " : "*";
> res += appendStr;
>
>
> I had a friend who loved to write things like:
>
> return userPrefersRed()?userPrefersMaroon()?
> processOrange():
> (userPrefersBurgundy()?
> processBurgundy():
> processRed()):
> processBlue();
>
> His code was strangely bulletproof but impossible to read.

all depends ...
with this particular example that sound to me quite clear

user prefers maroon ? hop! orange
if not, perhaps does he prefers burgundy? so OK this time
if not (ie: yellow), hop! red
default (no choice) is blue.

javascript:alert(!!i?i==3?'orange':(i==2?'burgun':'red'):'blue');

javascript:i=1;alert(!!i?i==3?'orange':(i==2?'burgun':'red'):'blue');

javascript:i=prompt('1, 2, 3 or nothing/empty');
alert(!!i?i==3?'orange':(i==2?'burgun':'red'):'blue');

there, it was a little more difficult to read but not more than :

var coloriz, i=prompt('1, 2, 3 or nothing/empty');
if(typeof i != 'undefined') {
if(i==3) coloriz = 'orange';
else (
if(i==2) coloriz = 'burgundy';
else coloriz = 'red';
}
}
else coloriz = 'blue';
alert(coloriz);

javascript:i=prompt('cancel or 2, 3, nothing/empty');switch(i){case
'3':i='orange';break;case '2':i='burgundy';break;case
'':i='red';break;default:i='blue'};alert(i)

var i=prompt('cancel or 2, 3, nothing/empty');
switch(i){
case '3': i='orange'; break;
case '2': i='burgundy';break;
case '': i='red'; break;
default: i='blue';
};
alert(i);

--
sm
From: Lasse Reichstein Nielsen on
Stevo <no(a)mail.invalid> writes:

> Yes it is. There's nothing at all wrong with referring to a ternary
> statement as "alternative if syntax". It gets across in very simple
> terms what you can do with it. If a regular if looks like this:
>
> if ( x==1 ) y=2; else y=0;
>
> and as a ternary statement it looks like this:

"ternary statement"? The typical mistaken name for the "?:" operator
is "the ternary operator", probably caused by taking a clause in the C
programming language specification out of context.
It is technically correct, even the use of "the", since the "?:"
operator is the only ternary operator (i.e., one that takes three
operands) in the C and JavaScript languages, but it's hardly
discriptive.

A better name for an expression built from the "?:" operator is a
"conditional expression". The operator itself is often called the
"conditional operator".

And it is *not* an *alternative* to "if" statements. It's more like a
parallel: "?:" does for expression what "if" does for statements.

> y = x==1 ? 2 : 0;
>
> That is clearly an alternative if syntax. Stop trying to confuse people.

No, that's a conditional at the expression level, not at the statement
level. The difference is both significant and important.

You can't use the conditional operator as an alternative in this "if"
statement:
if (test) while(true){} else break;
Nor can it easily represent this statement:
if (test) { x = 42; }
Especially the latter is instructive. When one understands why you can't
directly translate it to using "?:", one has, probably, understood the
fundamental difference between expressions and statements.

The conditional expression is no more an alternative to "if" statements
than the undefined value is an alternative to zero, even if they behave
the same in certain situations (e.g., conversion to boolean).

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

From: Osmo Saarikumpu on
optimistx kirjoitti:

>> Stevo wrote:
>> It doesn't take long for people to figure out how unfriendly
>> PointedEars is.

> I share the feelings of the OP.

What exactly was so "unfriendly" or "crude" as the OP put it? I could
not have responded more civilly even if I had three weeks to practice.
In fact, TL's response was not only civil, but also cordial. Go ahead,
read it, parse it and get the correct returned value.

--
Best wishes,
Osmo