Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Note: This blog has moved to omervk.wordpress.com.

Omer van Kloeten's Facebook profile

Omer has been professionally developing applications over the past 8 years, both at the IDF’s IT corps and later at the Sela Technology Center, but has had the programming bug ever since he can remember himself.
As a senior developer at NuConomy, a leading web analytics and advertising startup, he leads a wide range of technologies for its flagship products.

Get Firefox


powered by Dapper 

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

Lambda Expressions, Anonymous Methods and Methods

I really think Krzysztof and friends should post some guidelines in the spirit of The Framework Design Guidelines (read: The Book You Must Read) for lambda expressions, anonymous methods and methods, so there won't be any mixup as to what should be used. I'd hate to see people start writing long anonymous method in the middle of their code - they really disturb the flow.

My suggestion:
  • Do use a lambda expression if it consists of only one expression. This is what they're there for.
  • Avoid using a lambda expression if the types of parameters and return value are not obvious.
  • Do use an anonymous method if it consists of between 1 and 4 lines of code.
  • Consider a method instead if an anonymous method if it looks unreadable in the context of the code around it.
  • Do Not use an anonymous method if the code contained in it needs to be re-used.
And also:
  • Do match the names of parameters from the delegate's declaration when declaring them in a lambda expression or an anonymous method.
Again, this is my opinion and the principles I try to follow in my code. I'd love to hear your ideas too.

I'd also love to see a refactoring feature in Orcas that lets you refactor between lambda expressions, anonymous methods and methods.

Comments

No Comments