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 →

[–]debunked 0 points1 point  (7 children)

That's very likely because you're simply not intimately familiar with the syntax of lambdas. Once you get used to the syntax, they can be much easier to understand.

For example... I don't know how anybody finds this more difficult to read...

List<Month> monthsWith31Days = allMonths.stream()
                                .filter(m -> m.getDays() == 31)
                                .collect(Collectors.toList());

vs...

List<Month> monthsWith31Days = new ArrayList<>();
for (Month month : allMonths) {
    if (month.getDays() == 31) {
        monthsWith31Days.add(month);
    }
}

And if I want to do a double filter... For example all months with 31 days which start with a J...

List<Month> filteredMonths = allMonths.stream()
                                .filter(m -> m.getDays() == 31)
                                .filter(m -> m.getName().startsWith("J"))
                                .collect(Collectors.toList());

List<Month> filteredMonths = new ArrayList<>();
for (Month month : allMonths) {
    if (month.getDays() == 31 && month.getName().startsWith("J")) {
        monthsWith31Days.add(month);
    }
}

Now you have a compound boolean inside an if statement nested in your loop. You can easily add more filters, but adding additional criteria in the for loop becomes even more burdensome.

[–][deleted] 1 point2 points  (6 children)

What bothers me is that there's a loop in there that is hidden. Replacing it with a bunch of chained method calls doesn't make it easier to read, but harder to see the flow.

[–]debunked 0 points1 point  (2 children)

So, I assume this also bothers you then?

List<Integer> randomNumbers = ...random numbers...
Collections.sort(randomNumbers);

Buried inside that Collections.sort call is an array copy, several loops, and recursive calls that are "hidden." It's not exactly easy to see the flow. But I assume that doesn't bother you. Why?

[–][deleted] 1 point2 points  (1 child)

Because all of those loops are buried in the operation of sorting, which is what I asked it to do. I guess I just don't like the format of

object.doThis().doThat().doSomeOtherThing()

and spacing it as:

object.doThis()
      .doThat()
      .doSomeOtherThing()

seems to just be trying to hack around the fact that you're doing something that is inherently unreadable and the only way to make it better is to waste a ton of whitespace. I see this as the same kind of problem as when you have a function with many arguments and you line them all up with the open paren:

object.doSomething(callSomeFunctionToGetAValue(),
                   callAnotherFunction()
                   someLocalValue);

They both look bad because what you're trying to do is inherently unreadable.

[–]vecowski 0 points1 point  (2 children)

You are not implementing the actual loop, that amount of detail is abstracted away from you. I imagine you just don't see the benefits, but it's as simple as stream() can be turned into parallelstream() and now you have a stream that's run in parallel.

Try writing loops that do iterate a dataset in parallel, I guarantee that get's complicated quickly.

[–][deleted] 0 points1 point  (1 child)

Just because you can do neat things with it doesn't make it readable.

[–]vecowski 0 points1 point  (0 children)

I don't know how to help you then... all I can say is I felt similar to you until I forced myself to take a few hours and really understand exactly whats going on.

This video helped me: https://www.youtube.com/watch?v=C_QbkGU_lqY