Home SubSonic MVC-Storefront

My Personal Lambda Crusade

I love lambdas and I’m pretty sure they solve every problem imaginable. Why just the other day my daughter painted her face with leopard spots, using a Sharpie pen, and I said to myself "Rob(x=>x.Beer("immediately"))" and all of a sudden the world was a brighter place…

Lambdas Are A Party In One Line
This was overheard on Twitter today – a conversation between Kevin Dente and Scott Hanselman RE Lambdas and mocking frameworks:

The benefits [of using Lambdas], for me, are two fold, 1 it looks more like I think, 2 it doesn’t require knowledge of the different kinds of mocks

I agree with Scott completely – once you use them a few times, you GET them, and what they can do. And then you kind of go Lambda crazy (where I am now). There’s lots to read about them if you don’t know them very well:

Free Your Mind (or at least Delegate It)
Lambdas allow you to do some cool things – such as pass a reference to a method as an argument – constructing it on the fly. Yes, you can do this with Delegates – but that takes some code. Lambdas allow you to terse that up a bit and also get a bit more free-form.

If I have a method, such as this:

public void ActionLink<T>(Expression<Action<T>> action) where T:Controller{
     ....
}

I’ve created a delegate to handle the passed-in method using the plain Vanilla "Action" delegate that lives in System. I then slap a generic type argument on there, saying that "T" must be a Controller – in that I’ve now restricted the argument to take a Controller Method. This is all the code I had to write to handle this.

The final step is to wrap that with Expression (using System.Linq.Expressions) – this means that you can use a Lambda to specify the delegate and not have to wire up your own.

This is really important – as now you have a very type-safe, descriptive method signature when you call ActionLink<T>:

string link=ActionLink<HomeController>(x=>x.Index());

Note that I don’t have to create a delegate referring to Index() anywhere – this is "on the fly" so to speak and it makes coding a lot more flexible.

I’ve Got Lambda Fever

I’m driving Phil and Eilon crazy. They’re good sports and definitely listen to my input, but then they got me this T-Shirt, which Phil told me was probably causing Stack Overflow in my case:

try_try_again

Hrrmph. Well it’s soft, at the very least.

I’d love to see lambdas everywhere, but that’s me. I know that not everyone’s brain works that way – so I’d love your feedback. Do you agree with Scott? Or are these things really nerdy? Do you find Lambdas helpful or cumbersome?

Technorati Tags:

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DotNetKicks
  • del.icio.us
  • Technorati
  • TwitThis
  • Reddit
  • Slashdot

Tags: