1

I have a question which is more about best practices than actually solving code problems. I know the code works and I've seen somewhat similar questions for other languages but I'd like to get a resolution on this.

var parkRides = [["Birch Bumpers", 40] , ["Pines Plunge", 55] , ["cedar Coaster", 20] , ["Ferris Whell of Firs", 90]];
var fastPassQueue = ["Cedar Coaster","Pines Plunge","Birch Bumpers","Pines Plunge"];
var wantsRide = "Cedar Coaster";
function buildTicket (allRides, passRides, pick ) {
    if(passRides[0] == pick){
        var pass = passRides.shift();
        return function() {alert("Quick! You've got a Fast Pass to " + pass + "!");                          
    };
} else {
    for(var i = 0; i<allRides.length; i++){
        if(allRides[i][0] == pick){
            return function (){alert("A ticket is printing for " + pick + "!\n" + "Your wait time is about " + allRides[i][1] + " minutes.");
                };
            }
        }
    }
}

In the above example the function "buildTicket" takes in 3 parameters;

  • allRides which takes the value of the variable parkRides
  • passRides which takes the value of the variable fastPassQueue
  • pick which takes the value of the variable wantsRide

Through altering the function code I know that

var ticket = buildTicket(parkRides, fastPassQueue, wantsRide);

delivers the exact same results as

var ticket = buildticket(allRides, passRides,pick);

My question is whether one method is preferred over the other? Is it acceptable to code a function to directly use the variable names or is it better to give the parameters different names within the function.

Thanks.

chocofan
  • 57
  • 7
  • Don't expect to get a resolution on this -- it's a stylistic choice with no right or wrong answer. – John Henry Feb 15 '17 at 20:44
  • in fact, people will probably down vote this as begin primary opinion based. Sorry – stackoverfloweth Feb 15 '17 at 20:45
  • 1
    There is a difference. If you're using the same names in the argument list, the global variables are shadowed by the arguments in the scope of `buildTicket`. In your case, if you'd like to change the value of `wantsRide` in the function, the change wouldn't be reflected to the global variable, if it's name is included in the argument list of the function. – Teemu Feb 15 '17 at 20:48
  • ... Or actually any variable in the outer scope of the function is shadowed by its arguments with the same name. Basicly this question is a dup of http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language rather than opinion-based. – Teemu Feb 15 '17 at 21:03
  • if they have the same names, dont pass them as variables... – Jonas Wilms Feb 15 '17 at 21:05
  • being a prototype language, you can consider that javascript is resolved more than declarative. Expressions are solved depending on their context, meaning that it will be slower if you call an undefined reference in a long prototype chain than for a short one. You can take advantage of that and of using same names, see Teemu link for more details. But using same name inside functions is common – Kaddath Feb 15 '17 at 21:08
  • I've to admit that I'm using same names widely. But only when I can be sure that they are not shadowing any outer scope variable. – Teemu Feb 15 '17 at 21:11
  • Ok. So the general consensus is that there no actual error being made here and it's really just a styling choice. I can see the rationale behind actually separating the external variable names from the inputs used within the function. Thanks everyone. Now, can I close this or does a mod have to? – chocofan Feb 15 '17 at 21:17
  • Not an error, but a possibility to a bug quite hard to find. And no need to close the question. – Teemu Feb 15 '17 at 21:22

0 Answers0