If Else format

S

Steve Swift

On page 90 of my O'Reilly "Javascript The definitive guide" 3rd edition
there is an example of an If/Else construct: (some text removed)

If (username != null)
alert("Hello " + username);
else {
username = prompt("What is your name?");
alert("Hello " + username);
}


Should I erase the first ";" from the second line?

It certainly works better in my tests that way, but I don't know if this
is an error in the book, a change in the way JavaScript works, or a flaw
in my browser.
 
E

Erwin Moller

Steve Swift schreef:
On page 90 of my O'Reilly "Javascript The definitive guide" 3rd edition
there is an example of an If/Else construct: (some text removed)

If (username != null)
alert("Hello " + username);
else {
username = prompt("What is your name?");
alert("Hello " + username);
}


Should I erase the first ";" from the second line?

No. Why should you?
It is the end of a command, and for clarities sake, use a semicolon.
It certainly works better in my tests that way, but I don't know if this
is an error in the book, a change in the way JavaScript works, or a flaw
in my browser.

How does it work better without the semicolon???

Erwin Moller



--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
 
J

Jorge

On page 90 of my O'Reilly "Javascript The definitive guide" 3rd edition
there is an example of an If/Else construct: (some text removed)

If (username != null)
   alert("Hello " + username);
else {
   username = prompt("What is your name?");
   alert("Hello " + username);

}

Should I erase the first ";" from the second line?

I'd wrap that alert in curly braces:

If (username != null) {
alert("Hello " + username);
} else {
username = prompt("What is your name?");
alert("Hello " + username);
}
 
S

SAM

Steve Swift a écrit :
On page 90 of my O'Reilly "Javascript The definitive guide" 3rd edition
there is an example of an If/Else construct: (some text removed)

If (username != null)

no ... : if ( blah )
(lowercase)
alert("Hello " + username);
else {
username = prompt("What is your name?");
alert("Hello " + username);
}


Should I erase the first ";" from the second line?

The ';' in end of line is optional in JS


function hello() {
if(typeof username == 'undefined' || username == null)
username = prompt('What is your name?')
alert('Hello ' + username)
}

works as well as :

function hello() {
if(typeof username == 'undefined' || username == null) {
username = prompt('What is your name?');
}
alert('Hello ' + username);
}

But ... it's much better to code correctly.
 
S

Steve Swift

Jorge said:
I'd wrap that alert in curly braces:

If (username != null) {
alert("Hello " + username);
} else {
username = prompt("What is your name?");
alert("Hello " + username);
}

Yes, but those curly braces are redundant in the case of the single
statement above, and in the example. And your ";" is now inside the
curly braces, so terminating the alert() statement. In my O'Reilly book,
it seems to be terminating the "If" statement.

My difficulty is that I don't know if an if/else combination is a single
statement (in which case the ";" shouldn't have been in the example) or
two separate statements (in which case I'm wondering why it doesn't work
in any browser that I've tried).

The evidence is stacked heavily in favour of an error in the book, but
stranger things have happened.
 
E

Evertjan.

Steve Swift wrote on 19 sep 2008 in comp.lang.javascript:
Yes, but those curly braces are redundant in the case of the single
statement above, and in the example. And your ";" is now inside the
curly braces, so terminating the alert() statement. In my O'Reilly
book, it seems to be terminating the "If" statement.

Don't take O'Reilly as a standard.
Perhaps do not use it at all.
My difficulty is that I don't know if an if/else combination is a
single statement (in which case the ";" shouldn't have been in the
example) or two separate statements (in which case I'm wondering why
it doesn't work in any browser that I've tried).

No, the Q is if the

alert("Hello " + username);

is a single statement.
The evidence is stacked heavily in favour of an error in the book, but
stranger things have happened.

No, that is not strange, the archive of this NG reports many such errors.
 
J

Jeremy J Starcher

On page 90 of my O'Reilly "Javascript The definitive guide" 3rd edition
there is an example of an If/Else construct: (some text removed)

If (username != null)
alert("Hello " + username);
else {
username = prompt("What is your name?"); alert("Hello " + username);
}

I think that just about everyone in this group will agree with the
recommendation of: Always use {} in ifs and loops.

Just solves a lot of headaches.

if (username != null)
alert("Hello " + username);
else
username = prompt("What is your name?"); alert("Hello " + username);

is actually the same as:

if (username != null) {
alert("Hello " + username);
} else {
username = prompt("What is your name?");
}
alert("Hello " + username);

and


if (username != null)
alert("Hello " + username)
;
else
username = prompt("What is your name?"); alert("Hello " + username);

is the same as:
if (username != null) {
alert("Hello " + username);
}
;
else {
username = prompt("What is your name?"); alert("Hello " + username);
}
 
E

Evertjan.

Jeremy J Starcher wrote on 20 sep 2008 in comp.lang.javascript:
I think that just about everyone in this group will agree with the
recommendation of: Always use {} in ifs and loops.

I think not.

It depends on the complexity, the need for change by others, and most
important the mood of the programmer, "you" or "me".

Moreover I think "think that just about everyone in this group" is
incorrect, because you did not research it with an open Q,
and even then most will not bother to answer, as it is just their own
business.

Why not simply say you personally recommmend the use of {},
in stead of surmizing uncoroborated facts?
 
J

Jorge

In my personal opinion, the only time when omitting the curlies
is OK is when you're doing something like this:

if (flag_blah) arg += 1;
if (flag_bloh) arg += 2;
if (flag_blim) arg += 4;

Anything more complicated than that - use curlies.(...)

Geekish (?) :

javascript:if (1) alert('1'), alert('2');
javascript:if (0) alert('1'); alert('2');

.... or evil ?

Predictable :

javascript:if (1) { alert('1'), alert('2'); }
javascript:if (0) { alert('1'); alert('2'); }

I always use {}.
 
O

optimistx

Evertjan. said:
Jeremy J Starcher wrote on 20 sep 2008 in comp.lang.javascript:


I think not.

It depends on the complexity, the need for change by others, and most
important the mood of the programmer, "you" or "me".

Moreover I think "think that just about everyone in this group" is
incorrect, because you did not research it with an open Q,
and even then most will not bother to answer, as it is just their own
business.

Why not simply say you personally recommmend the use of {},
in stead of surmizing uncoroborated facts?

You have an interesting opinion.

I believe that Jeremy expressed a useful opinion.

If I say :

1) ' My opinion is A, and I believe the frequency of this
opinion is B % among group G'

then I transmit more information about my opinions than

2) 'My opinion is A'

If we allow beliefs and opinions here, why should we not
allow expressing beliefs about frequencies, probabilities, numbers?

I agree with Jeremy, using common human language,
not syntactically strict
computer language as in some standards:

' I also believe that almost all in
this group think it is good recommendation to use {} in ifs and loops'.

If one puts a socially blind syntax checker robot to find errors in
the above statement, one gets a long list of error messages.
 
J

Jeremy J Starcher

if (username != null)
... but argument here.

Your second example is a syntax error, and thus not the same as the
first. You can omit the curly braces if - and ONLY if - they contain
only a single statement or expression. If you do use the braces, you
cannot insert anything between the closing curly of the if-branch and
"else" (except for comments and an optional "else if" branch, of
course), not even an empty statement like ";".

Ouch, mis-understood the specs on that one.
 
E

Evertjan.

optimistx wrote on 20 sep 2008 in comp.lang.javascript:
You have an interesting opinion.

I believe that Jeremy expressed a useful opinion.

If I say :

1) ' My opinion is A, and I believe the frequency of this
opinion is B % among group G'

then I transmit more information about my opinions than

2) 'My opinion is A'

If we allow beliefs and opinions here, why should we not
allow expressing beliefs about frequencies, probabilities, numbers?

I agree with Jeremy, using common human language,
not syntactically strict
computer language as in some standards:

' I also believe that almost all in
this group think it is good recommendation to use {} in ifs and loops'.

You miss the word "all".

Then he could be wrong,
and he is wrong, in my view.
 
O

optimistx

Evertjan. said:
optimistx wrote on 20 sep 2008 in comp.lang.javascript:


You miss the word "all".

Then he could be wrong,
and he is wrong, in my view.

all? Where? 'about everyone' might not be correct official English, but I
as a foreigner understand that meaning 'almost all'. And how many
percent of all that might be? at least 51%? at least 75 %? at least 90%?

Here people are using 2 'languages':

1) ordinary human language with fuzzy logic and very different
inexact meanings for the words

2) exact language of ECMAScritp 262 with BNF (Bachus-Naur
notation format), formal definitions of key concepts, explicit
strict rules.

Newbies speak language 1) and some experts here might concentrate
to language 2 only, assuming that nobody here should use language 1
at all. They try desperately correct the newbies to use language 2 only,
and sometimes I feel like people are giving their texts to robotlike
syntax checkers: 'you use the language incorrectly, you must use it
like I do, syntax error in your signature, syntax error in your question,
read FAQ, **** your mother when you go away, your existence is
syntax error, FOAD'.

A common man may say 'in my opinion all scientists agree about that'.
A formal syntax checker finds one scientist in a mental hospital
saying 'I do not agree about that', and corrects: 'Common man, you are
wrong, all scientists do not agree about that, I know one. Fool, idiot!'

If it were so amusing to read comments like this it would be very
irritating, especially when those syntax checkers mostly work
admirably correct with THEIR rules.
 
E

Evertjan.

optimistx wrote on 21 sep 2008 in comp.lang.javascript:
all? Where?

.... good recommendation to use {} in ALL ifs and loops' ...

I do not agree with that.
'about everyone' might not be correct official English, but I
as a foreigner understand that meaning 'almost all'. And how many
percent of all that might be? at least 51%? at least 75 %? at least 90%?

You were on the wrong track.
 
O

optimistx

Evertjan. said:
optimistx wrote on 21 sep 2008 in comp.lang.javascript: ....
... good recommendation to use {} in ALL ifs and loops' ...

I do not agree with that.


You were on the wrong track.

Yes, I was, thanks for the correction.

If we consider everyday language expression

'...good recommendation to use {} in all ifs and loops'

we may ask :' if it is not a good recommendation, what is it? Bad?'.

http://jslint.com recommends:

--- start of quote ---
'
Required Blocks
JSLint expects that if and for statements will be made with blocks {that is,
with statements enclosed in braces}.

JavaScript allows an if to be written like this:

if (condition)
statement;That form is known to contribute to mistakes in projects where
many programmers are working on the same code. That is why JSLint expects
the use of a block:

if (condition) {
statements;
}Experience shows that this form is more resilient.'

---end of quote ---

and I believe that this IS a good recommendation and I believe most other
writers in this newsgroup agree with that :).
 
E

Evertjan.

optimistx wrote on 21 sep 2008 in comp.lang.javascript:
Yes, I was, thanks for the correction.

If we consider everyday language expression

'...good recommendation to use {} in all ifs and loops'

we may ask :' if it is not a good recommendation, what is it? Bad?'.

http://jslint.com recommends:

--- start of quote ---
'
Required Blocks
JSLint expects that if and for statements will be made with blocks
{that is, with statements enclosed in braces}.

JavaScript allows an if to be written like this:

if (condition)
statement;That form is known to contribute to mistakes in projects
where
many programmers are working on the same code. That is why JSLint
expects the use of a block:

if (condition) {
statements;
}Experience shows that this form is more resilient.'

---end of quote ---

The idea's or experiences of jslint do not prove anything.

I concede it is more resilient,
but that does not warrant a general advisory rule.

Example:

Not using javascript is very resilient.
It even is a good advice in cases where html only get you as or more
easily to the same end.
and I believe that this IS a good recommendation and I believe most
other writers in this newsgroup agree with that :).

I do not attack your believes. I attack the advice with "always".
I think it is wrong to advice such semantics.

Example:

I love nicely indented code,
but there are situations it does not add to the usefullness,
but instead is even harmfull.
 
G

Gregor Kofler

Evertjan. meinte:
Example:

I love nicely indented code,
but there are situations it does not add to the usefullness,
but instead is even harmfull.

Harmful? When?

And what are the advantages of not using curly braces - apart from
saving say 4 to 5 bytes each time they are omitted?

Gregor
 
E

Erwin Moller

Evertjan. schreef:
optimistx wrote on 21 sep 2008 in comp.lang.javascript:


The idea's or experiences of jslint do not prove anything.

I concede it is more resilient,
but that does not warrant a general advisory rule.

Example:

Not using javascript is very resilient.
It even is a good advice in cases where html only get you as or more
easily to the same end.


I do not attack your believes. I attack the advice with "always".
I think it is wrong to advice such semantics.

Example:

I love nicely indented code,
but there are situations it does not add to the usefullness,
but instead is even harmfull.

Oh come on, this is a nondiscussion.

I'll settle it for you, once and for all ;-)

1) Always use {} for all your logical block (if/then/else, foreach, etc)
2) Always end a command with semicolon ;

That way your code is understandable (syntaxwise) for any other programmer.

IMHO it is a designmistake that {} and ; are allowed to be omitted in
certain situations.

Erwin Moller

--
============================
Erwin Moller
Now dropping all postings from googlegroups.
Why? http://improve-usenet.org/
============================
 
B

beegee

I'll settle it for you, once and for all ;-)

1) Always use {} for all your logical block (if/then/else, foreach, etc)
2) Always end a command with semicolon ;

That way your code is understandable (syntaxwise) for any other programmer.

IMHO it is a designmistake that {} and ; are allowed to be omitted in
certain situations.


Yes, I agree completely. Unfortunately, at my shop, we have tons on
ASP pages ('mixin' of server-side VBScript and client-side JScript)
where not one semicolon can be found.

However, now that we've decided that {} should always be used, we need
to decide where. Should the opening curly bracket be on the same line
as the conditional,function etc. or on the next line?

if (blah) {
}

or

if (blah)
{
}

The readability of the second case is considered superior, however in
some cases it can cause program errors. As Crockford points out in
"Javascript: The Good Parts",

return
{
status: true
};

returns undefined, whereas

return {
status: true
};

returns true.


Bob
 
H

Henry

On Sep 22, 2:34 pm, beegee wrote:
if (blah) {

}

or

if (blah)
{

}

The readability of the second case is considered superior,
<snip>

Not universally. Personally I find the first more readable.

I would want to see any instance on one or the other (and/or the other
variations) accompanied by some genuine (peer reviewed) research into
their relative readabilities. Otherwise it is just personal opinion.

(I also _always_ put braces around if/else, with, while, do and for
blocks regardless of whether they are strictly required.)
 

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,777
Messages
2,569,604
Members
45,224
Latest member
BettieToom

Latest Threads

Top