R
raylopez99
Hi,
Best practices question.
When receiving an object passed from another method, is it a good idea
to use a shallow copy with a temporary object received on the RHS
(right hand side of =), or to use a instantiated object to receive the
object on the RHS?
Since I may be using the wrong lingo, to put it more concretely.
FIRST WAY:
void somemethod
{
SomeClass X; //declares X as an object but does not instantiate it
X = Y // where Y is a reference passed by another method of type
SomeClass
// work with X within 'somemethod', and when the method 'somemethod'
ends, X is local and will disappear
}
SECOND WAY:
void somemethod
{
SomeClass X = new SomeClass(); // instantiated X
X = Y
// X will also disappear once void somemethod ends
}
Which is "better"? I say FIRST WAY. Which is safer? Perhaps SECOND
WAY, since it's less likely that X is a null reference, though if Y is
null then X can be null, so you have to check for it anyway.
Best practices question.
When receiving an object passed from another method, is it a good idea
to use a shallow copy with a temporary object received on the RHS
(right hand side of =), or to use a instantiated object to receive the
object on the RHS?
Since I may be using the wrong lingo, to put it more concretely.
FIRST WAY:
void somemethod
{
SomeClass X; //declares X as an object but does not instantiate it
X = Y // where Y is a reference passed by another method of type
SomeClass
// work with X within 'somemethod', and when the method 'somemethod'
ends, X is local and will disappear
}
SECOND WAY:
void somemethod
{
SomeClass X = new SomeClass(); // instantiated X
X = Y
// X will also disappear once void somemethod ends
}
Which is "better"? I say FIRST WAY. Which is safer? Perhaps SECOND
WAY, since it's less likely that X is a null reference, though if Y is
null then X can be null, so you have to check for it anyway.