(4) 在每一行里写一条语句 这条规则不包括for语句:比如:'for (int i = 0; i < 10; i++) x--;’可以增加代码的可读性。 public class OSPL { int method (int a, int b) { int i = a + b; return i; // 可读性不强 } 正确的: public class OSPLFixed { int method (int a, int b) { int i = a + b; return i; } } 参考:Section 7.1 of http://java.sun.com/docs/codeconv/html/CodeConventions.doc6.html#431
public class T7 { public synchronized void release () throws Throwable{ if (!_released) { close_resources (); // do what the old 'finalize ()' did _released = true; } } public void finalize () throws Throwable { release (); super.finalize (); } public void close_resources() {} private boolean _released = false;}class TestFixed { void closeTest () throws Throwable { t71 .release (); // FIXED t71 = null; } private T7 t71 = new T7 ();}
参考:Nigel Warren, Philip Bishop: "Java in Practice - Design Styles and Idioms for Effective Java". Addison-Wesley, 1999. pp.110-111
(8)不要使用不推荐的API 尽量使用JDK1.3推荐的API。在类和方法或者java组件里有很多方法是陈旧的或者是可以选择的。有一些方法SUN用了"deprecated“标记。最好不要使用例如: private List t_list = new List (); t_list.addItem (str); 假如查一下javadoc的话,会发现建议用add()来代替addItem()。 参考:http://java.sun.com/j2se/1.3/docs/api/index.html