Configuring Google Style Guide for Java for IntelliJ

Reed Odeneal
Nov 12, 2020 · 4 min read
Image for post
Image for post

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 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 .

Link:

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 repository .

  • 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.
Image for post
Image for post
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.
Image for post
Image for post

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.

Image for post
Image for post

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.

The Startup

Medium's largest active publication, followed by +773K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface.

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox.

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store