Bookmark and Share

Don’t Instantiate Then Assign

Here’s an example of some bizarre code I’ve seen recently. I’ve changed the variable names, but this is what the developer was doing.

Customer customer = new Customer();
customer = customers[response.CustomerKey];

The first line is completely unnecessary. If you instantiate an object, then assign the object to another instance, the first instance sticks around and eventually gets collected. However, there’s no reference to it. You can never use that instance. Here’s how it should look.

Customer customer = customers[response.CustomerKey];

I would assume that it was dirty code that should have been cleaned before checking into version control, except for the fact the lines were adjacent. For the life of me, I don’t know why anyone would create an unnecessary instance. Any ideas?

blog comments powered by Disqus

KodefuGuru.GetInfo()

Chris Eargle
LinkedIn Twitter Technorati Facebook

Chris Eargle
Telerik Developer Evangelist, C# MVP

JustCode

Telerik .NET Ninja

 

INETA Community Speakers Program

 

MVP - Visual C#

 

Friend of RedGate

World Map

Tag cloud

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.