Reputation: 14379
Is reusing variables a good or bad thing or does it not make a difference?
Should I be doing this:
void someMethod (byte[] arrayOfBytes)
{
byte[] newArrayOfBytes = someOtherMethod(arrayOfBytes);
// ...
}
or this:
void someMethod (byte[] arrayOfBytes)
{
arrayOfBytes = someOtherMethod(arrayOfBytes);
// ...
}
or does it make no difference at all?
Upvotes: 0
Views: 119
Reputation: 5160
If you can do it without getting confused or confusing others then it is a good thing, because your code will require less memory.
Upvotes: 0
Reputation: 12633
I would not lose any sleep on this. The memory cost of the extra reference variable(s) is negligible. I'd prefer the newArrayOfBytes method just for safety - it's not considered great practice to reassign a parameter coming into a method and it may lead to bugs later on, when a developer does not realise the parameter has changed to refer to another Object (you'll see some developers mark the method params as final, to prevent this)
Upvotes: 1