Background

To encourage coding consistency, it makes sense to introduce templates for Eclipse and IntelliJ IDEA so that formatting consistent to the project can be done in a single step. Up to now, files have been manually formatted roughly along a standard that was most logical for when the file was created.

Highlights

The template, as discussed at HELIX-198, is very similar to the stock Eclipse template, with a few changes. Here are the key elements:

  • Maximum line length of 100 characters
  • Indentation with 2 spaces, line wraps are 4 spaces
  • Open brace on the same line as a declaration
  • Line wrapping for arguments is done when necessary
  • Line wrapping for enum constants is one per constant
  • Statements look like: int x = a + b;
  • Conditionals look like: if (condition) ...

In addition, there are various other conventions that are not captured by the template:

  • Camel case where the first character is lowercase
  • Instance variables are prefixed by an underscore ('_')

Editor-Specific Templates

helix-style.xml is available both as an attachment to this page and in the root directory of the git repository.

Eclipse

To import:

  1. Grab helix-style.xml
  2. Go to Window (or Eclipse on Mac) --> Preferences --> Java --> Code Style --> Formatter
  3. Click Import...
  4. Browse to the file and click OK
  5. Make sure "Helix" is selected as the active profile if you plan to use it globally (see Project-Specific Settings otherwise)

To use:

  1. Source --> Format

Project-Specific Settings

If you use Eclipse for multiple projects, it is possible to enable the formatter on a project-specific basis.

  1. Right click the project, select Properties
  2. Go to Java Code Style --> Formatter
  3. Check "Enable project specific settings"
  4. Ensure that "Helix" is selected as the active profile
  5. Go to Java Editor --> Save Actions
  6. Check "Enable project specific settings"
  7. Check "Perform the selected actions on save" and "Format source code"
  8. Select "Format all lines"

IntelliJ IDEA

To import:

  1. Grab helix-style.xml
  2. Go to File --> Settings --> Plugins --> Browse For Repositories...
  3. Search for "Eclipse Code Formatter," right click to install, apply settings, and restart IDEA
  4. Go to File --> Settings --> Eclipse Code Formatter for the Helix project
  5. Select "Use the Eclipse code formatter"
  6. Browse to the format file for "Eclipse Java Formatter config file"
  7. Apply settings

To use:

  1. Code --> Reformat Code

Javadocs

Please include javadocs for every new class and method introduced. A method should have a high level description, descriptions for parameters, and a description for the return value (if any).

Known Issues

The Eclipse formatter adds a single trailing space for empty javadoc lines. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=360523. There are two options: one is to run an additional cleanup to remove all trailing whitespace, and the other is to simply ignore it. We have decided to force the number of blank lines in the javadocs to be minimized.

  • No labels