Consider Switching Java back to 4 Space Indentation
We used to indent our Java code with 4 spaces, but when we added the spotless formatting plugin to our build, it switched to 2 spaces, since it's using googleJavaFormat, and Google's style guidelines dictate 2 spaces for block indentation. But this 2-space Java feels pretty unnatural to me. In my experience with Java, this style of indentation is pretty rare and 4 spaces is pretty standard.
But... to double check myself, I looked at the Github Trending Java Projects. The first half of that page are indeed all using 4 spaces (I didn't check the 2nd half).
- Apache Doris: 4 spaces
- Kubernetes Kafka: 4 spaces
- OpenAI Generator: 4 spaces
- Apache Dolphin Scheduler: 4 spaces
- Stirling PDF: 4 spaces
- Skylot Jadx: 4 spaces
- Keycloak: 4 spaces
Feel free to check around yourselves. These aren't cherry-picked or anything, they're literally just the top 7 results for Github Trending Java Projects (ignoring the Chinese ones I couldn't read).
Also, for good measure, I checked some other big player's Java repositories just to see:
-
Microsoft Java-Debug: 4 spaces
-
Microsoft Malmo AI: 4 spaces
-
MS-SQL Java: 4 spaces _
-
Apple FoundationDB: 8 spaces
-
Apple PKL-core: 2 spaces
-
Apple XCode JDK: 4 spaces _
-
AWS Java SDK: 4 spaces
-
AWS Lambda Java: 4 spaces
-
Amazon ExoPlayer: 2 spaces
-
Amazon Pay Java: 4 spaces _
-
Oracle Tribuo: 4 spaces
-
Oracle Graal: 4 spaces
-
Oracle VisualVM: 4 spaces
-
Oracle FastR: 4 spaces _
-
OpenJDK JDK: 4 spaces
-
OpenJDK Mobile: 4 spaces
-
OpenJDK Mission Control: 8 spaces
OpenJDK, Oracle, Amazon, and Microsoft seem to have clear 4 space standards (with 2 exceptions). Apple doesn't seem to have any consistency, using a mix of 2, 4, and 8 spaces. I didn't bother to check Google, because I assume they're using their own guideline of 2 spaces.
Note that Google's style guidelines are not Java specific. The fact that they use 2 spaces in Java is because they also use 2 spaces in C++ https://google.github.io/styleguide/cppguide.html#Spaces_vs._Tabs
Also, to disavow myself of my biases here: IMO, 4 space indentation is objectively easier to read than 2 spaces spoken as someone who is currently editing large chunks of the Java tests.
I'm fine with 4 or 2, I don't think it makes much of a difference. But I would prefer to keep the Google style default, which is the point of using a style, if we were to start tweaking things here and there we could keep the original style.
I agree that we shouldn't spend time tweaking and modifying the defaults. My thinking was just switching which format-style we were using, but keeping it's defaults intact.
It looks like spotless supports a couple styles if you scroll to the "java" section of this:
- java.GoogleJavaFormatStep
- java.PalantirJavaFormatStep
- java.EclipseJdtFormatterStep
The PalantirJavaFormat style seems like it was specifically made to "fix" the google style, after reading it's README. Maybe they're biased here though.
Also, it looks like spotless has support for other lints apart from just formatting:
- java.RemoveUnusedImportsStep
- java.ImportOrderStep
- java.FormatAnnotationsStep
Should we try enabling any of these? If they work well, they seem like good checks to be performing.
It would be nice to be able to use the very common Language Support for Java(TM) by Red Hat Java extension for formatting in VS Code. It seems to use the eclipse formatter.
https://github.com/redhat-developer/vscode-java/wiki/Formatter-settings
EDIT: This extension also supports the Google format. I'm going to open a PR to configure it for VS Code. EDIT 2: Unfortunately these are not compatible. This recommendation from RedHat is quite bad as the XML they reference is abandoned.
Unfortunately these are not compatible
Like the 2 tools don't format the code in the same way? If so, that's indeed pretty lame.
Tomorrow I plan to at least look at enabling the other linting 'spotless' supports, and maybe checking out the other styles it supports. Assuming no one is working on anything big in Java right now (except me).
Spotless itself uses tabs for indentation with indent-size = 4. https://github.com/diffplug/spotless/blob/04d5f28c0401803323b3e8d82677fc081ab2b818/.editorconfig#L5
(I don't think we should use tabs)
This was implemented in #2734.