I imagine this comes up a lot. Let's say I have:
downloadData () {
var myData = //do something ;
displayData(myData);
}
displayData (myData) { //<--- is it bad practice to use same variable name myData?
//display data
}
Is it good or bad practice (or doesn't really matter) to use the same variable name in the second function displayData() or should I accept myData with some other name, like myDataToDisplay? Both names tell me what's in the var just as effectively.
The variable
myData
is bounded to the scope of functiondisplayData
whendisplayData
is invoked. So it's not like you're risking the possibility of any name collisions.My general rule of thumb for parameter naming is clarity. So yes, I think it's perfectly fine to use the same name variable names in this case, insofar as they accurately describe the data that is being passed.
As a though experiment, lets suppose we had a hard rule that says "Thou shall not use the same variable names in multiple functions". What if you have multiple functions that call the same data and you're chaining the calls? That won't look pretty.