Are Java 8 lambdas and streams bringing more benefits beyond conciseness?

5

The only benefits I realize in the lambdas and streams of Java 8 are code savings and, as the case may be, better express the author's intent. Is that all?

Is there any example code that uses one of these features and is it better not only to make the code shorter compared to the pre-Java 8 code?

Is there any extra benefit of leaving the code less plastered or easier to maintain or extend?

    
asked by anonymous 23.07.2017 / 16:32

1 answer

10

More benefits to what? In creating whole classes to simulate the same result? After all, lambda uses the class infrastructure to work. So conciseness is probably the biggest gain. And this is no small thing.

I would say that along with conciseness comes the simplification of the code, it makes everything be defined in one place only. And simplification helps you avoid mistakes that could be made if you had to do everything at hand.

The use of anonymous classes helps at least get everything where it's being consumed, but it's still too much code, too illegible, lost in the stream.

Stating the intention better is another very important point. And this is no small thing. Do you want more than that?

The lambda itself does not give more power or flexibility because everything can be done without it.

That's the thing, if you do not want concision, simplification, and robustness, then program it at Assembly :) Everything in high-level languages is about conciseness, simplification, robustness, and giving the author the opportunity to state his or her intention better .

Functional style

Its use changes the style of programming and standards adopted somewhat. It allows for better abstractions without complications. The code tends to be more declarative and less imperative . Java was a language that was born a bit defending that the imperative is not good, only that the declarative is what opposes the imperative, not the orientation to object. Functional Programming is more concise and lambda is a fundamental concept of this paradigm. This is a significant gain for expressivity and legibility, but it has to do with intent.

Where it is used

An example is that you do not need to create a new class to customize a behavior, as long as the original class supports it the object itself can customize what to do in a given situation. At this point the lambda can function as a virtual method. In fact the mechanism is identical. It is you have a pointer to a function that will still be defined in the object, but the lambda allows the consumer to do what they want, but is equal to a virtual method. This can be good or bad, not always what you want.

Another point is when you need to call a method in a class that has no direct relation to inheritance, how do you do that? Normally you create a class that can be abstract or interface that holds a contract for this method and a concrete class derived from it is created to be consumed by another class that expects the class or base interface, then it knows that it can call the desired method that it will exist in this class. Usually a lot of complication, lots of code to get the desired result.

Imagine having to create a class every time you need a stream (I hate that name, it confuses something else, even though it's the same concept). Certainly this is a huge beneficiary. This allows you to create events and simplify the creation of observables . What was even the fuse for the creation of C #. I always knew that one day Java would have it in order to survive, there was an arm drop by egos in the case.

Other winnings

I could say that the better abstraction is an extra gain not quoted in the question, but in fact it could always do. People did not do it because it was not concise. And a little bit too because they usually follow cake recipes and before lambdas rarely someone else did a recipe showing how to do it this way. That's why I always tell everyone to learn all concepts, fundamentals, paradigms, learn "strange" languages just to see different ways of doing what you want.

So I do not even know if there is so much gain in the expression of intention, this could always do, the conciseness is what encouraged to do better.

So if the gain is basically known to the AP and it should know codes that give more conciseness and better intent, then I have no code examples to post.

    
23.07.2017 / 17:19