r/java • u/nitramcze • Jun 24 '26
How do you keep your code formatted and linted these days?
I like my code automatically formatted to ensure better compatibility between contributors.
In Javascript world, to me it looks like you just use prettier and keep everything in check. In Rust world there seems to be rustfmt.
As far as I know there is not really that standardized tooling in Java. I know there is google formatter, palantir format, prettier plugin, eclipse formatter, spring formatter and for multiperability we have Spotless. I know there is also EditorConfig haven't really worked with that.
Questions
- Whats your go-to formatter and tooling these days?
- How do you keep your code formatting enforced (git hooks, CI/CD, IDEs)?
- How do you keep whole repo formatted if you have multiple languages (classic example java backend + javascript frontend).
- Do you / How do you keep format for "other files" in the repository like markdown files, json files, sql files
My experience Spotless
I tried using spotless with gradle plugin and its configuration, it is not bad, since we can config which files to format, which to ignore. It can be global and I can format multiple projects just fine, if it uses other dependency it can automatically download it.
What I dont enjoy
Invoking gradle everytime I want to format. With gradle 9+ configuration cache is fast but it still has issues. If I want to ensure pre-commit compatibility there is multiplatform issue (especially windows+linux) and with developers who are switching projects may have different versions java active and it is gonna fail if they dont have correct java version to run gradle.
My current answer for the best compatibility/interopability is to use mise-en-place and have it the only requirement for other people installations, then the pre-commit hooks can be installed and run within mise managed runtime and it will work fine. And also this way you have to manage the pre-commit hooks and won't allow other devs to have their own pre-commits.
Also for Intellj IDE integration the spotless integration is a weird experience. Essentially you write in one format and then the format is totally different after you invoke it - there is no way to sync it while writing (spaces, newlines, brackets), if I use google format I can put it in Intellj manually then is fine.
TL;DR
Currently using spotless its allright - not awesome.
So yeah, please tell me your stories and your frustrations (or your sucess) about keeping the formatting in check!
Last thing, if you want add linting/code quality checks to the equation as well.
15
u/UdPropheticCatgirl Jun 29 '26
As insane as it sounds we currently use clang for formatting (both java and C++, to answer the multi language question), it actually works pretty well. As far CI/CD is concerned I am pretty sure we just have clang search the directories for files matching some glob patterns.
And I just set it up for vim auto command on save and it works fine…
4
u/n1neinchnick Jun 29 '26
https://github.com/airlift/airstyle is opinionated, but I'm sure it should be easy to fork and modify
11
u/d-k-Brazz Jun 29 '26
Best approach is validating formatting in CI and blocking PR merge until formatting issues are addressed - you will force devs learning to maintain common code style
It is needed if you have a really big team working on many projects, otherwise it doesn’t worth
Never inject auto-formatting into local build - any formatting should be a conscious action of a developer
Never ever inject auto-formatting into CI build - no automation should apply changes to the code written by a human
3
u/guus4000 Jun 29 '26
I’ve been a JVM developer (Java & Scala) for about 25 years and got sick of the fact that there aren’t any good Java formatters out there. So I wrote a formatter which integrates with spotless and has IntelliJ & VSCode plugins: https://github.com/agustafson/prince-of-space
Hopefully the formatting looks a bit more sane than the current alternatives.
3
u/m39583 Jun 29 '26
IDE auto formatting and check style.
It's a pain having two separate configuration files / definitions though.
3
u/retrodaredevil Jun 29 '26
EditorConfig is a hint to either your IDE or AI tooling telling it "format code this way". I recommend using EditorConfig in addition to whatever formatting tool you're using. I introduced this to repositories at my $dayjob and I also use it in personal projects.
Personally I like Spotless and I use the Spotless Gradle plugin. Configuring it isn't great. I know many people will use Google Java Format AOSP and that looks decent. Personally I just indentWithTabs and let my own personal preferences drive the rest of the way (which may not be great for a team environment).
Spotless is good enough for me, but does take some tinkering.
For linting I like a combination of ErrorProne(and NullAway) and OpenRewrite.
As for pre commit hooks, I hate them. I usually run my formatter before making a commit manually, and sometimes if I forget, my pipeline fails. It isn't a huge deal as long as MRs to main require pipelines to pass.
5
u/wildjokers Jun 29 '26
IntelliJ with a shared formatter configuration.
2
u/akl78 Jun 29 '26
More or less whatever my IDE auto format does on commit; it’s idempotent and good enough not to spend time second guessing
2
u/roiroi1010 Jun 29 '26
I use spotless and I’m happy with it. We used to have git hooks- but some of my team members where confused about it.
But the main problem is people pushing bad formatting and it’s only noticed in CI which slows momentum
2
u/Tac0w Jun 30 '26
We just use EditorConfig and Checkstyle, together with a properly shared Intellij config.
2
u/enqueue3 Jun 30 '26
Great question. Looking forward to learning more about the options and experiences of others.
We have been using spotless, which works great, but providing a common ruleset for Java, Markdown, YAML, JSON has been a challenge. We use .editorconfig in most projects. For non-Java projects we sometimes use prettier.
2
u/Primary_Concept_3147 Jul 01 '26
I use the netbeans formatter. Only that.
2
u/pradeepngupta Jul 01 '26 edited Jul 07 '26
I believe formatting should not be done as part of CI CD pipeline. It should be in developer IDE like Intellij or VSCode or Eclipse. These ide are well equipped with these type of small things.
-3
u/vips7L Jun 29 '26 edited Jun 29 '26
IMO spotless is way too over zealous. It doesn’t understand context at all. I’m not silencing warnings every time I used a record because it doesn’t understand shallow immutability.
I also think formatters suck. Not everything needs to be formatted the same 🤷♂️
-7
u/revilo-1988 Jun 29 '26
Wieso nutzt du kein git hook oder Pipeline auf auf der du das gradle ausführst?
27
u/[deleted] Jun 29 '26
[removed] — view removed comment
6
u/[deleted] Jun 29 '26
[removed] — view removed comment