Pretty Code #1 – Building SelectListItems

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?

Print | posted @ Friday, July 03, 2009 1:11 AM

Comments on this entry:

Gravatar # re: Pretty Code #1 – Building SelectListItems
by Joshua at 7/3/2009 1:59 AM

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.
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by Stan at 7/3/2009 2:15 AM

Think I'll go with #3 on this one. Though, it might depend on the context of where the code is used.
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by Jason Jarrett at 7/3/2009 5:52 AM

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
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by Mikkel Ovesen at 7/3/2009 8:32 AM

I like #2 and #3 the best (in that order).

I know #1 is very compact... but I hate magic strings.
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by Mark Heath at 7/3/2009 8:40 AM

from your list I select #3
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by Christer Nyberg at 7/3/2009 9:43 AM

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.
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by Kim Johansson at 7/3/2009 11:33 AM

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());
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by Jaco Pretorius at 7/3/2009 12:12 PM

My vote is #3 - if you have to use 6 lines to display a single select your pages are gonna get loooong....
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by BengtBe at 7/3/2009 5:07 PM

Vote up for #3
Vote down for #1 because of magic strings
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by Eber Irigoyen at 7/3/2009 6:25 PM

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
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by Patrik Hägne at 7/3/2009 6:30 PM

Vote up for #3.
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by Ben Taylor at 7/4/2009 2:06 PM

+1 for #3.
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by scott at 7/4/2009 2:47 PM

Hmmm - It's looking like I'm in the minority here. Where is the love for #2? :)
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by Rik Hemsley at 7/4/2009 3:52 PM

#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.
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by Paul Batum at 7/4/2009 11:49 PM

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.
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by Lea Cohen at 7/5/2009 6:29 AM

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.
  
Gravatar # re: Pretty Code #1 – Building SelectListItems
by Hoghweed at 7/11/2009 12:01 AM

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 # re: Pretty Code #1 – Building SelectListItems
by Mohammad Azam at 10/29/2009 11:47 PM

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 have been closed on this topic.