所有的程序开发手册都包含了各种规则。一些习惯自由程序人员可能对这些规则很不适应,但是在多个开发人员共同写作的情况下,这些规则是必需的。这不仅仅是为了开发效率来考虑,而且也是为了后期维护考虑。
命名规范
定义这个规范的目的是让项目中所有的文档都看起来像一个人写的,增加可读性,减少项目组中因为换人而带来的损失。(这些规范并不是一定要绝对遵守,但是一定要让程序有良好的可读性)
byte[] buffer; |
byte buffer[]; |
SetCounter(int size){ this.size = size; } |
Java 文件样式
所有的 Java(*.java) 文件都必须遵守如下的样式规则
/** * Copyright ® 2000 Shanghai XXX Co. Ltd. * All right reserved. */ |
package hotlava.net.stats; import java.io.*; import java.util.Observable; import hotlava.util.Application; |
/** * 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 */ |
public class CounterSet extends Observable implements Cloneable |
/** * Packet counters */ protected int[] packets; |
/** * 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; } |
public CounterSet(int size){ this.size = size; } |
public Object 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 final void 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); } |
public String toString() { String retval = "CounterSet: "; for (int i = 0; i < data.length(); i++) { retval += data.bytes.toString(); retval += data.packets.toString(); } return retval; } } |
代码编写格式
if (i>0) { i ++ }; // 错误, { 和 } 在同一行 if (i>0) { i ++ }; // 正确, { 单独作为一行 } 语句永远单独作为一行. |
程序编写规范
... { FileOutputStream fos = new FileOutputStream(projectFile); project.save(fos, "IDE Project File"); } ... |
FileOutputStream fos = new FileOutputStream(projectFile); project.save(fos, "IDE Project File"); fos.close(); |
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()); } } |
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); } |
Swing
调试
性能
可移植性
Borland Jbulider 不喜欢 synchronized 这个关键字,如果你的断点设在这些关键字的作用域内的话,调试的时候你会发现的断点会到处乱跳,让你不知所措。除非必须,尽量不要使用。
参考资料