VIVO/Vitro Java Style Guide (Adopted 2023, for VIVO/Vitro 1.14.x and above)

As of June 2023, the below VIVO/Vitro Java Style Guide will be enforced on all Pull Requests to the "main" branch for newly created files. Therefore, if a Pull Request to the "main" branch does not align with the below Style Guide, it will fail the build process within our GitHub CI.

  1. 4-space indents. NO TABS ALLOWED.
  2. One true brace style. Braces are required on all blocks.

    if (code) {
      // code
    } else {
      // code
    }


  3. Do not use wildcard imports (e.g. import java.util.*). Duplicated or unused imports are not allowed.
  4. Maximum length of lines is 120 characters (except for long URLs, packages or imports)

  5. No trailing spaces allowed (except in comments)
  6. Tokens should be surrounded by whitespace, e.g. http://checkstyle.sourceforge.net/config_whitespace.html#WhitespaceAround

    // This is NOT valid. Whitespace around tokens is missing
    String []={"one","two","three"}
    
    // This is valid. Whitespace exists around all tokens
    String [] = { "one", "two", "three" }


  7. Each line of code can only include one statement.  This also means each variable declaration must be on its own line, e.g.

    // This is NOT valid. Three variables are declared on one line
    String first = "", second = "", third = "";
    
    // This is valid. Each statement is on its own line
    String first = "";
    String second = "";
    String third = "";


  8. Each source file must contain the required VIVO/Vitro license header, e.g.

    /*
     * This file is distributed under the terms of the license in LICENSE
     */


Resources

IDE Support

Most major IDEs include plugins that support Checkstyle configurations. The plugin usually let you import an existing "checkstyle.xml" configuration to configure your IDE to use and/or validate against that style.

IntelliJ IDEA

(These instructions are based on IntelliJ 2021.2.3.  They should apply to other recent versions, but some menus or settings may have changed.)

Eclipse

These instructions were created using Eclipse 2022-03.

NetBeans

These instructions worked for NetBeans 8.2.

The plugin still works in Apache NetBeans 12.6.  It can be obtained at https://www.sickboy.cz/checkstyle/download.html.  The instructions above should still work.

VSCode

These instructions are for VSCode version 1.63.2.

Checking code style from commandline

If you do not use one of the above IDEs to write your code, you can always verify your code from the command-line using the following Maven command.  

# This checks the code style of all source code under the current directory
# It also ensures all source code has the required license header.
mvn -U checkstyle:check license:check


Fixing existing code / PRs

Use IntelliJ to perform bulk cleanup