views:

18

answers:

1

Hello All,

Does the given code samples perform same operation? Do i really need a EXTRA object instantiation code? Will there be any issue with the first code segment?

Sample 1

Dog adog= new Dog();
adog.ID = dogID;
adog.CategoryId= dogCategoryId;

adog= DogRepository.FindDogByCategoryId(adog);

Assign the values back to the same object

Sample 2

Dog adog= new Dog();
adog.ID = dogID;
adog.CategoryId= dogCategoryId;

Dog odog= DogRepository.FindDogByCategoryId(adog);

Assign the values to a different object

+1  A: 

No, you can reuse the original object if you wish. The first code will work just fine.

Provided, that is, that FindDogByCategoryId returns a new Dog object and you no longer need the old adog. Which will be gone forever. So sad to lose a loved pet.

Be careful not to fall victim to overoptimization, which usually occurs when a programmer starts thinking about performance minutia (and the gain here really is minute) before they know what their performance needs are.

Borealid
I am not going to lose my pet as it will reborn :)
Sri Kumar