Linq code refactoring: good programmer and bad one

IMHO the difference between a good programmer and a bad one is that a good programmer always strives to do things the straight way while a bad programmer feels smart when he discover and use dirty tricks  -  While refactoring long methods with Linq/Linq2Sql code and so trying to extract some Linq/Linq2Sql code into a new method it do emerge the need to return anonymous types from the extracted method. The C# compiler can complains for this


You can Google and find a trick to return anonymous types. While in the first moment it looks interesting by a technological point of view, it do lead to poor quality code (hard to read, error prone) - This is the bad programmer path


It is also possible to declare and use a real type (e.g. a nested class) with just automatic properties and without constructors.  It work fine with Linq and even with Linq2Sql (the Sql execution plan will not change because of the refactoring)   The resulting code is easy to read/understand and is type-safe - This is the good programmer path


This solution come out working in pair and joining Linq/Linq2Sql skills, C# and IL skills and refactoring skills. But this is another story :)


Resharper refactoring: Convert Anonymous to Named Type (C# only) - when refactoring Linq2Sql use only automatic properties and no constructors to avoid changes on Sql execution plan.


Print | posted @ Thursday, April 9, 2009 7:46 PM

Comments have been closed on this topic.