【转】Java 程序编码规范
所有的程序开发手册都包含了各种规则。一些习惯自由程序人员可能对这些规则很不适应,但是在多个开发人员共同写作的情况下,这些规则是必需的。这不仅仅是为了开发效率来考虑,而且也是为了后期维护考虑。
命名规范
定义这个规范的目的是让项目中所有的文档都看起来像一个人写的,增加可读性,减少项目组中因为换人而带来的损失。(这些规范并不是一定要绝对遵守,但是一定要让程序有良好的可读性)
byte[] buffer;而不是:
byte buffer[];方法的参数
SetCounter(int size){ this.size = size;} Java 文件样式
所有的 Java(*.java) 文件都必须遵守如下的样式规则
/** * Copyright ? 2000 Shanghai XXX Co. Ltd. * All right reserved. */其他不需要出现在 javadoc 的信息也可以包含在这里。
package hotlava.net.stats;import java.io.*;import java.util.Observable;import hotlava.util.Application;这里 java.io.* 使用来代替InputStream and OutputStream 的。
/** * A class representing a set of packet and byte counters * It is observable to allow it to be watched, but only * reports changes when the current set is complete */接下来是类定义,包含了在不同的行的 extends 和 implements
public class CounterSet extends Observable implements Cloneable (如果引入的类和接口很多,建议换行,转载者注)Class Fields
/** * Packet counters */protected int[] packets;public 的成员变量必须生成文档(JavaDoc)。proceted、private和 package 定义的成员变量如果名字含义明确的话,可以没有注释。
/** * Get the counters * @return an array containing the statistical data. This array has been * freshly allocated and can be modified by the caller. */public int[] getPackets() { return copyArray(packets, offset); }public int[] getBytes() { return copyArray(bytes, offset); }public int[] getPackets() { return packets; }public void setPackets(int[] packets) { this.packets = packets; }其它的方法不要写在一行上publicCounterSet(int size){ this.size = size;}克隆方法 publicObject clone() { try { CounterSet obj = (CounterSet)super.clone(); obj.packets = (int[])packets.clone(); obj.size = size; return obj; }catch(CloneNotSupportedException e) { throw new InternalError("Unexpected CloneNotSUpportedException: " + e.getMessage()); }}类方法 /** * Set the packet counters * (such as when restoring from a database) */protected finalvoid setArray(int[] r1, int[] r2, int[] r3, int[] r4) throws IllegalArgumentException{ // // Ensure the arrays are of equal size // if (r1.length != r2.length || r1.length != r3.length || r1.length != r4.length) throw new IllegalArgumentException("Arrays must be of the same size"); System.arraycopy(r1, 0, r3, 0, r1.length); System.arraycopy(r2, 0, r4, 0, r1.length);}toString 方法 publicString toString() { String retval = "CounterSet: "; for (int i = 0; i < data.length(); i++) { retval += data.bytes.toString(); retval += data.packets.toString(); } return retval; }}main 方法 代码编写格式
if (i>0) { i ++ }; // 错误, { 和 } 在同一行 if (i>0) { i ++ }; // 正确, { 单独作为一行 } 语句永远单独作为一行. 如果 } 语句应该缩进到与其相对应的 { 那一行相对齐的位置。程序编写规范
... { FileOutputStream fos = new FileOutputStream(projectFile); project.save(fos, "IDE Project File"); } ...除非输出流一出作用域就关闭,非引用计数的程序语言,比如JAVA,是不能自动完成变量的清场工作的。必须象下面一样写: FileOutputStream fos = new FileOutputStream(projectFile); project.save(fos, "IDE Project File"); fos.close();Clone
implements Cloneable public Object clone() { try { ThisClass obj = (ThisClass)super.clone(); obj.field1 = (int[])field1.clone(); obj.field2 = field2; return obj; } catch(CloneNotSupportedException e) { throw new InternalError("Unexpected CloneNotSUpportedException: " + e.getMessage()); } }final 类 public void setPackets(int[] packets) { this.packets = packets; } CounterSet(int size) { this.size = size; }编程技巧
class Colour { public static final Colour BLACK = new Colour(0, 0, 0); public static final Colour RED = new Colour(0xFF, 0, 0); public static final Colour GREEN = new Colour(0, 0xFF, 0); public static final Colour BLUE = new Colour(0, 0, 0xFF); public static final Colour WHITE = new Colour(0xFF, 0xFF, 0xFF);}(JDK5以后推荐使用枚举了,转载者注)Swing
调试
性能
可移植性
Borland Jbulider 不喜欢 synchronized 这个关键字,如果你的断点设在这些关键字的作用域内的话,调试的时候你会发现的断点会到处乱跳,让你不知所措。除非必须,尽量不要使用。
参考资料
查找有关 Thread 类和 Java 2 平台,标准版,API 规范的其他方面的信息。