WebSphere Studio tip - Format your code Click here to see all the Developer/Admin Tips

Date tip published:10/07/2003
Description:When typing Java source code in WebSpere's Java Editor or pasting Java source code in from another source it can sometimes get out of line or not indent properly. This makes the source code harder to read and harder to debug. There is an quick and easy way in WebSphere Studio to get the source code to line up and indent properly.


To learn more about WebSphere Studio use the following links:

Introduction to WebSphere Studio 5
Introduction to WebSphere 5 for Domino Developers



Formatting Java Source Code


When typing Java source code in the Java Editor or pasting Java source code in from another source it can sometimes get out of line or not indent properly. This makes the source code harder to read and harder to debug. Thankfully there is an quick and easy way to get the source code to line up and indent properly using a menu option from the pop up menu. Below is an example of unformatted source code. There is no indenting or white space.



After doing the automatic formatting the source code is now formatted nicely.


The exact formatting applied can be customized in WebSphere Studio.


Procedure: Format Java Code

When editing a Java source file, It is useful to format the code according to the Code Formatter settings in the WSAD Preferences dialog. Follow these steps to format new or modified code:

1. Add or modify the code in a Java source file.

2. Right click in the editor area to access its context menu.

3. Select Format from the context menu to format the code according to the Code Formatter settings.


Procedure: Setting the Format Preferences



To change the way that the Format command formats the code follow the procedure below.

1. Go to Window | Preferences from the main WebSphere Studio menu.



2. In the list on the left hand side expand Java and then click on Code Formatter.

3. This will open the settings for controlling the code formatter on the right hand side of the dialog box. The three tabs at the top can be used to control when a new line is inserted, when a line is split and the style. The code sample at the bottom will show how code will appear using the formatting you chose.