BT

InfoQ Homepage News Google's Java Coding Standards

Google's Java Coding Standards

Leia em português

Bookmarks

Google has recently released their complete definition of coding standards for Java source code. These are hard-and-fast rules that are clearly enforceable, and are followed universally within Google. It covers not only formatting, but other types of conventions and coding standards.

The document is broken up into 6 main sections: Source File Basics, Source File Structure, Formatting, Naming, Programming Practices, and Javadoc. Source File Basics talks about filenames, file encoding, whitespace characters, and special characters. Source File Structure talks about license information, package and import statements, and class member ordering. Formatting talks about braces, indents, line wrapping, whitespace, parentheses, enums, arrays, switch statements, annotations, comments, and modifiers. Naming talks about identifiers (package, class, method, constant, field, local variable, type variable) and defines CamelCase. Programming Practices talks about @Override, exceptions, static members and finalizers. Javadoc talks about how to format Javadoc and where it is required.

Here are a few items contained in the guide.

  • No wildcard imports.
  • Overloads appear sequentially.
  • Braces are used even when the body is empty or contains a single statement.
  • 2 spaces indentation.
  • Column limit can be 80 or 100 characters.
  • No C-style array declarations.
  • The default statement in switch statements are required.
  • Modifiers appear in the order recommended by the Java Language Specification.
  • Constants use CONSTANT_CASE. Note that every constant is a static final field, but not all static final fields are constants.

For further reading, please read the Google Java Style. There is also the official Code Conventions for the Java Programming Language from Oracle. Google also has style guides for other languages like C++, Objective-C, Python, Shell, HTML/CSS, JavaScript, and Lisp.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • 2 Spaces? Rly?

    by Oliver Weiler /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    2 spaces indentation may be useful in dynamically typed languages (Python, Groovy) but Java?

  • Re: 2 Spaces? Rly?

    by Chris Melikian /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Then again they're only allowing 80 or 100 characters per line so those indents can mount up if they're more than 2 characters.

  • Style Validation

    by Suresh Murthy /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    How does google enforce the style guide, do they use the standard static analysis tools such as checkstyle, PMD, FindBugs or do they have they own tools?

  • Re: Style Validation

    by Michael Ahern /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    In eclipse itself you can define shareable code-style rules that are enforced every time you save a file that:
    * Fix indentation
    * Organize imports (to enforce the wild-carding rules).
    * Add braces
    * Etc.

    You can then cover the rest of it using the built-in checkstyles plugins:
    checkstyle.sourceforge.net/
    Or defining their own. Checkstyles has ANT / Maven tasks that can be run as part of a build. The eclipse plugin highlights issues as your code.

    ----

    You would still run findbugs during a policing build to find bugs (obviously), but checkstyles is a good add for your java environment / build setup if you want to enforce a common setting of coding styles across a development team.

    Do also look at the eclipse formatting rules as well as they can be implemented more quickly, while less easy to enforce in a build.

  • Spaces... lol

    by Jonathan Fisher /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Seriously no tabs? This isn't Python lmao.

    Just use tabs people and end this stupid argument. Spaces are for between words.

  • K&R braces considered harmful

    by phloid domino /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Yeah, the 2 spaces are bad enough (someone in Mountain View was smoking crack and washed it down with Python Kool-Aide), but they must really want that code to be completely unreadable with the K&R braces.


    There is one and only one true brace style:

    if (foo)
    {
    bar(); // (should be indented 4 spaces, but the webform loses them)
    }


    Anything else is unreadable, incorrect, communist, and the mark of the beast.

  • Re: K&R braces considered harmful

    by Dmitriy Korobskiy /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    phloid domino,

    K&R ("Egyptian") braces have been part of Sun/Oracle Java conventions since those conventions existed. Hence, the vast majority of Java projects use them. Let's all be friends anyway, OK? :)

    I've been using Sun/Oracle Java conventions on all my projects with very minor tweaks, such as expanding line length limit to 120 characters and banning tabs.

    Thinking of switching to Google Style though. 2 spaces for indents might make more sense after some getting used to.

  • Re: Re: K&R braces considered harmful

    by phloid domino /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    We can all be friends, but K&R braces are still wrong. No matter who says different.

    And the 2 spaces is just brain-dead, even among friends.

    Google got most of it right. But not the 2-space madness and not the obsolete K&R braces.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.