Re: Passing by reference
- From: "Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx>
- Date: Wed, 1 Nov 2006 10:26:16 +0100
On Tue, 31 Oct 2006 07:24:59 -0600, David Veeneman wrote:
Is passing a method argument by reference inherently dangerous?
No. As long as no aliasing issue gets involved, i.e. there is only one
argument. With aliasing copy-in/copy-out is as dangerous as reference-in.
The problem is in aliasing.
I develop in the .NET environment. For a long time, I have avoided what I
consider to be the dangers of passing method arguments by reference, where
the method changes the object argument. The biggest danger, IMHO, is that
you can't tell by looking at the calling object whether the argument was
changed by the method:
MyClass.MyMethod(myObject);
So, at the first step in a method, I have deep-copied any arguments passed
in that the method will modify, then passed the modified object back as a
return value:
myObject = MyClass.MyMethod(myObject);
One of the advantages of this approach is that it forces methods to limit
themselves to doing just one thing.
But now I'm rethinking the approach. Is it too conservative? Is it
necessary? Is it even good practice. Any and all opinions welcome. Thanks
It seems that you are mixing parameter passing (by-reference vs. by-copy)
with mutability (constant vs. mutable).
Parameter passing is semantically transparent. That is - the choice shall
not influence the behavior. If it does, then either the program or the
language is wrong. [Presuming absence of aliasing]
As for functional notation x=f(x) vs. procedural f(x), note that if left
side of assignment is same as the argument of f, then they are semantically
equivalent and you have no gain. This is because x=f(x) is equivalent to
=(x, f(x))
Here = is the assignment. Note that x appears in "=" in mutating form. So
it isn't any better than just
f(x)
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
.
- References:
- Passing by reference
- From: David Veeneman
- Passing by reference
- Prev by Date: object databases
- Next by Date: Re: object databases
- Previous by thread: Re: Passing by reference
- Next by thread: Re: Passing by reference
- Index(es):
Relevant Pages
|