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.
-
Open a
.java source file in the Eclipse editor.
-
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.
-
From the menu bar, select
Window->Preferences->Java->Code Style->Formatter.
-
Notice the select box indicating the active profile used to format code.
Eclipse comes with several built-in profiles.
-
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".
-
Notice all the tabs, each containing many detailed formatter settings.
You'll have to browse through these to accomplish the next few steps.
-
Set the formatter to ensure Javadoc header comments do not exceed
an 80-character line width.
-
Set the formatter so a switch statement is formatted like this:
switch (v) {
case 0:
break;
default:
System.out.print(v);
break;
}
-
Set the formatter so a simple
if statement
(without an else) is formatted like this:
if (b) System.out.println("good");
-
Press the "OK" button to apply the style, and test out your new settings.
Make sure they work for the examples shown above.
-
Try out different settings. See what you like.
Adopt formatting standards for your team.
-
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.
-
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!