红色代码为错误示范,绿色代码为正确示范
一、 名称统一使用英文命名,不使用拼音命名或拼音+英文组合的命名。
public boolean hasWanCheng(){
}
public boolean hasFinish(){
}
二、 变量名采用驼峰命名法,用有意义的单词前缀或后缀来区别两个含义不同的变量,不使用大小来区分,不使用加无意义数字后缀区分。
private int number;
privateint Number;
privateint number1;
privateint number2;
private int carNumber;
privateint trainNumber;
三、 包名采用全小写单词。包名与变量名相同,不使用无意义数字后缀。
dyx.co.isid.FrameWork.Banking.ACCOUNT
dyx.co.isid.framework.banking.a00001
dyx.co.isid.framework.banking.account
dyx.co.isid.framework.banking.account
四、 类名首字母大写,与变量名相同,不使用无意义数字后缀。
public class S0001 {
}
public class sampleclass {
}
public class Sample {
}
public class SampleClass {
}
五、 实现接口或抽象父类的实现类,以“Impl”结尾。
public class SampleImpl implements ISample {
}
六、 方法名首字母小写,与变量名类似。
public void BadSample(int number){
}
public void setNumber(int number){
}
七、 方法名要使用动词性质的单词,并能真实表达含义。如:创建某一个实例的方法名,以create或意思相近的单词开始,跳转或进入以to、enter开始。
public Sample createSample(){
}
public Another toAnother(){
}
八、 返回类型为boolean型的方法名,要能表达判断的含义,可以是象exists这样能表达判断含义的单词,也可以使用is+名词/形容词、can+动词、has+名词/动词的过去分词等组合来表达,方法名所要表达含义为:满足方法名的描述,结果就为true,如:
public boolean isAsleep(){//如果是沉睡的这样一种状态,结果为true。
}
public boolean canSpeak(){//如果能够说,结果为true。
}
public boolean hasExpired(){//如果已经过期,结果为true。
}
public boolean exists(){//如果存在,结果为true。
}
public boolean hasValue(){//如果有值,结果为true。
}
九、 boolean变量名与返回类型为boolean型的方法名相似。
private boolean isAsleep;
private boolean canSpeak;
private boolean hasExpired;
private boolean exists;
private boolean hasValue;
十、 常量的定义,名称全大写,单词之前用“_”切分。
public static final int SampleValue = 10;
public static final int SAMPLE_VALUE = 10;
十一、for循环与嵌套for循环,控制循环次数的临时变量应按照i、j、k……这样的顺序命名。
for(int j = 0; j < 10; j++){
...
for(int n = 0; n < 10;n++){
...
for(int t = 0; t < 10;t++){
}
}
}
for(int i = 0; i < 10; i++){
...
for(int j = 0; j < 10;j++){
...
for(int k = 0; k < 10;k++){
}
}
}
十二、除特殊需要外,尽可能使用接口类型定义变量,而不用实例类型。
ArrayList sampleList = new ArrayList ();
List sampleList = new ArrayList ();
十三、不使用当前JDK版本下不推荐使用但JDK中依然保留的方法。
Date sampleDate = new Date();
System.out.println(sampleDate.getYear());
Calendar sampleCalendar = Calendar.getInstance();
System.out.println(sampleCalendar.get(Calendar.YEAR));
十四、不在程序中出现未曾使用过的私有变量、私有方法,一般开发环境也会以黄色提示。
十五、严格使用public、private、protected修饰符,类中不可能被第二个类调用的方法、变量等不要用public来代替private。
十六、用空格控制代码格式的规范:
1、 用“,”间隔的参数列表,for循环中用“;”间隔的执行语句,“,”、“;”与前面代码之间不空格,与后面代码之间空一格。
public void badSampleMethod(int score,int number){
}
for (int i = 0;i < 1000;i++){
}
public void fixedSampleMethod(int score, int number){
}
for (int i = 0; i < 1000; i++){
}
2、 各种算术运算符、逻辑运算符、赋值运算符等与前后变量之间空一格。
①a=1;
②b-=a;
①a = 1;
②b -= a;
3、“++”、“--”要紧跟变量名,不空格。
a ++;
a++;
十七、boolean型变量或方法返回,在做判断条件时还需要与true或false比较判断吗?
①while(hasStock == true) {
}
②if(hasStock == false){
}
①while(hasStock) {
}
②if(!hasStock){
}
十八、不要导入无用的包,一般开发环境会给出黄色提示,也不要用“*”把一个包下所有的类全导入进来,用到某个类只导入该类。
十九、静态方法、静态变量属于类,而不属于某个对象,不要用对象来调用静态方法。
public class BadSample {
public static voidsampleClassMethod(){}
public void sampleMethod(){
BadSample object = newBadSample();
object.sampleClassMethod();
}
}
public class FixedSample {
public static voidsampleClassMethod(){}
public void sampleMethod(){
FixedSample object = newFixedSample();
FixedSample.sampleClassMethod();
}
}
二十、不要在一行内申明多个变量。
private String firstName, lastName;
private String firstName;
private String lastName;
二一、程序中要使用的定值,应定义常量后使用。
private int[] sampleArray = new int[10];
private static final int ARRAY_SIZE = 10;
private int[] sampleArray = new int[ARRAY_SIZE];
二二、严格按照变量实际使用情况确定作用域。如:只在方法内使用的临时变量不要定义为成员变量,除特殊情况外,循环体里不申明临时变量,应将循环体内要用的临时变量放在循环体外申明。
private int value;
public void calcValue(SomeObj inValue){
value = inValue.getData();
for(int i = 0; i < value;i++){
StringBuffer sampleBuffer = new StringBuffer();
……
}
}
public void calcValue(SomeObj inValue){
int value =inValue.getData();
StringBuffer sampleBuffer =new StringBuffer();
for(int i = 0; i <value; i++){
……
sampleBuffer.setLength(0);
}
}
二三、在含义不同的情况下,不要重复使用同一变量,应定义不同含义的变量名称。
int i;
for (i = 0; i < a; i++) {
}
for (i = 0; i < a; i++) {
}
i = a * 2;
for (int i = 0; i < a; i++) {
}
for (int i = 0; i < a; i++) {
}
int total = a * 2;
二四、不要申明让开发人员的理解上产生二义性的变量,虽然程序能正常执行,执行时不会产生任何二义性,如:不要申明与父类相同的成员变量名,方法内临时变量名不要与成员变量名相同。这时不加“super.”、“this.”这样的前缀修饰,开发人员很容易混淆。
public class BadSample{
protected int number = 4;
}
public class ChildBadSample extends BadSample{
protected int number = 5;
private void print(){
int number = 10;
System.out.println("numberis " + number );//此时的number到底是哪一个?
//编译器当然知道,维护者呢?
}
}
public class BadSample{
protected int number = 4;
}
public class ChildBadSample extends BadSample{
protected int childNumber =5;
private void print(){
int numberTemp = 10;
System.out.println("numberis " + numberTemp );
}
}
二五、无特殊情况下,for循环应放在try、catch块里面。
for(int i = 0; i < size; i++){
try {
int num = Integer.parseInt(str[i]);
someOtherMethod(num);
}catch(NumberFormatExceptione) {
e.printStackTrace();
}
}
try {
for(int i = 0; i < size;i++){
int num = Integer.parseInt(str[i]);
someOtherMethod(num);
}
} catch(NumberFormatException e) {
e.printStackTrace();
}
二六、String型的变量的申明同时赋值,无特殊情况不要使用“new”关键字创建对象。
private String sampleString = new String (“Literal”);
private String sampleString = “Literal”;
二七、拼接字符串用StringBuffer而不用String。
String fruit = “apples”;
fruit += “, bananas”;
StringBuffer fruit = new StringBuffer();
fruit.append(“apples”);
fruit.append(“, bananas”);
二八、与二七相反,如果未拼接字符串,只是申明使用,用String而不用StringBuffer。
StringBuffer buffer = new StringBuffer(“Message”);
String output = buffer.toString();
System.out.println(output);
String output = “Message”;
System.out.println(output);
二九、如果判断某个String变量是否等于某个常量,不需要先判空,换种写法。
public boolean validate(String userInput) {
return (userInput != null &&userInput.equals(“y”);
}
public boolean validate(String userInput) {
return “y”.equals(userInput);
}
三十、int型->String型、String型->int型的类型转换。
String sample = “” + integer;
String sample = (new Integer(integer)).toString();
int integer = (new Integer(sample)).intValue();
int integer = Integer.valueOf(sample).intValue();
String sample = String.valueOf(integer);
int integer = Integer.parseInt(sample);
三一、用System.getProperty()取换行符方法,代替“\n”、“\r\n”。
StringBuffer result = new StringBuffer(“Message:¥n”);
result.append(message);
System.out.println(result);
StringBuffer result = new StringBuffer(“Message:”);
result.append(System.getProperty(“line.separator”));
result.append(message);
System.out.println(result);
三二、为减少计算时误差,用BigDecimal类型。
BigDecimal b = new BigDecimal("0.0");
三三、JDK5.0及以上,引入泛型概念,在申明与使用过程中应加以使用。
List<String> list = new ArrayList<String>();
三四、关于类似IO操作后的流关闭。(相似的操作最后都应关闭流,序列化操作reset,否则会导致内存溢出)
try {
BufferedReader reader = newBufferedReader(new FileReader(file));
reader.read();
reader.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
BufferedReader reader = null;
try {
reader = newBufferedReader(new FileReader(file));
reader.read();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe1) {
ioe1.printStackTrace();
} finally {
try{
reader.close();
}catch(IOException ioe2){
ioe2.printStackTrace();
}
}
以上为目前Coding中必须严格执行的,做为对自己代码的复检的标准,确认完成任务前应反复核对后达到标准,并无明显异常的代码,才算确认完成所分配任务,下面给出建议性的规范,不做强制要求:
三五、关于类型强制转换。
public void badSampleMethod(Object object){
Parent parent = (Parent)obj;
}
public void fixedSampleMethod(Object object){
if (object instanceofParent) {
Parentparent = (Parent)object;
}else {
}
}
三六、异常类以“Exception”结尾。
public class Sample extends Exception{
}
public class SampleException extends Exception{
}
三七、接口以“I”开始。
public interface ISample {
}
三八、抽象类以“Abstract”开始。
abstract class AbstractSample {
}
三九、功能相反的两个方法应用一对反义词来做方法名。如:send与receive、get与set、top与bottom、width与height。
public void send(String message){
}
public String receive(){
}
四十、方法的参数名称以“_”开始。
public void setName(String name){
}
public void setName(String _name){
}
四一、比较符号,“<”、“<=”、“>”等在一个完整判断所有分支中应保持方向一致。
if( a < i ){
}else if( a > i){
}else{
}
if( a < i ){
}else if( i < a ){
}else{
}
四二、为了防止Class.newInstance()调用时异常,类中都应该有默认的构造函数(无参构造函数),即使需要构造时为成员变量赋值,也不要简单的写一个带参构造函数,这样会覆盖默认构造函数。解决过程如下:
public class BadSample {
private int number;
public BadSample(int num){
number =num;
}
}
public class FixedSample {
private int number;
public FixedSample(){
this(10);
}
public FixedSample(intnum){
number= num;
}
}
四三、不要一次性申明很多变量,应在使用处申明,使代码易读。
int localValue1 = 10;
int localValue2 = 20;
int localValue3 = 30;
TestClass test = new TestClass();
test.sampleMethod1(localValue1);
・・・
test.sampleMethod2(localValue2);
・・・
test.sampleMethod3(localValue3);
・・・
int localValue1 = 10;
test.sampleMethod1(localValue1);
・・・
int localValue2 = 20;
test.sampleMethod2(localValue2);
・・・
int localValue3 = 30;
test.sampleMethod3(localValue3);
・・・
四四、添加用于统计代码的注释,
以下为.java文件中的注释格式
/**
*<pre>
*人员列表
*</pre>
*
*@pagePersonList
*@author王伟
*/
以下为.jsp文件中的注释格式
<%--
/**
* @page PersonList
* @author 王伟
*/
--%>