Configuring Google Style Guide for Java for IntelliJ
By: Reed Odeneal
Tags: Programming, Java, Coding, Intellij

A case for style guides

Tabs or spaces? Single or double quotes? As a developer, your time is more wisely spent learning and knowing about what the code is doing, not trying to figure out idiosyncrasies of how the previous engineer (or you, six+ months ago) liked to style things. Enter: a style guide.

A style guide is a set of conventions and best practices about how to write code. Not only will following a style guide help you write clean, consistent, and easy to read code but enforcing uniformity makes it easier to spot errors, make changes to your code in the future, and allows teams to perform better code reviews.

Many languages have an official style guide such as Golang’s Effective Go and most others have a widely accepted style guide that you and your development team can implement. Where I work, we follow the Google Style Guide for our Java projects which is published in Google’s styleguide repository.

Link: Google Style Guide for Java

Automating your style

You can take your style guide implementation a step further and automate your checks in your IDE. You’ll gain the benefit of making it easier to adhere to your team’s accepted style guide by automatically highlighting deviations from the guide as you write code. Most IDEs support the enforcement of a style guide with automated checks and refactoring features. Here we’re going to look at how to configure JetBrains IntelliJ for Google Style Guide for Java.

Download the intellij-java-google-style.xml file from the google/styleguide repository here.

Launch IntelliJ and go to the Settings > Preferences… menu and expand the Code Style sub-menu underneath Editor. Here, you will see a list of supported languages. Select Java.

Settings > Preferences… > Editor > Code Style > Java

IntelliJ allows you to configure code style schemes at both the global (IDE) and project levels. You can select if you want to import the guide to the Project or IDE level by selecting the default scheme from the Scheme drop-down menu (in bold). Configuring your style guide scheme at the IDE level will allow you to use your style guide for all new projects making it easier to implement your team’s style guide going forward.

Next to the Scheme drop-down menu select the gear icon then Import Scheme > IntelliJ IDEA code style XML then select the intellij-java-google-style.xml file you downloaded from GitHub. Give the schema a name (or use the default GoogleStyle name from the import). Click OK or Apply for the settings to take effect. Note: You have the option of viewing and adjusting any of the style guide conventions should you or your team decide to do so.

Applying the Google Style Guide in your Java code

Now that you have imported the style guide to IntelliJ you can take advantage of the Code function menu and apply the style guide to your code. You have the option of reformatting an entire package/folder by selecting the package from the navigation menu, the currently open .java file, or a selection of code from within the currently open file by highlighting the code you wish to reformat. Select Code > Reformat Code.

Your code will be reformatted automatically based on the Google Style Guide for Java.

Workflow Tip: Make a habit to Reformat Code before every commit

Conclusion

A style guide is a set of standards that development teams can implement to ensure consistency across projects for how code should be formatted and organized. Many languages maintain official guides while most others have widely accepted standards such as Google Style Guide for Java. Leveraging IntelliJ’s Code Style feature you can import and automatically enforce Google Java Style Guide as you write your code. Letting your IDE work for you frees you up to think more about solving problems and debugging instead of worrying about formatting.