4

Code will speak more plainly than I will:

var candidateIndex = 0;
var minValue = Number.MAX_VALUE;
topArray.every(function(element, index) {
    if (element.innerArray && element.innerArray.length < minValue) {
        minValue = element.innerArray.length;
        candidateIndex = index;
        if (minValue == 0) {
            return false;
        }
    }
    return true;
});

// ... use minValue and candidateIndex

What this is doing is going through the topArray, and finding either the first member of that array that has an innerArray of length 0, otherwise finding the one that has the smallest length innerArray. It's working fine, but the checker that I have accurately reports "Mutable variable is accessible from closure."

I see that that's usually a bad thing, particularly with asynchronous code. I've looked through How to avoid access mutable variable from closure and accessing mutable variable in an event closure, and understand that in those cases, the anonymous function is asynchronous, and it's desirable to store the state of the mutable variable at the time, but in my case, I want the synchronous anonymous function I invoke to change the variable.

In this case, the warning that I'm getting is wrong, and I should just ignore it, right? Outside of using a for loop instead of every, is there any way to get the functionality I want without the warning occuring?

Update: for what it's worth, the warning does seem to be coming from my WebStorm IDE itself, instead of any of the analysis tool plugins.

Community
  • 1
  • 1
Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
  • 1
    What tool is reporting that issue? – Bergi Dec 12 '13 at 00:59
  • Would you still receive the warning if you stored those tracking variables on the function itself? And is `this` set to the function before calling it inside `every`? That seems like a lot of confusing code to avoid a warning. My take: ignore the warning. – David Harkness Dec 12 '13 at 01:01
  • 1
    @Scott: Hm, actually I'm getting that message from neither tool (in their online versions). Could you investigate, please? – Bergi Dec 12 '13 at 01:08
  • 1
    ^^ I just get "Use === to compare to zero", in `minValue == 0`; good advice, but no other warnings. – elclanrs Dec 12 '13 at 01:09
  • 1
    @elclanrs Same here. I suspect the warning is from WebStorm. – David Harkness Dec 12 '13 at 01:10
  • @David I'm all for ignoring the warning, but wanted a sanity check before saying "meh. It's a false positive." – Scott Mermelstein Dec 12 '13 at 01:11
  • @David that's entirely possible, and would explain why I didn't see any other tool name while looking into it. :-) – Scott Mermelstein Dec 12 '13 at 01:14

1 Answers1

1

Having gotten confirmation from the comments above that this warning is basically a false positive, I modified the code to ignore the warning message:

topArray.every(function(element, index) {
    //noinspection JSReferencingMutableVariableFromClosure
    if (element.innerArray && element.innerArray.length < minValue) {
        minValue = element.innerArray.length;
        candidateIndex = index;
        //noinspection JSReferencingMutableVariableFromClosure
        if (minValue == 0) {
            return false;
        }
    }
    return true;
});

(The warning only triggers when comparing the value, as opposed to when setting it.)

I'm eager to hear any other answers, but if I don't, I'll accept this answer in about a week.

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76