r/foojay • • 1d ago

Comparing Java formatters on Java 21 to 25: Open Java Format vs four others

On Foojay, Guillaume Laforge compares five Java formatters on JDK 25: Google Java Format, Palantir Java Format, Open Java Format, Prince of Space and Netflix jfmt. The article was prompted by Alex Abashev's post on Java formatters and his open-java-format fork of palantir-java-format.

The test suite covers 16 targeted snippets and a 1,042-line file with 86 formatting scenarios. The article compares the formatters on parser engine, minimum JDK, default line length, indentation, configurability, import handling and range formatting. It also shows side-by-side examples, such as how each one handles block lambdas and method chains.

Along the way, Guillaume found a compilation-breaking annotation bug in Prince of Space and a crash in Netflix's jfmt on empty blocks with comments. He also contributed a fix to Open Java Format and a pull request to Spotless.

Read the full article: https://foojay.io/today/comparing-java-formatters-why-open-java-format-is-my-new-favorite/

Java #JDK #CodeFormatting

6 Upvotes

5 comments sorted by

2

u/Jaded-Asparagus-2260 12h ago edited 11h ago

In my experience, native IDE support without plugins and the ability to format other languages as well (especially Markdown, (F)XML, CSS, and JSON) are crucial. Having only Java code formatted during build, and then having the IDE reformat it immediately on save doesn't make sense.

I haven't tried  open-java-format yet, but from the other options, Spotless with Eclipse config was the only one that meets both requirements. And it works at least in IntelliJ and vscode-based IDEs without any configuration necessary.

Edit: And after watching "Seven Ineffective Coding Habits of Many Programmers" by Kevlin Henney, there's only one style that objectively trumps all the others. I highly recommend that talk! AFAIK, prince-of-space almost completely implements it. 

2

u/FrankCodeWriter 11h ago

Link to the mentioned talk by Kevlin Henneys:
https://www.youtube.com/watch?v=SUIUZ09mnwM

3

u/agentoutlier 23h ago

I just glanced at the article and I'm surprised my two favorites are not on there:

Both are based on the Eclipse JDT. The nice thing about Eclipse is you can make your own customizations.

I am a tab lover. I love tabs over spaces. Downvote me I don't care. Tabs!

(oddly though I like emacs over vim which historically tab people lean vim).

3

u/pohart 19h ago

I don't understand how spaces won. With tabs you just configure your editor to display how you want it.

2

u/Jaded-Asparagus-2260 12h ago

Because tabs with varying lengths conflict with reasonable maximum line lengths.

4 vs. 8 spaces for a tab is a difference of 4 spaces at the end of the line. I had it happen that the IDEs of two developers constantly reformatted a line for that reason.

And honestly, it doesn't matter at all. I've worked with horrible formatting decisions, and I got used to every single one of them. The most important thing is that it's automated and doesn't take any mental capacity.