473,413 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Best method to increment a variable.

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?
Dec 16 '07 #1
14 8162
AKS


Steve:
and is now redundant and I should use "++i" or "i=i+1".

I think that -prefix increment operator (++i)- is more effective and
its action are clear enough.
Dec 16 '07 #2
AKS
On 16 ÄÅË, 15:46, Steve <stephen.jo...@googlemail.comwrote:
...and is now redundant and I should use "++i" or "i=i+1".
I think that -prefix increment operator (++i)- is more effective and
its actions are clear enough.

Dec 16 '07 #3
AKS wrote:
On 16 ���, 15:46, Steve <stephen.jo...@googlemail.comwrote:
>...and is now redundant and I should use "++i" or "i=i+1".

I think that -prefix increment operator (++i)- is more effective and
its actions are clear enough.
Dunno about Javascript, but when I examined the output of compiled C for

i=i+1;
i+=1;
i++;
++i;

They all resulted in exactly the same code.

a simple register INC operator..;-)

(or if 'i' was never referenced later, nothing at all. Modern compilers
are very good at eliminating unused variables ;-))

If there was many programmers outputting clean readable code that works,
instead of arguing about the most elegant and efficient constructs, the
world would probably be a better place.
Dec 16 '07 #4
In article <5e**********************************@b1g2000pra.g ooglegroups.com>, Steve <st***********@googlemail.comwrote:
>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 ?
--
Regards,
Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.
Dec 16 '07 #5
On Dec 16, 2:03 pm, spamb...@milmac.com (Doug Miller) wrote:
In article <5ecc086d-5d4c-4350-9230-cc04bbcfd...@b1g2000pra.googlegroups.com>, Steve <stephen.jo...@googlemail.comwrote:
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!

Cheers, Steve.
>
--
Regards,
Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.
Dec 16 '07 #6
Steve wrote:
On Dec 16, 2:03 pm, spamb...@milmac.com (Doug Miller) wrote:
>In article <5ecc086d-5d4c-4350-9230-cc04bbcfd...@b1g2000pra.googlegroups.com>, Steve <stephen.jo...@googlemail.comwrote:
>>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!
Syntactically it makes sense.Increment 'i' *before* assigning its value
to 'i'...

But its unnecessarily verbose.

5 characters, same as i=i+1.

I always prefer i+=1; Four characters.

But i++; is 3, as is ++i;

<shrug>

Cheers, Steve.
>--
Regards,
Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.
Dec 16 '07 #7
DS
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.

Without taking any direct measurements, I would assume i++ and ++i will
be the most efficient when using most javascript engines because the
other expressions will probably introduce temporary variables.
Dec 16 '07 #8
Steve wrote:
On Dec 16, 2:03 pm, spamb...@milmac.com (Doug Miller) wrote:
>In article <5ecc086d-5d4c-4350-9230-cc04bbcfd...@b1g2000pra.googlegroups.com>, Steve <stephen.jo...@googlemail.comwrote:
>>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. [...]
Hopefully you have a shredder.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Dec 16 '07 #9
Lee
Steve said:
>
On Dec 16, 2:03 pm, spamb...@milmac.com (Doug Miller) wrote:
>>In article <5ecc086d-5d4c-4350-9230-cc04bbcfd...@b1g2000pra.googlegroups.com>,
Steve <stephen.jo...@googlemail.comwrote:
>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!

i=++i;

is equivalent to the two lines:

i++;
i=i;

So, yes, it works, but it's silly to do it that way.
--

Dec 16 '07 #10
Lee wrote on 16 dec 2007 in comp.lang.javascript:
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)
Dec 16 '07 #11
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.

Dec 16 '07 #12
On Dec 16, 8:59 pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
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
Dec 16 '07 #13
In comp.lang.javascript message <13*************@corp.supernews.com>,
Sun, 16 Dec 2007 14:12:54, DS <ds@bigpond.netposted:
>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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 16 '07 #14
In comp.lang.javascript message <eba4d857-4c70-47e7-a3a2-f0f30c56a3ba@e2
3g2000prf.googlegroups.com>, Sun, 16 Dec 2007 13:09:06, Steve
<st***********@googlemail.composted:
>
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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 17 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: G. | last post by:
How to increment global xsl:variable? I've daclared variable: <xsl:variable name="my-var" select="0" /> Somewhere else I do something like this: <xsl:variable name="my-var"...
6
by: Anon Email | last post by:
Hi everyone, I've written some code that contains a class, and I want to track the number of instances of this class that have been created, eg.: "This is number xx object created." What is the...
8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
1
by: mike | last post by:
Hello all, any advice or links to pages about how to have multiple language asp pages? For example, you have a database driven website that you want customers around the world to use. How do...
3
by: Paul | last post by:
Hi all. Can someone provide some help on the following as there seems to be many different methods of acheiving the same outcome. Basically I am trying to develop a web service which will...
3
by: George Ter-Saakov | last post by:
What is the purpose of having Interlocked.Increment if it does not work with variable declared as volatile. Here is my problem, Interlocked.Increment increments the variable in thread safe...
1
by: Grant Merwitz | last post by:
Hi We are conducting a test with our website. We are going to redirect every 1 in 10 visitors to a new default homepage to determine the effectiveness of this page. I'm looking for ideas on...
11
by: ryan | last post by:
Hi, I've omitted a large chunk of the code for clarity but the loop below is how I'm calling a delegate function asynchronously. After I start the each call I'm incrementing a counter and then...
21
by: Imran | last post by:
I have a vector of integers, such as and I want to find out the number which occurs most frequently.what is the quick method. My array size is huge. what I am doing is 1. find out the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.