0

Is it a bad practice to reuse function parameter variables?

const reformat = (phone)=>{
    phone = phone.replace("-", ",")

    //... more operation using phone

    return phone;
}

is there any reason such reusage should be avoided? or is it always safe to continue such usage?

Abraham
  • 12,140
  • 4
  • 56
  • 92
  • depending on what you need to do, regarding to the function argument types – Mister Jojo Sep 15 '21 at 14:09
  • [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – Andreas Sep 15 '21 at 14:10
  • @Andy there are more operations in between. – Abraham Sep 15 '21 at 14:10
  • @MisterJojo, Is there any case this could be dangerous? on any argument type in javascript? – Abraham Sep 15 '21 at 14:11
  • That variable is declared by and exists only within that function, I see no reason why that function can't assign values to it as it sees fit. Whether or not this makes the intent of the code less clear is a matter of opinion. – David Sep 15 '21 at 14:11
  • dangerous ? somebody could be killed ? – Mister Jojo Sep 15 '21 at 14:12
  • @MisterJojo, I won't be using it for the surgery robot I am writing in javascript. I meant unexpected results... – Abraham Sep 15 '21 at 14:13
  • @David, It's because I am still puzzled by a random error here https://stackoverflow.com/questions/68644126/strange-javascript-variable-re-assignment-issue still traumatized by that, not to reassign value to parameters – Abraham Sep 15 '21 at 14:16
  • @David, But Both are assigning `data[0]` to the holder variable – Abraham Sep 15 '21 at 14:19
  • 1
    @Abraham: In that other example the re-use of the same variable definitely made the code confusing at a glance. But without a more complete example demonstrating the problem, one might only be able to guess. A [mcve] would really help here. Because it sounds like *this* question is based mostly on assumptions resulting from that question, rather than specific debugging observations. – David Sep 15 '21 at 14:20
  • @David Ok, Thank you. I should improve that. But beyond the confusion, based on your current understanding, they should bring the same result, right? – Abraham Sep 15 '21 at 14:23
  • 1
    @Abraham: Unless you have an example demonstrating otherwise, [yes](https://jsfiddle.net/9mr7j52d/). – David Sep 15 '21 at 14:26
  • Thank you, I will improve those – Abraham Sep 15 '21 at 14:32

3 Answers3

1

For strings, like in your example, it's fine. Some people may think it could get confusing if you're trying to debug and log that variable to the console sometime after it's been changed, but that's mostly subjective. Use your best judgement.

For objects, which are always passed by reference, you should avoid making changes in the function because those changes will still be present outside the function as well.

E.G.:

var myObject = {
  message: "Hello, world"
};

function alertMessage(msgObj){
  msgObj.message = "Hello moto";
  alert(msgObj.message);
}

alertMessage(myObject);

// The object has changed.
console.log(myObject);
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • how about total reasignment, would that be always safe? – Abraham Sep 15 '21 at 14:17
  • 1
    reassigning the variable name to something completely different will cause you to completely lose the reference to whatever was originally assigned to that variable name, but it will not change the variable outside of the function. Really depends on what you consider "safe." – I wrestled a bear once. Sep 15 '21 at 14:21
  • Yes, I agree it could be confusing. But logically, it shouldn't make any different result, right? – Abraham Sep 15 '21 at 14:25
  • Completely reassigning the variable will not make any difference outside the function, no. But any modifications of any objects or anything that inherits from an object (like an Array), will make a difference outside of the function. – I wrestled a bear once. Sep 15 '21 at 14:29
1

No it's not. And it's memory saving sometimes, since when you use another new variable, it will take up your memory. And the function process is much longer many transformations then it will be not a good practice to do. So according to your questions,

  1. Yes, you can use the same variable inside the function (But make sure to consider what are the types that you refer when passing as arguments)
  2. Re-usage should be avoided when you are using a object and that will change the content of the origin object.
Nimna Perera
  • 1,015
  • 10
  • 16
  • incase of an object, if you modify its content like `objectVar.name = "name"`, it could affect the others. But what if you do a total reassignment like `objectVar = {name:"name"}` why would it be a problem? – Abraham Sep 15 '21 at 14:21
  • Total reassignment will not affect to the original object @Abraham. Only the properties reassignment will affect. – Nimna Perera Sep 15 '21 at 15:37
1

different cases to take care:

// argument is not a reference ( string, numbers )
function add_5(num)
  {
  num += 5
  return num
  }

let initial_num = 10
let returned_num = add_5(initial_num )
 
console.log( `initial_num-> ${initial_num}, returned_num-> ${returned_num}`)


// argument is  reference ( objects )
function directArgObj( obj)
  {
  obj.num += 5
  }

let initial_obj = { num: 10, abc:'xyz' }

directArgObj(initial_obj )

console.log( `initial_obj-> ${JSON.stringify(initial_obj)} `)
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40