文档库 最新最全的文档下载
当前位置:文档库 › CodingConventions

CodingConventions

Java Coding Conventions

Identifier Naming and Capitalisation

Guidelines 1. Use descriptive names for all variables, function names, constants, and other identifiers.

2. Use single letter identifiers only for the counter in loops.

3. Variable names start with lower case.

4. Multi-word identifiers are internally capitalised.

5. Do not use hyphens or underscores to separate multi-word identifiers. Examples private static float sumDiffSquares = 0;

for (int i = 0; i < 5; i++){ ... }

Avoid private static float Sum = 0;

private static float sumdiffsquares = 0;

private static float sum_diff_squares = 0;

private static float x = 0;

Class, Package, and Method Naming and Capitalisation

Guidelines 1. Classes begin with a capital letter.

2. Packages are all lower case.

3. Methods begin with a lower case letter.

4. Multi-word identifiers are internally capitalised in methods.

Examples package foo.bar.baz;

public class MeanStandardDeviation

private Vector getNewVector(Vector oldVector)

Avoid package Foo.Bar.Baz;

public class Meanstandarddeviation

private Vector GetNewVector(Vector oldVector)

Program Modules

Guidlines 1. Each public class is contained in a separate file.

Comments: Classes

Guidelines Every class should be preceded with a descriptive comment using the

"JavaDoc" notational convention. The comment should name the class,

describe its purpose, and name the author using JavaDoc keywords. Example/**

* MyFirstJavaProgram - A simple program to print

* out "Welcome to my first Java Program".

* @author Lijing

*

public class MyFirstJavaProgram {

...

}

Comments: Methods

Guidelines 1. Every method should be preceded with a descriptive comment using the "JavaDoc" notational convention.

2. The comment should name the method, describe its purpose,

comment all arguments, the return value, and any exceptions using

JavaDoc keywords.

Example/**

* Prints out "Welcome to my first Java Program" and

* the command line arguments.

* @param arg A string array containing the command

* line arguments.

* @return No return value.

* @exception Exception If the file is not found

* or other error.

*/

public static void main (String[] arg) throws Exception {

...

}

Comments: In-line

Guidelines 1. In-line comments should be used to explain complicated sections of code, such as loops.

2. Use the // comment delimiter for in-line comments.

3. Do not comment generally known features of the Java language. Example// Do a 3D transmogrification on the matrix in args.

for (int i=0; i < args.length; i++) {

for (int j=0; j < args.length; j++) {

for (int k=0; k < args.length; k++) {

vals.transmogrify(args[i], args[j], args[k]);

}

}

}

Avoid// the variable "i" loops from 0 to the length of args.

for (int i=0; i < args.length; i++) {

:

}

Spacing: Between lines

Guidelines 1. Use two blank spaces to separate each method within a class definition.

2. Use one blank space to separate logical sections of code within a

method.

Examples public static void main(String[] arg) {

System.out.println ("Hello" + " " + "World");

}

public void foo() {

:

}

Class Indentation

Guidelines Class declarations are not indented.

Example public class Foo {

:

}

Avoid public class Foo {

:

}

Blocks in Conditionals and Loops

Guidelines The bodies of conditionals and loops should always be written using curly braces to create a compound statement block, even if only one statement is

in the body.

Example if (foo() < bar()) {

baz(qux);

}

Avoid if (foo() < bar())

baz(qux);

相关文档