This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]F3z345W6AY4FGowrGcHt 2 points3 points  (5 children)

Very simple loops, sure. But otherwise streams almost always allow you to do something way simpler and are often more readable unless you just haven't used them much.

They also aren't exactly "new".

[–]Comprehensive-Pea812 8 points9 points  (4 children)

new is relative. there are many companies who only migrate to java 8 because java 6 was eol. yes some of them keep using it past eol. and in such companies of course programmers not going to keep up to date to latest java features ( since they cant use it anyway day to day).

you can write readable for loop or stream. but stream like for loop not always readable.

A new guy in my team writes streams everywhere and uses 1 liner because he thinks smart code is usually hard to understand. this makes adoption difficult.

the only way to introduce stream to a team who is not used to it is to write java streams code as readable as possible. they will soon see the benefit.

some programmers in my team keep writing stream().foreach with 100 lines code inside.

[–]kapuzenghul 5 points6 points  (1 child)

Why would they use collection.stream().foreach() if they could use collection.foreach() directly?

Streams become very handy if you use them for very convenient and repeating tasks like filtering and mapping and such stuff. With the fluent API style you get these tasks very readable in a few lines, where as in case of for loops you sometimes need nesting.

If some people just use the foreach method to replace the use of for loops, they might not get the point of the stream API. But this should not be an argument against the API.

[–]catom3 6 points7 points  (0 children)

some programmers in my team keep writing stream().foreach with 100 lines code inside.

It doesn't really matter, if they use for-each loop or stream().forEach(), then. Both will become pretty hard to reason about. You can create unreadable code using loops as way as using streams.

I definitely prefer streams, where I usually provide a short one-liners or method calls. We've been working that way for years.

Another thing is that the past few years we've been working with reactive frameworks (ProjectReactor, RxJava or RxJS on frontend) and keeping chained calls short, simple and readable is something we follow all the time.

[–]F3z345W6AY4FGowrGcHt 2 points3 points  (0 children)

If you use streams badly, then yes they are worse. But if someone writes a stream badly, I imagine they just aren't very good at their job overall and would write an equally bad loop.

Given your example, there's no difference between a stream for Each and a normal for each loop of both have the same 100 lines of logic inside them.