Last month, a co-worker referenced an Eclipse setting that I wish I’ve known about for years. With streams, I typically indent code like:
list.stream().filter(j -> j.disabled) .map(j -> j.name) .forEach(System.out::println);
Then I’d let Eclipse format the code and it would turn into a one (or two) line wall of text. I’ve been “solving” that problem by adding comments before some of the methods so Eclipse couldn’t wrap it. Some of those comments are useful for those new to streams. However many are redundant. You probably don’t need a comment that the last line outputs :).
Then my co-worker taught me that there is an Eclipse setting to control the line breaks. Wish this was on by default!
How to configure
If you don’t already have a custom Eclipse formatter:
- Eclipse preferences
- Java > Code Style > Formatter
- Enter a name
- Click ok
Control line breaks
- Edit the profile
- Line wrapping tab
- Check “Never join already wrapped lines”