Pretty Code #1 – Building SelectListItems

Friday, July 3, 2009

In ASP.NET MVC, you can use a collection of SelectListItems to help build an HTML

Tonight, you’ll be the judge in this first contest of charm, grace, and readability.

Contestant #1 hails from the System.Web.Mvc namespace. It likes pina coladas and string literals, but is turned off by tattoos that look like programming symbols. Let me introduce the SelectList class:

var products = GetProducts();
var selectItems = new SelectList(products, "ID", "Name");

Contestant #2 lives in the System.Linq namespace. It likes whips and method chains. Functional programmers call it “map”, but in .NET we call it "Select":

var selectItems = from product in GetProducts()
                  select new SelectListItem 
                  {
                      Text = product.Name,
                      Value = product.ID.ToString()
                  };

… or (from the backside) …
var selectList = GetProducts().Select(product =>
                    new SelectListItem
                    {
                        Value = product.ID.ToString(),
                        Text = product.Name 
                    });

Contestant #3 lives in the MvcContrib project. It’s turned on by pointy things and practices yoga for extensibility. Introducing the ToSelectList method:

var selectItems = GetProducts().ToSelectList(product => product.ID,
                                             product => product.Name);

Personally, I like #2. While the name of #3 makes its purpose obvious, it sometimes takes a moment to be 100% clear about what property becomes Text, and what property becomes Value. In #2 the Text and Value assignments are obvious, even though the code is a little longer. Setting the Selected properties with either approach is trivial.

What do you think?


Comments
Joshua Friday, July 3, 2009
I prefer the compactness of #1, but lean to #3 as my favorite due to, A) its more compact than #2, and B) the refactoring support and safety of not having to put a property name in a string line in #1.
Stan Friday, July 3, 2009
Think I'll go with #3 on this one. Though, it might depend on the context of where the code is used.
Jason Jarrett Friday, July 3, 2009
I haven't played w/ C# 4.0 yet, but when that comes out I think a combination of contestant #3 with C# Named Parameters would be an elegant solution.

Until then, I'll still go with #3, although less "understandable at a glance" it feels like more essence and less ceremony
Mikkel Ovesen Friday, July 3, 2009
I like #2 and #3 the best (in that order).

I know #1 is very compact... but I hate magic strings.
Mark Heath Friday, July 3, 2009
from your list I select #3
Christer Nyberg Friday, July 3, 2009
I would have to go with #2.

#1 is out because of the magic strings.

#3 would be okay with named parameters, but without them, having to remember the parameter order to know what is what makes #2 superior.
Kim Johansson Friday, July 3, 2009
I lean towards #3 because it can be more compact than #2. As already said, #3 is the choice when C# 4.0 is here.

var items = GetProducts().ToSelectList(Text: p => p.Name, Value: p => p.Id.ToString());
Jaco Pretorius Friday, July 3, 2009
My vote is #3 - if you have to use 6 lines to display a single select your pages are gonna get loooong....
BengtBe Friday, July 3, 2009
Vote up for #3
Vote down for #1 because of magic strings
Eber Irigoyen Friday, July 3, 2009
I would go with #3, I tend to write the shortest legible code, I like the simplicity of #1, but magic strings are the deal breaker, I hate the verbosity of #2
Patrik Hägne Friday, July 3, 2009
Vote up for #3.
Ben Taylor Saturday, July 4, 2009
+1 for #3.
scott Saturday, July 4, 2009
Hmmm - It's looking like I'm in the minority here. Where is the love for #2? :)
Rik Hemsley Saturday, July 4, 2009
#2 wins for me. Reading the code of #3 I can't tell if it's right or wrong. #2 looks like it will probably be correct with just a glance needed.
Paul Batum Saturday, July 4, 2009
Love for #2 right here Scott. I agree with the others that #3 is nice once we have named parameters. Until then, #2 for me.
Lea Cohen Sunday, July 5, 2009
I agree with others before me: #2 is best, because it's very clear to understand what's going on. When named parameters will be used, then #3 will be best because it will be both short and clear. #1 - same problem as said before me: magic strings.
Hoghweed Saturday, July 11, 2009
Personally I love the #3, I hate magic strings, and I like so much c# 3.0 features, I think, why continue to use old coding strategies as till .NET 1.1? sorry for my english
gravatar Mohammad Azam Thursday, October 29, 2009
I was playing around with it just right now and came with the following:

_customers.ToSelectList("FirstName", "Id");

Using ExtensionMethods of course!

Just my 2 cents!



Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!