第一章 Java開發(fā)中通用的方法和準(zhǔn)則
Rule1:不要在常量和變量中出現(xiàn)易混淆的字母
public class Client {
public static void main(String[] args) {
long i = 1l;
System.out.println("i的兩位是:" + (i+i));
}
}
字母“l(fā)”和字母“O”盡量不要和數(shù)字混用仰担,以免閱讀時產(chǎn)生理解與程序意圖的偏差疙驾。
如果字母和數(shù)字必須混合使用专执,字母“L”務(wù)必大寫蚯舱,字母“O”則增加注釋。
Rule2:莫讓常量蛻變成變量
public class Client {
public static void main(String[] args) {
System.out.println("常量會變哦:" + Const.RAND_CONST);
}
}
/*接口常量*/
interface Const {
//這還是常量嗎?
public static final int RAND_CONST = new Random().nextInt();
}
不用像上面那樣使用常量會變的功能來實(shí)現(xiàn)序列號算法、隨機(jī)種子生成。