Java编程规范

1. 程序命名规范:

文件名前缀 文件名后缀

EJB i. 实体bean *.java

bean的文件名以EJB结尾

remote interface的文件名就是ejb的name
home interface的文件名以Home结尾
例子: bean的文件名:ejbnameEJB
remote interface的文件名:ejbname
home interface的文件名:ejbnameHome
ii. Session Bean
bean的文件名以SEB结尾
remote interface的文件名就是ejb的name
home interface的文件名以Home结尾
例子: bean的文件名:ejbnameSEB
remote interface的文件名:ejbname
home interface的文件名:ejbnameHome
SERVLET 文件名以SER结尾
*SER.java *.java
2. 程序结构规范:
i. 程序头注释
程序启示部分为程序头注释部分,包括以下内容:
/* class name 
* version information 
* Design Date 
* Copyright notice 
*/
ii. 包与输入包的声明
程序头注释部分以后为该程序调用系统API以及PACKAGE内容:
package java.awt
import java.awt.peer.CanvasPeer;
import com.xxx.shopping.web.Event;
iii. 类与接口(Class/Interface)的规范
类与接口的规范 Note
1 类或接口头部注释部分 /**
*Class description 类或接口的简单功能描述
*@Version x.xx date 版本号及日期
*@author Name 程序员名字
*/
2 类或接口的声明 无具体限制
3 类或接口中注释 /* 不同方法的简要描述 */
4 类或接口中的变量定义的规范 1.public class 变量
2.protected
3.package level
4.private
5 实例(instance) 变量定义的规范 1.public class 变量
2.protected
3.package level
4.private
6 构造器(Constructor) 无具体限制
7 方法 (Methods) 无具体限制
代码范例:
/*
* @(#)Blah.java 1.82 99/03/18
*
* Copyright (c) 1994-1999 Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"
. You shall not 
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*/
package java.blah;
import java.blah.blahdy.BlahBlah;
/**
* Class description goes here.
*
* @version 1.82 18 Mar 1999
* @author Firstname Lastname
*/
public class Blah extends SomeClass {
/* A class implementation comment can go here. */
/** classVar1 documentation comment */
public static int classVar1;
/**
* classVar2 documentation comment that happens to be
* more than one line long
*/
private static Object classVar2;
/** instanceVar1 documentation comment */
public Object instanceVar1;
/** instanceVar2 documentation comment */
protected int instanceVar2;
/** instanceVar3 documentation comment */
private Object[] instanceVar3;
/**
* ...constructor Blah documentation comment...
*/
public Blah() {
// ...implementation goes here...
}
/**
* ...method doSomething documentation comment...
*/
public void doSomething() {
// ...implementation goes here...
}
/**
* ...method doSomethingElse documentation comment...
* @param someParam description
*/
public void doSomethingElse(Object someParam) {
// ...implementation goes here...
}
}
3. 程序缩格规范:
i. 程序每行不超过80列
ii. 断行规范
1) 逗号后断行
2) 运算符前断行
代码范例:
someMethod(longExpression1, longExpression2, longExpression3,
longExpression4, longExpression5); //断行规范,逗号后断行(推荐)
var = someMethod1(longExpression1,
someMethod2(longExpression2,
longExpression3)); //断行规范,逗号后断行(推荐)
longName1 = longName2 * (longName3 + longName4 - longName5)
+ 4 * longname6; //断行规范,运算符前断行(推荐)
longName1 = longName2 * (longName3 + longName4
- longName5) + 4 * longname6; //断行规范,运算符前断行(不建议)
4. 注释规范:
i. 长注释
程序中
/*
* 长注释部分
*/
ii. 单行注释
程序中
/* 单行注释 */
iii. 句尾注释
程序中
if (a == 2) {
return TRUE; /* 句尾注释 */
}
iv. 段末注释
对于某一段逻辑进行解释
if (a > 1) {
…
}
else {
return false; //对返回值的解释
}
//段末注释部分
//解释前段逻辑
//使得该逻辑部分便于理解
v. 详细注释
出现在程序头部
/**
*Class description
*@Version x.xx date
*@author Name
*/
5. ?声明
* int level; // indentation level 变量声明部分(推荐)
int size; // size of table 变量声明部分(推荐)
int level, size;
int foo, fooarray[]; //禁止不同类型在同行声明(不建议)
* 对于局部变量都要进行初始化(Initialization)
* 必须在段的启始部分定义变量
代码范例:
void myMethod() {
int int1 = 0; // 局部变量都要进行初始化(该方法的起始段)
if (条件) {
int int2 = 0; // 局部变量都要进行初始化 ("if" 段的起始部分)
...
}
} (推荐)
for (int i = 0; i < maxLoops; i++) { ... } (推荐) ???
int count;
...
myMethod() {
if (条件) {
int count = 0; // 禁止!
...
}
...
}
* 类与接口中的声明
i. 方法名后的括号中无空格
ii. 启始的"{"与声明在同行
iii. 结尾的"}"单独成行,除非此类/接口/方法为空
代码范例:
class Sample extends Object {
int ivar1;
int ivar2;
Sample(int i, int j) {
ivar1 = i;
ivar2 = j;
}
int emptyMethod() {}
...
}
6. 语句
* 每行只写一个语句行
代码范例:
argv++; // 推荐
argc--; //推荐
argv++; argc--; // 禁止
* ‘if, if-else, if else-if else’ 语句
代码范例:
if (条件) {
语句部分;
}
if (条件) {
语句部分;
} else {
语句部分;
}
if (条件) {
语句部分;
} else if (条件) {
语句部分;
} else{
语句部分;
}
* ‘for’ 语句
for (initialization; 条件; update) {
语句部分;
}
空的‘for’ 语句:
for (initialization; 条件; update);
* ‘while’ 语句:
while (条件) {
语句部分;
}
空的‘while’ 语句:
while (条件);
* ‘do-while’ 语句:
do {
语句部分;
} while (条件);
* ‘switch’ 语句:
switch (条件) {
case ABC:
语句部分;
/* falls through */
case DEF:
语句部分;
break;
case XYZ:
语句部分;
break;
default:
语句部分;
break;
}
* ‘try-catch’ 语句:
try {
语句部分;
} catch (ExceptionClass e) {
语句部分;
}
try {
语句部分;
} catch (ExceptionClass e) {
语句部分;
} finally {
语句部分;
}
7. 命名规则范例:
见下表
范例
包(Packages) com.sun.eng
com.apple.quicktime.v2
edu.cmu.cs.bovik.cheese
类 (Classes) class Raster;
class ImageSprite;
接口(Interfaces) interface RasterDelegate;
interface Storing;
方法 (Methods) run();
runFast();
getBackground();
变量 (Variables) int i;
char c;
float myWidth;
常量 (Constants) static final int MIN_WIDTH = 4;
static final int MAX_WIDTH = 999;
static final int GET_THE_CPU = 1;
8. Coding 中的几点注意事项:
避免多个变量赋同样值
fooBar.fChar = barFoo.lchar = 'c'; // 不建议
避免以下情况的发生
if (c++ = d++) { // 禁止! (Java 不接受)
...
}
应该写为:
if ((c++ = d++) != 0) {
...
}
d = (a = b + c) + r; //不建议
应该写为:
a = b + c;
d = a + r;
if (a == b && c == d) //不建议
if ((a == b) && (c == d)) //正确