Why use functional programming, lambda, and generics, in Java
<p>The Java programming language is famous for being Object Oriented, and infamous for being verbose and full of checked exceptions. When the Java language designers introduced Functional Programming abilities into the language with version 1.8, it wasn’t immediately understood how that would help programmers.</p>
<p>Today I bring an example that shows it can be used to improve reusability and clarity. As with anything O.O., the limit is bad abstractions.</p>
<h1>The problem</h1>
<p>A communication client programmed by a third party uses dependency injection and annotations to implement functionality. It throws checked exceptions, has no logging, and has no retry ability. My program needs to wrap calls to that client’s functionality as to add retry ability, logging, and handle exceptions nicely.</p>
<p>Without functional programming, my program would add a facade, wrapping each of the client’s functionalities in a delegate, that performs logging, exception handling, and communications retrying.</p>
<p>The client has 50 functions that need wrapping. That would result in 50 copies of almost exactly the same coding, with the difference being the client function invoked and the data type passed through. That is a lot of duplicate code.</p>
<h1>Functional Programming to the rescue!</h1>
<p>The solution is to add 1 or 2 new methods dedicated to handling exceptions, perfom logging, and implement a retry loop. However, they need to call a specific function in the communication client. How do you solve that?</p>
<p>With Functional Programming, a method can receive a function as its argument. The method does not need to know, at design time, which function. All that is required by Java (because this differs from one programming language to the next), is that the function has the expected signature.</p>
<p>In our case, we needed 2 variations: a BiFunction signature and a BiConsumer signature. The difference is that BiFunction returns a value, and BiConsumer doesn’t.</p>
<p><a href="https://towardsdev.com/why-use-functional-programming-lambda-and-generics-in-java-af19cc19e8d7">Click Here</a></p>