Enhanced for-Loop vs .forEach() – Which One Is Better?

0
748
Roller Coaster

With Java 8 we got the a new way to iterate over iterable structures: the forEach() method in the Iterable interface. In many ways it looks similar to the familiar enhanced for-loop. But which one is better?

// Enhanced for-loop
for (var person: persons) {
  System.out.println(person.getFirstName());
  System.out.println(person.getLastName());
  System.out.println(person.getAge());
  System.out.println("------");
}

// .forEach() method
persons.forEach(person -> {
  System.out.println(person.getFirstName());
  System.out.println(person.getLastName());
  System.out.println(person.getAge());
  System.out.println("------");
});

Both examples achieve the absolutely same thing. So they can be used interchangeably.

Readability

I argue the enhanced for-loop is easier to read. Here is why:

  • The for keyword at the very start of the first line indicates the start of a loop. IDEs highlight the keyword accordingly. The forEach() method on the other side is burried in the middle of the line – just like any other method call.
  • The body of the enhanced for-loop starts with a curly brace and ends with a curly brace. With that the block is immediately visible for anyone who has spent some time doing C, C#, Java or any similar languague. With forEach() the actual loop body is terminated with a curly brace, a parenthesis, and a semicolon. Which makes it just a little harder to pick up on the fly.
  • The enhanced for-loop – unlike the forEach() method – also works for arrays. In addition it looks very similar to the basic for-loop. So if we stick to the enhanced for-loop, our code looks more consistent. And consistency helps with readability.

Yes, the forEach() method still is absolutely readable. However the enhanced for-loop is just a bit more readable.

Performance

In all realistic cases the choice of loop implementation has no siginficant impact on the overall performance, as the performance will be strictly dominated by the body of the loop. Therefore – as so often – performance should not be a consideration for our choice.

In-Line Use

An argument can be made for the “in-line” use of the forEach() method – as it allows us to get things done neatly in a single line. While I tend to agree with that, I prefer to write my own method in these cases, as I find that easier to read.

// OK
persons.forEach(this::printPerson);

// Even easier to read
printPersons(persons);

Conclusion

Yes, the forEach() method is new and fancy. Yes, it offers the possibility to play around with lambda expressions – another cool new feature. However, in most cases it does not offer any tangible advantages over the good old extended for-loop.

The extended for-loop on the other hand is more readable. Not that the forEach() method would be hard to read. But if I get the opportunity to improve readability “for free”, I will always take it.

As I value readability very highly, the enhanced for-loop is the clear winner for me.