Do Java 8 streams and streams bring more benefits than concision?

Asked

Viewed 586 times

7

The only benefits I understand in Amble and streams Java 8 is code saving and, as the case may be, better expressing the author’s intention. That’s all?

Is there any example of code that uses one of these features and is better not only in the sense of making the code shorter, compared to pre-Jjava 8 code?

Some extra benefit like making code less plastered or easier to maintain or extensible?

1 answer

12


More benefits compared to what? In creating entire classes to simulate the same result? After all the lambda uses class infrastructure to work. So conciseness is probably the biggest gain. And that’s no mean feat.

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

The use of anonymous classes helps a little at least to stay where everything is being consumed, but it is still too much code, unreadable, lost in the flow.

Declaring the intention better is another very important point. And that’s no small thing. You want more than that?

To lambda in itself gives no more power or flexibility because everything can be done without it.

It’s that thing, if you don’t want concision, simplification and robustness, then program it in Assembly :) All there is in high-level languages is about conciseness, simplification, robustness and giving the opportunity for the author to better declare his intention.

Functional style

Its use does change somewhat the programming style and standards adopted. 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 little arguing that the imperative is not good, only that the declarative is what counters the imperative, not the object orientation. Functional programming is more concise and lambda is a fundamental concept of this paradigm. This is a significant gain for expressiveness and readability, but it has to do with the intention.

Where it is used

An example is that you do not need to create a new class to customize a behavior, since the original class supports that the object itself can customize what to do in a given situation. At that 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, only the lambda allows the consumer to do what he wants, but it is equal to a virtual method. This can be good or bad, it is not always what is desired.

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? Typically a class is created 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 base class or interface, then she knows she can call the desired method that it will exist in this class. Usually it’s too much complication, too much code to get the desired result.

Imagine having to create a class every time you need one stream (I hate that name, confuses it with something else, despite being the same concept). Certainly this is a huge beneficiary. With this you can create events and simplify the creation of observable. Which was even the trigger for the creation of C#. I always knew that one day Java would have it to be able to survive, there was an arm wrestle for egos in the case.

Other gains

I could say that better abstraction is an extra gain not mentioned in the question, but actually it always might do. People didn’t do it because it wasn’t concise. And a little too because, in general, they follow cake recipes and before the Amble seldom did anyone make 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 don’t even know if there’s so much gain in the expression of intention, it could always do, the concision is that it encouraged to do better.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.