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.
- 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.
- 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.