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.


3 thoughts on “Lambda Expressions, Anonymous Methods and Methods

  1. Keep in mind that the Framework Design Guideline book only addresses public API’s. I don’t know that the use of lambdas could be included in a public API signature.

  2. sorry, lambda expression has not come yet, it is on 3.0 right?
    I also wondering if the framework design guideline book has already updated with a 2.0 version?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s