1、盡量不要使用註解。

 

2、用程式碼表達本意

錯誤://Check to see if the employee is eligible for full benefits

         if((employee.flags & HOURLY_FLAG) && (employee.age > 65))

正確:if(employee.isEligibleForFullBenefits())

 

3、對意圖解釋:

如://Attempt to get a race condition by creating large number of threads.

      for(int i=0; i<2500; i++){

        .......

        thread.start();

      }

 

4、如果參數或回傳值是標準函式庫的一部份,或是某些不能修改的程式碼,可以加入幫助闡明的註解。

 

5、不錯的IDE都會顯現//TODO註解,如果完成代辦事項記得刪除。

 

6、日記、署名或出處等註解,應該用原始碼管控系統來儲存類似資訊。

 

7、被註解的程式碼:

如:this.bytePos = writeBytes(pngIdBytes, 0);

      //hdrPos = bytePos;

      writeHeader();

在有原始碼管控的情形下,這些就不用保留了。

 

 

1、變數的宣告要盡可能靠近被使用的地方

如:private static void readPreFerences(){

        IuputStream is = null;

          try{

            is = new FileInputStream(getPreferencesFile());

            ....

      }

 

2instance variable應該被宣告在類別的上方

 

3、運算子左右邊添加空白,除法或乘法因為有較高的優先權,所以不添加空白。

 

4、函式和他的參數高度相關,因此不另外添加空白

 

5、水平對齊

如:public class FitNessExpediter implements ResponseSender{

        private Socket               socket;

        private FitNessContext context;

        ........

      }

可改成(讓人專注於類別而不是變數名)

如:public class FitNessExpediter implements ResponseSender{

        private Socket socket;

        private FitNessContext context;

        ........

      }

文章標籤
全站熱搜
創作者介紹
創作者 阿洲 的頭像
阿洲

阿洲程式天地

阿洲 發表在 痞客邦 留言(0) 人氣(27)