Warning on Javascript Increment (++) Operator

Questions, tips and tricks and techniques for scripting in Servoy

Warning on Javascript Increment (++) Operator

Postby kwpsd » Wed Sep 12, 2012 10:33 pm

Version: 6.1.2 - build 1421

The following warning is shown:

recordCount..png
recordCount..png (3.14 KiB) Viewed 2793 times

for the code:

Code: Select all
        var countRecords = 0
       
        for ( var i = 1; i <= records.length; i++ )
        {
                ...
            countRecords++
                ...
        }


However, if I change the code to:

Code: Select all
        var countRecords = 0
       
        for ( var i = 1; i <= records.length; i++ )
        {
                ...
            countRecords = countRecords + 1
                ...
        }


no warning is issued. I would think either code style should work. Is this a bug, or am I not understanding something?
Kim W. Premuda
San Diego, CA USA
User avatar
kwpsd
 
Posts: 687
Joined: Sat Jul 28, 2007 6:59 pm
Location: San Diego, CA USA

Re: Warning on Javascript Increment (++) Operator

Postby ptalbot » Wed Sep 12, 2012 10:49 pm

Not a bug, it only means that you are declaring a value, and assigning it, but never use it.
Look for the occurences of this variable AFTER the code you've shown: I bet you never really use it anywhere else, and in the loop it is just useless.

Using a variable means that it assigned to something (on the left hand side of an assignment), or that you are using it for a test for example.

In your case:
Code: Select all
countRecords = countRecords + 1

is a use of the countRecords, but since you are not using it elsewhere, this code does nothing anyway, so you are just masking the warning saying that countRecords is never used, by assigning it to something.
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Warning on Javascript Increment (++) Operator

Postby kwpsd » Wed Sep 12, 2012 11:23 pm

Ah, got it...the warning mechanism is looking for a left-hand side assignment (which the increment operator is not). Thank you, Patrick, for the explanation.
Kim W. Premuda
San Diego, CA USA
User avatar
kwpsd
 
Posts: 687
Joined: Sat Jul 28, 2007 6:59 pm
Location: San Diego, CA USA


Return to Methods

Who is online

Users browsing this forum: No registered users and 6 guests