Which Java code style should you use?
Tags: java, google, twitter, codestyle
Published: 2019-10-01 15:27:40

---
title: Which Java code style should you use?
published: true
description: Soo….you are thinking about adding or changing a Java style guide for your team.
tags: java, google, twitter, codestyle
cover_image: https://thepracticaldev.s3.amazonaws.com/i/7xv5cggqzx6gc99l17sy.jpg
---

Soo….you are thinking about adding or changing a Java style guide for your team? Do you stick with the tried-and-true Sun guide? Or is it too outdated now that it is twenty years old? What about Google style? But those two-space indents! Maybe AOSP? Twitter? Could you just accept the default that comes with IntelliJ?

Aaaaaagh!! Too many questions!

## Let's take a step back

You have options. And, in 2019, none of the options widely available really address Java 8+ features like lambdas and streams. You will likely have to choose a foundation and evolve from there. Here are some of the options:

[The Original Sun Java Style Guide](https://www.oracle.com/technetwork/java/codeconvtoc-136057.html)

[Google’s Java Style Guide](https://google.github.io/styleguide/javaguide.html)

[Android Open Source Project (AOSP) Style Guide](https://source.android.com/setup/contribute/code-style)

[Twitter’s Java Style Guide]( https://github.com/twitter/commons/blob/master/src/java/com/twitter/common/styleguide.md)

[The JavaRanch Style Guide](https://javaranch.com/style.jsp)

## Food for thought

![XKCD #1513: Code Quality](https://imgs.xkcd.com/comics/code_quality.png)

Before going through the options and deciding, I think there are three steps you should follow.

### 1. Define your goal

Of course we all want to work on code that is consistent. In which case, any of the options are fine. But what is your actual goal for having a style guide? To share the code outside your team? Maybe outside your company? Or maybe you have multiple goals? 

In my current team, we defined our goal as something like 60% consistency (so mostly we don't care as long as we have a style guide), 30% so that we are consistent with a couple other teams we work closely with, and to allow for our devs and theirs to work across team boundaries fluidly, and 10% for the sake of what tooling is available to support insuring that the code style is followed.

You may have other considerations.

### 2. Consider the environment

By this, I mean consider the places you look at your code. I'm guessing you perform code reviews. How? In Gitlab? That's a pretty good environment for viewing code and you may not care that much which of the styles you choose. 

Do you want to be able to view it in a tool like [FishEye](https://www.atlassian.com/software/fisheye)? Does code ever make it into your Jira cards? What about on mobile? Do you ever look at code there? Does anyone ever open your code in a plain text editor? 

It may be wise to think about these things and consider what long lines might do, or similarly what using up a lot of vertical space could do.

![XKCD 1926: Bad Code](https://imgs.xkcd.com/comics/bad_code.png)

### 3. Consider tooling

Finally, how will you support code style in your project? If you use a build tool like Maven, there are some plug-ins like the [fmt-maven-plugin](https://github.com/coveooss/fmt-maven-plugin) to autoformat code when builds are ran (fmt-maven-plugin uses Google style). 

That may feel a bit extreme. You may be comfortable with just letting your IDE support this, in which case you would want to get a hold of an XML file for the code style settings that you can import into Eclipse or IntelliJ.

It's really up to you.

### Thoughts on lambdas and streams

All that being said, you also probably want to grapple with how to approach Java 8+ features like lambdas and streams. I asked Stuart Marks what Oracle is using. Marks is a _Consulting Member of Technical Staff_ for Oracle and a Java evangelist.

{% twitter 1151550811400314880 %}

Marks (perhaps unsurprisingly) had a lot of thoughts!

{% twitter 1151585112741429248 %}

{% twitter 1151586156347138048 %}

{% twitter 1151587225374564352 %}

{% twitter 1151588240782061568 %}

{% twitter 1151589003721756672 %}

So there you have it. Loads of great advice for modern Java!

Just, whatever you do, please don't get stuck arguing spaces over tabs!

![Scene from Silicon Valley](https://thepracticaldev.s3.amazonaws.com/i/ov61brd32fojfp9xdbb7.gif)