Wednesday, February 12, 2014

Why do we need lambda expressions? (Why would we need to write a method without a name?)

Convenience. It's a shorthand that allows you to write a method in the same place you are going to use it. Especially useful in places where a method is being used only once, and the method definition is short. It saves you the effort of declaring and writing a separate method to the containing class.
Benefits:
  1. Reduced typing. No need to specify the name of the function, its return type, and its access modifier. 
  2. When reading the code you don't need to look elsewhere for the method's definition. 
Lambda expressions should be short. A complex definition makes the calling code difficult to read. 

No comments:

Post a Comment