HighDots Forums  

Best method to increment a variable.

Javascript JavaScript language (comp.lang.javascript)


Discuss Best method to increment a variable. in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
Evertjan.
 
Posts: n/a

Default Re: Best method to increment a variable. - 12-16-2007 , 01:57 PM






Lee wrote on 16 dec 2007 in comp.lang.javascript:

Quote:
i=++i;

is equivalent to the two lines:

i++;
i=i;

So, yes, it works, but it's silly to do it that way.
Try figuring out [without testing]
what the alerted result of this would be:

var i = 6;
i = i++;
alert(i);

And now of this:

var i = 6;
i = (++i + i--)/2;
alert(i);

And now test it. ;-)

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #12  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Best method to increment a variable. - 12-16-2007 , 02:59 PM






Steve wrote:
Quote:
On Dec 16, 2:03 pm, Doug Miller wrote:
Steve wrote:
Several textbooks, including Javascript for Dummies (2005),
show the "i=++i" method of incrementing a variable.

You sure about that? Or did you mean i = i+1 ?

Yes I'm sure, it is in the table on page 50 of the Dummies
book for example, except they use x instead of i. And it
works too, try it!
snip

The 'it works' criteria only gets you so far. The outcome of - i = ++
i; - is not different from - ++i; -, but the second operation in the
expression mans that the first will take (very fractionally) longer and
if the outcomes are the same that second operation must be redundant.

Remember that books on javascript are more likely to be written by
journalists than programmers, and even if written by programmers they
are unlikely to be good javascript programmers. David Flanagan's
"javascript: The definitive Guide" is, by a very wide margin, the best
(or, more normally, the least bad) book available on javascript, but it
is quite obvious from its text that even its author has little practical
experience of browser scripting (certainly in comparison to his
experience of writing books) and in many cases is just parroting the
assertions of other, less then well informed, sources.

Richard.



Reply With Quote
  #13  
Old   
Dr J R Stockton
 
Posts: n/a

Default Re: Best method to increment a variable. - 12-16-2007 , 03:49 PM



In comp.lang.javascript message <13macj6927jut0f (AT) corp (DOT) supernews.com>,
Sun, 16 Dec 2007 14:12:54, DS <ds (AT) bigpond (DOT) net> posted:
Quote:
On Sun, 16 Dec 2007 02:46:22 -0800, Steve wrote:

Several textbooks, including Javascript for Dummies (2005), show the
"i=++i" method of incrementing a variable. I have been using this method
but I have been recently informed by some experts in the Google Maps API
group that this method is confusing and wasteful of resources and is now
redundant and I should use "++i" or "i=i+1". Any strong opinions in this
group?

Profile your code first! There is absolutely zero point in optimizing
something which may have 1% or less performance degradation on your code.
Instead, work at optimizing your code which consumes most of the CPU. Use
firebug's profiler to determine the functions your code is spending most
of its time.
(1) It is even more important, where applicable, to optimise the
underlying algorithm that the code represents.

(2) While the difference in run speed between i++ ++i i+=1 and i=i+1
will except in artificial cases be negligible, the exercise of
considering and perhaps testing it may well lead to improved
understanding of how the language works.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


Reply With Quote
  #14  
Old   
Steve
 
Posts: n/a

Default Re: Best method to increment a variable. - 12-16-2007 , 04:09 PM



On Dec 16, 8:59 pm, "Richard Cornford" <Rich... (AT) litotes (DOT) demon.co.uk>
wrote:
Quote:
Steve wrote:
On Dec 16, 2:03 pm, Doug Miller wrote:
Steve wrote:
Several textbooks, including Javascript for Dummies (2005),
show the "i=++i" method of incrementing a variable.

You sure about that? Or did you mean i = i+1 ?

Yes I'm sure, it is in the table on page 50 of the Dummies
book for example, except they use x instead of i. And it
works too, try it!

snip

The 'it works' criteria only gets you so far. The outcome of - i = ++
i; - is not different from - ++i; -, but the second operation in the
expression mans that the first will take (very fractionally) longer and
if the outcomes are the same that second operation must be redundant.

Remember that books on javascript are more likely to be written by
journalists than programmers, and even if written by programmers they
are unlikely to be good javascript programmers. David Flanagan's
"javascript: The definitive Guide" is, by a very wide margin, the best
(or, more normally, the least bad) book available on javascript, but it
is quite obvious from its text that even its author has little practical
experience of browser scripting (certainly in comparison to his
experience of writing books) and in many cases is just parroting the
assertions of other, less then well informed, sources.

Richard.
So where did the good javascript programmers learn to program
javascript?

Steve


Reply With Quote
  #15  
Old   
Doug Miller
 
Posts: n/a

Default Re: Best method to increment a variable. - 12-16-2007 , 05:22 PM



In article <c532b8fb-8d7c-47bc-90fb-30e8f959f282 (AT) s8g2000prg (DOT) googlegroups.com>, Steve <stephen.joung (AT) googlemail (DOT) com> wrote:
Quote:
On Dec 16, 2:03 pm, spamb... (AT) milmac (DOT) com (Doug Miller) wrote:
In article
5ecc086d-5d4c-4350-9230-cc04bbcfd... (AT) b1g2000pra (DOT) googlegroups.com>, Steve
stephen.jo... (AT) googlemail (DOT) com> wrote:

Several textbooks, including Javascript for Dummies (2005), show the
"i=++i" method of incrementing a variable.

You sure about that? Or did you mean i = i+1 ?

Yes I'm sure, it is in the table on page 50 of the Dummies book for
example, except they use x instead of i. And it works too, try it!
Oh, I have no doubt that it works. It's clear enough what it does.

But it's also clear that the book *should* be titled "Javascript *By*
Dummies" as
i = ++i;
is equivalent to the obviously silly
++i;
i = i;
and simply ++i; or i++; is sufficient.

--
Regards,
Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.


Reply With Quote
  #16  
Old   
Dr J R Stockton
 
Posts: n/a

Default Re: Best method to increment a variable. - 12-17-2007 , 11:04 AM



In comp.lang.javascript message <eba4d857-4c70-47e7-a3a2-f0f30c56a3ba@e2
3g2000prf.googlegroups.com>, Sun, 16 Dec 2007 13:09:06, Steve
<stephen.joung (AT) googlemail (DOT) com> posted:
Quote:
So where did the good javascript programmers learn to program
javascript?
They will have learned by applying a combination of experience,
intelligence, and suspicion to the available source materials.

Note that an intelligent five-year-old should be able to detect at least
one error in ECMA-262 3rd Edn, which therefore cannot be considered
infallible.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


Reply With Quote
  #17  
Old   
Ron Croonenberg
 
Posts: n/a

Default Re: Best method to increment a variable. - 12-17-2007 , 01:45 PM



but if f is some function,

f(++i) means something different then f(i++)

i+=1; and i=i+1; generate the same code. (or at least should)

i++ is incremented after it comes off the stack. ++i is incremented
before it goes on the stack.

for example:

for (i=0; i<5
document.write(++i);

vs.

for (i=0; i<5
document.write(i++);

Quote:
i=i+1;
i+=1;
i++;
++i;

Reply With Quote
  #18  
Old   
Randy Webb
 
Posts: n/a

Default Re: Best method to increment a variable. - 12-17-2007 , 07:18 PM



Ron Croonenberg said the following on 12/17/2007 1:45 PM:

Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?

Quote:
but if f is some function,

f(++i) means something different then f(i++)
No, what means something different is ++i versus i++, has nothing to do
with whether f is some function or not.

Quote:
i+=1; and i=i+1; generate the same code. (or at least should)
Whether they "generate the same code" or not, I don't really care about.
They produce the same results though.

Quote:
i++ is incremented after it comes off the stack. ++i is incremented
before it goes on the stack.
And it has nothing to do with whether "f is some function" or not.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.