The answer to this question lies in the essence of the usage of functional programming, the basic or foremost advantage of Functional programming is that it promotes pure functions, a functional approach involves composing the problem as a set of functions to be executed, functions that are self contained and free of side effects, which result is dependent on the passing arguments i.e stateless.
With these two attributes (self-contained and stateless) of function programming, it opens a number of benefits such as :
Increased readability and maintainability. This can only be achieved if the programmer has an understanding of the relative syntax and the specific task. Becuase, each function is designed to accomplish a specific task given its arguments. The function does not rely on the external state and that also makes it easy to refactor.
Easy testing and debugging. Due to the fact that pure functions can be easily tested due to their limited scope, you can write test code that calls the pure functions with typical values.
However, a lot of things are hard to understand with the FP point of view that is why the usage of functional programming is like a syntactic sugar on stuff good programmers have been doing for years. To my understanding, it has been added for some scenarios, its like another tool in your tool box to make an efficient solution to a problem.
Why Lambdas? (Get back to this after tutorial)
It enables functional programming in java. It is elegant in certain scenarios, In object oriented programming all your logic is related to some class or some object. If you have to pass any behavior to a method then you have to create a type that will have the definition of all the behaviors and the instance of that type will be passed into the method argument. For e.g. we have a greet method that need to print greeting according to the situation, one thing we can do is write every possible greeting that can be passed as an argument and write a switch block to handle that request. However, it is not an elegant design what more we can do (in JAVA 7) we can create an interface and abstract all the possible greetings over there and pass an implementation of the type and greets accordingly.
public void greet(GreetInterface greetImpl){
greetImpl.greet();
}
still we are not passing a behavior but we are passing a thing that has a behavior, what if we just pass the action it self to the argument which does not belong to any class and it can be pass as an argument..?
Lambda expression enables you to do this they enable you to pass your function implementation directly in your code.
With these two attributes (self-contained and stateless) of function programming, it opens a number of benefits such as :
Increased readability and maintainability. This can only be achieved if the programmer has an understanding of the relative syntax and the specific task. Becuase, each function is designed to accomplish a specific task given its arguments. The function does not rely on the external state and that also makes it easy to refactor.
Easy testing and debugging. Due to the fact that pure functions can be easily tested due to their limited scope, you can write test code that calls the pure functions with typical values.
However, a lot of things are hard to understand with the FP point of view that is why the usage of functional programming is like a syntactic sugar on stuff good programmers have been doing for years. To my understanding, it has been added for some scenarios, its like another tool in your tool box to make an efficient solution to a problem.
Why Lambdas? (Get back to this after tutorial)
It enables functional programming in java. It is elegant in certain scenarios, In object oriented programming all your logic is related to some class or some object. If you have to pass any behavior to a method then you have to create a type that will have the definition of all the behaviors and the instance of that type will be passed into the method argument. For e.g. we have a greet method that need to print greeting according to the situation, one thing we can do is write every possible greeting that can be passed as an argument and write a switch block to handle that request. However, it is not an elegant design what more we can do (in JAVA 7) we can create an interface and abstract all the possible greetings over there and pass an implementation of the type and greets accordingly.
public void greet(GreetInterface greetImpl){
greetImpl.greet();
}
still we are not passing a behavior but we are passing a thing that has a behavior, what if we just pass the action it self to the argument which does not belong to any class and it can be pass as an argument..?
Lambda expression enables you to do this they enable you to pass your function implementation directly in your code.
References: javabrains.io
No comments:
Post a Comment