admin管理员组

文章数量:1302984

I'm helping someone on his school assignment - we're trying to write recursive function (if it matters - either in PHP or JavaScript).

I understand principles of recursion quite well, but I haven't wrote any of those from "academic" viewpoint.

Is it good practice to use global variable to store result, something like:

var results = [];

var rec = function(a) {
    ...
    if (match)
        results.push(someValue);
}

Or should I use return for collecting all those results back together (which would be much more difficult)?

I'm helping someone on his school assignment - we're trying to write recursive function (if it matters - either in PHP or JavaScript).

I understand principles of recursion quite well, but I haven't wrote any of those from "academic" viewpoint.

Is it good practice to use global variable to store result, something like:

var results = [];

var rec = function(a) {
    ...
    if (match)
        results.push(someValue);
}

Or should I use return for collecting all those results back together (which would be much more difficult)?

Share Improve this question asked Oct 27, 2014 at 19:41 user1702401user1702401 1,6581 gold badge16 silver badges18 bronze badges 4
  • 1 It would probably be better to not use a global variable and just return an array each time adding an element to the array. – brso05 Commented Oct 27, 2014 at 19:43
  • Thought about this - but that array will get branches (for my given task) and dealing with returns would take biggest part of code. Therefore I got that naughty idea to use global. – user1702401 Commented Oct 27, 2014 at 19:47
  • 1 It is not better to use a global variable. Since arrays are passed by pointer, it's simple to pass the array you are operating on into the function and then just operate on the array passed in. This avoids the global and the inherent disadvantages of globals in Javascript. – jfriend00 Commented Oct 27, 2014 at 19:47
  • If you are using JS and don't want a global variable, you could always wrap it in a closure. – Brian Glaz Commented Oct 27, 2014 at 19:53
Add a ment  | 

2 Answers 2

Reset to default 7

It is good practice to use as little global variables as possible, preferrably none1.

To avoid the need for a global variable in recursion, you can use an inner function that uses a closure:

var rec = function(a) {
    var someValue = [];
    function dorec() {
      // stuff happens
      if (match)
        results.push(someValue);
      }
    }
    dorec();  
}

1 Douglas Crockford states

All variables should be declared before used. JavaScript does not require this, but doing so makes the program easier to read and makes it easier to detect undeclared variables that may bee implied globals. Implied global variables should never be used. Use of global variables should be minimized.

To expand on the existing ments and to give you a concrete example, here is the code for recursively adding the even numbers to the given array:

var match;
var rec = function(a, res) {
    if (a < 0) {
        return res;
    }

    match = a % 2 == 0;

    if (match) {
        res.push(a);
    }
    return rec(a - 1, res);
}

var results = rec(10, []);

alert(results);

and the fiddle: http://jsfiddle/xukukggL/

本文标签: javascriptIs it good practice to use global variables in recursionStack Overflow