Java and JSP Programming Style Guide
From EQUIS Lab Wiki
Contents |
Java Programming Style
We will be following Doug Lea's Java Coding Standard (with a few additions and modifications shown below.) Read this standard carefully, and follow it in your projects.
In addition to Lea's style, we will follow these rules:
Brace Style
All blocks should be delimited by braces. Open and close braces should be on a separate line. Code between braces should be indented. For example:
if( expn ) { statement1 } else { statement2 }
Use braces even if the block is only one statement long. Do not write:
// DON'T do this if( x > 3 ) y = 245;
Instead, write:
// DO do this if( x > 3 ) { y = 245; }
This is longer, but less prone to problems if you should add another statement to the block later.
Parentheses
In statements that involve parentheses (if, for, while, try, catch, ...) and in method calls, parentheses should be flush to the statement, and one character should be used to space the contents of the parentheses. For example, write:
while( x > 0 ) ... for( int x=0; x<100; x++ ) ... myObject.m( x, y, z ); myObject.m2();
For mathematical expressions, use traditional math format. For example, write:
myObject.m( x*(4+y) );
Indenting
Code blocks should always be indented by four characters. For example,
for( int i=0; i<10; i++ ) { System.out.println( "i=" + i ); System.out.println( "i**2=" + i*i ); }
Member Variables
Member variables (or fields) should be named with a m prefix. For example, write:
class Foo { private String mBar; // mBar is a member variable public void doSomething( String bar ) // bar is a parameter { int x; // x is a local mBar = bar; } }
Configuring Eclipse
It is easy to configure Eclipse to help with adherance to this style.
Open Window|Preferences. Select Java|Code Style|Formatter, as shown below. Use Edit to create a new profile based on the default profile.
Within the format, set the brace style as follows:
Save your changes by clicking Ok. When prompted, specify a name for your profile (e.g., 322 Style).
JSP Programming Style
To come...