Home

 

Formatter

The formatter preferences in Application Developer are used to ensure that the format of the Java code meets the standard defined for a team of developers working on code. There are three profiles built-in with Application Developer with the option of creating new profiles specific for your project:

Eclipse (default profile on startup for Application Developer)

Eclipse 2.1 (similar to WebSphere Studio V5)

Java Conventions

To configure the formatter, do these steps:

Select Window Æ Preferences Æ Java Æ Code Style Æ Formatter .

To display the information about a profile, click Show, or to create a new profile click New.

An existing profile can also be loaded by clicking Import.

Figure 3-23 Formatter preferences

Enforcing coding standards

The code formatting rules are enforced on code when a developer has entered code using their own style, and that does not conform to the team-defined standard. When this is the case, after the code has been written and tested and preferably before adding the code to the team repository, we recommend that the code be formatted.

To format the code, right-click in the Java editor, and select Source Æ Format. This formatting uses the rules that have been defined in the formatter preferences.

Creating a user-defined profile

User-defined profiles are established from one of the existing built-in profiles, which can be exported to the file system to share with team members. If the existing profile is modified, then you are prompted to create a new profile. Any profile that is created can be exported as an XML file that contains the standardized definitions required for your project. This can then be stored in a team repository and imported by each member of the team.

A profile consists of a number of sections that are provided as tab sections to standardize on the format and style that the Java code is written . Each of these tab sections are self-explanatory and provide a preview of the code after selection of the required format.

Figure 3-24 Formatter preferences: Eclipse profile

The definition of these tabs are described as follows:

Indentation: Specifies the indentations that you want on your Java code in the Workbench . The area it covers includes:

Tab spacing
Alignment of fields
Indentation of code

Braces: Formats the Java style of where braces are placed for a number of Java language concepts. A preview is provided as you select the check boxes to ensure that it fits in with the guidelines established in your team .

Figure 3-25 Formatter: Braces

White Space: Format where the spaces are placed in the code based on a number of Java constructs .

Figure 3-26 Formatter: White Space

Blank Lines: Specify where you want to place blank lines in the code for readability or style guidelines, for example:

Before and after package declaration
Before and after import declaration
Within a class:
Before first declaration

Before field declarations

Before method declarations

At the beginning of a method body

New Lines: Specifies the option of where you want to insert a new line, for example:

In empty class body
In empty method body
In empty annotation body
At end of file

Control Statements: Control the insertion of lines in control statements, as well as the appearance of if else statements of the Java code .

Figure 3-27 Formatter: Control Statements

Line Wrapping: Provides the style rule on what should be performed with the wrapping of code, for example:

Maximum line width (default is 80)
Default indentation (2)
How declarations, constructors, function calls, and expressions are wrapped

Comments: Determine the rules of the look of general comments and of Javadoc that are in the Java code.
ibm.com/redbooks