posts - 61,  comments - 2033,  trackbacks - 0

          1.統(tǒng)一工作目錄

          2.Interface oriented programming

          在定義參數(shù)類型,或者方法返回類型,使用Map或者List,不用Hashmap or ArrayList。只有在構(gòu)造時(shí)才允許出現(xiàn)Hashmap或者ArrayList
          public List getAllProduct(); //正確。定義返回類型
          public ArrayList getAllProduct(); //錯(cuò)誤!!定義返回類型
          
          public List queryByCritical(Map criticals); //定義參數(shù)類型
          public List queryByCritical(HashMap criticals); //錯(cuò)誤!!定義參數(shù)類型
          List result = null;//定義類型的時(shí)候未List
          result = new ArrayList();//只有構(gòu)造時(shí)出現(xiàn)的實(shí)現(xiàn)類
          
          Map result = null;//定義類型的時(shí)候未Map
          result = new HashMap();//只有構(gòu)造時(shí)出現(xiàn)的實(shí)現(xiàn)類

          3.變量命名不允許出現(xiàn)下劃線,除了常量命名時(shí)用下劃線區(qū)分單詞

          String user_name= null;//錯(cuò)誤!!! 即使數(shù)據(jù)庫中這種風(fēng)格
          String userName = null;//正確寫法
          int CET_SIX=6;//常量命名時(shí)用下劃線區(qū)分單詞,字符全部大寫

          4.代碼中不能出現(xiàn)magic number

          //錯(cuò)誤!!不能出現(xiàn)如此寫法,需要定義為常量
          if(user.getNumber() == 1001 ) {
          //do something
          }
          //正確寫法
          static final int ADMINISTRATOR_ROLE_NUMBER = 1001;
          static final int MIN_WIDTH = 4;
          static final int MAX_WIDTH = 999;
          static final int GET_THE_CPU = 1
          
          if(user.getNumber() == ADMINISTRATOR_ROLE_NUMBER ) {
          //do something
          }

          5.不在循環(huán)中定義變量

          //sample code snippet
          for(int i=0;i<10;i++){
              ValueObject vo = new ValueObject();
          }
          //recommend this style
          ValueObject vo = null;
          for(int i=0;i<10;i++){
             vo = new ValueObject();
          }

          6.NOT TAB,采用4 spaces。大家請?jiān)O(shè)置ide的TAB為4 space

          7.使用StringBuffer來替代String + String

          不正確寫法:
          //sample code snippet
          String sql =”INSERT INTO test (”;
          
          Sql+=”column1,column2,values(”
          Sql+=”1,2)”

          正確寫法:
          StringBuffer sql = new StringBuffer();
          sql.append(”INSERT INTO test (”);
          sql.appdend(”column1,column2,values(”);
          sql.append(”1,2)”);

          8.單語句在IF,While中的寫法. 使用Brackets 來做程序塊區(qū)分

          不正確寫法:
          if ( condition) //single statement, code here
          
          while ( condition ) //single statement, code here

          正確寫法:
          //IF
          if ( condition) {
            //code here
          }
          
          //WHILE
          while ( condition ) {
            // code here
          }

          9.Brackets 應(yīng)當(dāng)直接在語句后

          if ( foo ) {
              // code here
          }
          
          try {
              // code here
          } catch (Exception bar) {
              // code here
          } finally {
              // code here
          }
          
          while ( true ) {
              // code here
          }

          10.用log4j來做日志記錄,不在代碼中使用System.out

          錯(cuò)誤寫法:
          System.out.println(" debug 信息");

          正確寫法:
          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;
          
          private static Log logger = LogFactory.getLog(SQLTable.class);
          
          logger.debug("debug 信息"); //注意這里沒有涉及字符串操作
          
          //涉及字符串操作或者方法調(diào)用的,需要用logger.isDebugEnable()來做判斷
          if(logger.isDebugEnable()){
             logger.debug(String1  + string 2 + string3 + object.callMethod()); 
          }

          如果程序中需要輸出的信息為非調(diào)試信息,用logger.info來做輸出
          logger.info(""Can't find column to build index. ColName=" + columnName");

          11.異常處理中日志問題

          錯(cuò)誤寫法1:
          try{
             //handle something
          } catch (Exception ex) {
             //do nothing. 請確定該exception是否可以被忽略!!!!
          }

          錯(cuò)誤寫法2:
          try{
             //handle something
          } catch (Exception ex) {
            ex.printStackTrace ();//不在程序中出現(xiàn)如此寫法!!
          }

          錯(cuò)誤寫法3:
          try{
             //handle something
          } catch (Exception ex) {
            log.error(ex);//這樣將僅僅是輸出ex.getMessage(),不能輸出stacktrace
          }

          正確寫法:
          try{
             //handle something
          } catch (Exception ex) {
             log.error("錯(cuò)誤描述",ex);//使用log4j做異常記錄
          }

          12.Release Connection ,ResultSet and Statement

          //sample code snippet
          Connection con = null;
          Statement st = null;
          ResultSet rs = null;
          
          try {
            con = JNDIUtils.getConnection();
            st = …
            rs = …
          } finally {
                  JDBCUtil.safeClose(rs);//close resultset ,it is optional
                  JDBCUtil.safeClose(st);//close statement after close resultset, it is optional
                  JDBCUtil.safeClose(con);//make sure release it right now
          }

          13.Replace $variableName.equals(’string’) with ‘string’.equals($variableName)

          減少由于匹配的字符為null出現(xiàn)的nullpointexception
          //sample code snippet
          String username = …
          …
          if(“mark”.equals(userName)){
          	…
          }

          14.always import classes

          //recommend coding convention
          import java.util.HashMap;
          import java.util.Map;
          import java.util.List;
          
          //import java.util.*; NOT!!
          //We can use eclipse,right click, choose Source -> Organize Imports
          //hotkey:Ctrl+Shift+O

          15.注釋

          程序header,加入如下片斷。這樣將可以記錄當(dāng)前文件的版本以及最后的修改人員

          java,jsp中加入
          /**
           * $Id: $
           *          
           */

          xml中加入
          <?xml version="1.0" encoding="UTF-8"?>
          <!--
          $Id:$ 
          -->

          16.在有issue出,加入//TODO 來記錄,以便在task中可以方便記錄未完成部分

          //sample code snippet
          //TODO issue: the data format, should be fixed

          17.domain model或者VO使用注意事項(xiàng)

          檢查數(shù)據(jù)庫中允許為null的欄位

          對從數(shù)據(jù)庫中取出的domain model或者VO,如果數(shù)據(jù)庫中允許為null,使用有必要檢查是否為null
          CODE SNIPPET
          //user table中的該字段允許為null,使用時(shí)就需要去check,否則可能出現(xiàn)nullpoint exception
          if(user.getDescription()!=null) {
            //do something
          }

          需要完成VO中的equals,hashCode,toString 三個(gè)方法

          18.JSP中約定

          為每個(gè)input設(shè)定好size同maxlength

          <input type="text" maxlength = "10" size="20"/>
          posted on 2005-12-20 17:07 魚上游 閱讀(1450) 評論(7)  編輯  收藏 所屬分類: 爪哇世界探險(xiǎn)


          FeedBack:
          # re: Rule Of Development
          2005-12-20 17:29 | 胡子魚
          # re: Rule Of Development
          2005-12-20 17:46 | Flyingis
          市面上可以買到一個(gè)小冊子《Java編程規(guī)范》,英文版的,講的也非常詳細(xì),但僅限于Java編碼部分,不包括JSP等。  回復(fù)  更多評論
            
          # re: Rule Of Development
          2005-12-20 17:50 | 胡子魚
          謝謝介紹!  回復(fù)  更多評論
            
          # re: Rule Of Development
          2005-12-20 18:06 | ronghao
          不錯(cuò),仔細(xì)看了一遍,發(fā)現(xiàn)有幾個(gè)地方自己未在意過,流汗:)  回復(fù)  更多評論
            
          # re: Rule Of Development
          2005-12-20 18:26 | TrampEagle
          確實(shí)汗,以前也看過不少規(guī)范文檔,但是都沒有好好遵守,以為自己已經(jīng)夠規(guī)范的了,但是現(xiàn)在看來還有不少細(xì)節(jié)沒有好好重視。一定改!一定要做一個(gè)合格的編程人員。  回復(fù)  更多評論
            
          # re: Rule Of Development
          2005-12-22 15:46 | 老妖
          汗,看來還要加強(qiáng)規(guī)范  回復(fù)  更多評論
            
          # re: Rule Of Development[未登錄]
          2007-03-21 23:45 | 阿蜜果
          呵呵,蠻好,有些東西平時(shí)還真沒有注意!  回復(fù)  更多評論
            
          <2005年12月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(82)

          隨筆分類(59)

          文章分類(21)

          相冊

          收藏夾(40)

          GoodSites

          搜索

          •  

          積分與排名

          • 積分 - 1267403
          • 排名 - 22

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 东乌珠穆沁旗| 安吉县| 龙山县| 井研县| 宁强县| 石泉县| 蓬安县| 鹤峰县| 苍梧县| 资阳市| 江山市| 濮阳县| 大连市| 油尖旺区| 古浪县| 织金县| 吉安县| 洛浦县| 阳东县| 台北县| 长乐市| 扎囊县| 泰州市| 海口市| 和政县| 阜平县| 桑植县| 剑河县| 治多县| 子洲县| 稷山县| 闽清县| 荆门市| 铅山县| 江陵县| 长宁区| 平顺县| 江孜县| 沐川县| 睢宁县| 乌兰察布市|