CSE 70, Fall 2008

Software Engineering

Lab 9 Instructions


Code formatting in Eclipse

Eclipse can help you with certain aspects of the coding standard practice in Extreme Programming. In this lab we'll take a look at Eclipse's configurable code-formatting feature.

Automatically reformat some code.

  1. Open a .java source file in the Eclipse editor.
  2. Select a modest-sized block of code and press Ctrl+Shift+F. (Alternatively, from the menu bar, select Source->Format.) Notice the code has been automatically reformatted according to some standard. What standard? Let's see.

Customize formatter settings.

  1. From the menu bar, select Window->Preferences->Java->Code Style->Formatter.
  2. Notice the select box indicating the active profile used to format code. Eclipse comes with several built-in profiles.
  3. Create your own profile by pressing the button labeled "New..." and entering a name of your choice. Leave the "Open the edit dialog now" box checked, and press "OK".
  4. Notice all the tabs, each containing many detailed formatter settings. You'll have to browse through these to accomplish the next few steps.
  5. Set the formatter to ensure Javadoc header comments do not exceed an 80-character line width.
  6. Set the formatter so a switch statement is formatted like this:
    switch (v) {
        case 0:
            break;
        default:
            System.out.print(v);
            break;
    }
    
  7. Set the formatter so a simple if statement (without an else) is formatted like this:
    if (b) System.out.println("good");
    
  8. Press the "OK" button to apply the style, and test out your new settings. Make sure they work for the examples shown above.
  9. Try out different settings. See what you like.

Adopt formatting standards for your team.

  1. Discuss code formatting with your teammates, and agree on some standards. It might be easier to use one of the built-in profiles than to make sure everyone has the same custom profile, but it's up to you.
  2. Reformat code that clearly deviates from your standards, but always follow the commit procedure. Reformatting is unlikely to change code behavior, but it's best to be sure!

Valid XHTML 1.0 Strict Valid CSS!