DEV Community

Maximilian Koch
Maximilian Koch

Posted on

IntelliJ google-java-format plugin vs IntelliJ java-google-style.xml - what's the deal?

Code Style. One of those controversial topics, where everyone seems to have an opinion and no one seems to has the silver bullet.

Recently, I was caught up in a discussion on how to format chained methods or in that case Java stream().

Here are the two examples from this discussion:

Option A:

obj.stream()
    .map(...)
    .distinct(...)
    .collect(...);

Option B:

obj.stream().map(...).distinct(...).collect(...);

Personally, I feel like option A is nice to read, but not everyone feels the same way. This is perfectly fine, but it would be nice to have a set of rules and restrictions, which will settle this debate once and for all.

Luckily, we have decided to on a Style Guide, the Google Java Style Guide.
Having a style guide is a nice thing, but just referencing to the style guide if not really enforcing the rules. Furthermore, to make life easy, it is always nice to have some tolling, which will allow to combine the chosen style with IDE features, like auto-format.

For IntelliJ, I see here two options. It's either installing the google-java-format plugin or importing the java-google-style.xml.

One could think that both options are resulting in the same format, but unfortunately this doesn't seem to be the case.

As an example, the plugin would format streams like in the above shown option A, but the imported XML would go with option B.

Furthermore, if you're using Spring, the XML would result in this format for annotations:

@Autowired
private Entity entity; 

Whilst the plugin would format it as following:

@Autowired private Entity entity;

These are just a couple of examples, but there are plenty of other differences. It surprises me as both, the XML and the plugin, are maintained by Google (or at least under their GitHub organisation).

So, this leaves a few open questions:

  • Is this supposed to be like that?
  • Could it be an issue with IntelliJ or the XML that the formatting is so different between the two options?
  • Which one is right or closer to the actual Google Style guide?
  • Is there a different tool, which is reliable for a reliable code formatting for Java?

Personally, I kind of feel that closer to the plugin. This is mostly because it seems to be much tighter and almost leaves no free for different styles. But is it too tight?

Top comments (2)

Collapse
 
jeremycoxbmi profile image
JeremyCoxBMI

I think the plugin gets the final word. Everyone's probably going to use it because it is automated, and it does what it wants to change the code. I came here because I recently saw a minor change in behavior, and I am researching. At the end of the day, it doesn't matter because the plugin has taken the power. Viva la plugin.

Collapse
 
dploeger profile image
Dennis Ploeger

I think, they're different because they're different things.

The xml tries to mimic the Google style guides using IntelliJs built in code formatter and is written by a third party. The plugin comes directly from Google and seems to be a wrapper around the official formatter by Google as there are plugins for other platforms as well.

To be as close as possible to the code style, I'd suggest the plugin over the xml.

I'm used to using Prettier but it seems they have no Java support currently.