L
-Lost
My professor said NEVER pass by reference. Your opinions please?
I am not quite sure about this thing he said, so I came here for your
advice, opinions, or whatever.
Everything I was told was that reusing memory and/or objects is more
efficient than creating numerous copies of your data (which is what I
thought passing by value did). So can you offer your insight on
this, and on what he said below? Thank you!
"So my take on reference and value is this:
Rule 1)
Never pass by reference unless you have a specific reason to do so
The only real reason to ever do this is when you need to get back
more than 1 value from a function. Keep in mind that by definition,
functions can return AT MOST, one value. So if you need two things
coming out, then pass those variables by reference and make sure the
method is a void method (it just keeps things simpler in my opinion).
Now, why is it bad to pass by reference? Because you're giving
another method direct access to your memory, which is a bit scary.
Think of it, now that method can make any changes it wants to my
variable. Also keep in mind that many times one programmer will write
one method and another will call on it.
I don't know, it's just a bit worrisome at times, that's all."
Thanks again for your time.
I am not quite sure about this thing he said, so I came here for your
advice, opinions, or whatever.
Everything I was told was that reusing memory and/or objects is more
efficient than creating numerous copies of your data (which is what I
thought passing by value did). So can you offer your insight on
this, and on what he said below? Thank you!
"So my take on reference and value is this:
Rule 1)
Never pass by reference unless you have a specific reason to do so
The only real reason to ever do this is when you need to get back
more than 1 value from a function. Keep in mind that by definition,
functions can return AT MOST, one value. So if you need two things
coming out, then pass those variables by reference and make sure the
method is a void method (it just keeps things simpler in my opinion).
Now, why is it bad to pass by reference? Because you're giving
another method direct access to your memory, which is a bit scary.
Think of it, now that method can make any changes it wants to my
variable. Also keep in mind that many times one programmer will write
one method and another will call on it.
I don't know, it's just a bit worrisome at times, that's all."
Thanks again for your time.