<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 297,  comments - 1618,  trackbacks - 0
    昨天轉悠到一blog,看到一篇好文。
    原文出處:http://www.tkk7.com/bibi/archive/2007/03/21/24819.html

    1.統一工作目錄

    2.Interface oriented programming

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

    3.變量命名不允許出現下劃線,除了常量命名時用下劃線區分單詞

    String user_name= null;//錯誤!!! 即使數據庫中這種風格
    String userName = null;//正確寫法
    int CET_SIX=6;//常量命名時用下劃線區分單詞,字符全部大寫

    4.代碼中不能出現magic number

    //錯誤!!不能出現如此寫法,需要定義為常量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.不在循環中定義變量

    //sample code snippetfor(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。大家請設置ide的TAB為4 space

    7.使用StringBuffer來替代String + String

    不正確寫法:
    //sample code snippetString 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 來做程序塊區分

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

    正確寫法:
    //IFif ( condition) {
      //code here
    }
    
    //WHILEwhile ( condition ) {
      // code here
    }

    9.Brackets 應當直接在語句后

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

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

    錯誤寫法:
    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 信息"); //注意這里沒有涉及字符串操作
    //涉及字符串操作或者方法調用的,需要用
    logger.isDebugEnable()
    來做判斷
    if(logger.isDebugEnable()){ logger.debug(String1 + string 2 + string3 + object.callMethod()); }

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

    11.異常處理中日志問題

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

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

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

    正確寫法:
    try{
       //handle something
    } catch (Exception ex) {
       log.error("錯誤描述",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出現的nullpointexception
    //sample code snippetString username = …
    …
    if(“mark”.equals(userName)){
    	…
    }

    14.always import classes

    //recommend coding conventionimport 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,加入如下片斷。這樣將可以記錄當前文件的版本以及最后的修改人員

    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使用注意事項

    檢查數據庫中允許為null的欄位

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

    需要完成VO中的equals,hashCode,toString 三個方法

    18.JSP中約定

    為每個input設定好size同maxlength

    <input type="text" maxlength = "10" size="20"/>
    posted on 2007-03-22 11:03 阿蜜果 閱讀(471) 評論(1)  編輯  收藏 所屬分類: Java


    FeedBack:
    # re: Rule Of Development[轉]
    2008-01-08 12:50 | hbyufan@hotmail.com
    受教了  回復  更多評論
      
    <2007年3月>
    25262728123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

          生活將我們磨圓,是為了讓我們滾得更遠——“圓”來如此。
          我的作品:
          玩轉Axure RP  (2015年12月出版)
          

          Power Designer系統分析與建模實戰  (2015年7月出版)
          
         Struts2+Hibernate3+Spring2   (2010年5月出版)
         

    留言簿(263)

    隨筆分類

    隨筆檔案

    文章分類

    相冊

    關注blog

    積分與排名

    • 積分 - 2295092
    • 排名 - 3

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 午夜精品一区二区三区免费视频 | 亚洲heyzo专区无码综合| 国产无遮挡裸体免费视频在线观看 | 99久久精品国产免费| 亚洲成A∨人片在线观看不卡| 一级成人a做片免费| 亚洲午夜精品第一区二区8050| 看成年女人免费午夜视频| 男人的天堂亚洲一区二区三区 | 夜夜嘿视频免费看| 亚洲综合精品第一页| 两个人的视频高清在线观看免费| 亚洲噜噜噜噜噜影院在线播放| 91成年人免费视频| 亚洲无人区码一二三码区别图片| 噜噜嘿在线视频免费观看| 久久久久亚洲精品无码网址色欲| 国产精品无码一区二区三区免费| 美女露隐私全部免费直播| 亚洲中文字幕视频国产| 三上悠亚在线观看免费| 亚洲综合久久1区2区3区| 免费一本色道久久一区| 国产成人精品日本亚洲语音| 亚洲精品一级无码鲁丝片| 大地影院MV在线观看视频免费| 亚洲精品韩国美女在线| 午夜老司机免费视频| 一级片在线免费看| 久久亚洲美女精品国产精品| 在线视频精品免费| 国产成人精品亚洲| 久久精品国产亚洲香蕉| 久久久高清免费视频 | 暖暖在线视频免费视频| 亚洲国产最大av| 国产a v无码专区亚洲av| 在线观看免费中文视频| 国产精品亚洲专区无码不卡| 久久久影院亚洲精品| 韩国18福利视频免费观看|