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

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

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

    ivaneeo's blog

    自由的力量,自由的生活。

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
    范例(Examples)
    我們從一個簡單計算開始:
    double price() {
       //price is base price - quantity discount + shipping
       return _quantity * _itemPrice -
          Math.max(0, _quantity - 500) * _itemPrice * 0.05 +
          Math.min(_quantity * _itemPrice * 0.1, 100.0);
    }

    這段代碼還算簡單,不過我可以讓它變得更容易理解.首先我發現,底價(base price)等于數量(quantity)乘以單價(item price).于是我把這一部分計算的結果放進一個臨時變量中:
    double price() {
       //price is base price - quantity discount + shipping
       final double basePrice = _quantity * _itemPrice;
       return basePrice -
          Math.max(0, _quantity - 500) * _itemPrice * 0.05 +
          Math.min(_quantity * _itemPrice * 0.1, 100.0);
    }

    稍后也用上了[數量乘以單價]運算結果,所以我同樣將它替換為basePrice臨時變量:
    double price() {
       //price is base price - quantity discount + shipping
       final double basePrice = _quantity * _itemPrice;
       return basePrice -
          Math.max(0, _quantity - 500) * _itemPrice * 0.05 +
          Math.min(basePrice * 0.1, 100.0);
    }


    然后,我將批發折扣(quantity discount)的計算提煉出來,將結果賦予臨時變量quantityDiscount:
    double price() {
       //price is base price - quantity discount + shipping
       final double basePrice = _quantity * _itemPrice;
       final double quantityDiscount = Math.max(0, _quantity - 500) * 
                                                          _itemPrice * 0.05;

       return basePrice - quantityDiscount  +
          Math.min(basePrice * 0.1, 100.0);
    }

    最后,我再把運費(shipping)計算提煉出來,將運算結果賦予臨時變量shipping.同時我還可以刪掉代碼中的注釋,因為現在代碼已經可以完美表達自己的意義了:
    double price() {
       //price is base price - quantity discount + shipping
       final double basePrice = _quantity * _itemPrice;
       final double quantityDiscount = Math.max(0, _quantity - 500) * 
                                                          _itemPrice * 0.05;
       final double shipping = Math.min(basePrice * 0.1, 100.0);
       return basePrice - quantityDiscount  + shipping;
    }

    posted on 2005-08-25 16:46 ivaneeo 閱讀(146) 評論(0)  編輯  收藏 所屬分類: refactoring-從地獄中重生
    主站蜘蛛池模板: 亚洲精品人成网在线播放影院| 亚洲大成色www永久网站| 亚洲日韩AV无码一区二区三区人| 69影院毛片免费观看视频在线| 国产精品亚洲精品日韩已满| 成人免费av一区二区三区| 狠狠亚洲婷婷综合色香五月排名| 一级中文字幕乱码免费| 亚洲日本乱码在线观看| 男人都懂www深夜免费网站| 亚洲精品高清久久| 18禁止观看免费私人影院| 亚洲综合精品伊人久久| 国产成人3p视频免费观看| 一级毛片免费全部播放| 亚洲日韩精品一区二区三区无码 | 日本成年免费网站| 国产成人亚洲综合网站不卡| 成人免费视频国产| gogo免费在线观看| 色播亚洲视频在线观看| 又粗又大又黑又长的免费视频| 精品国产日韩久久亚洲| 免费乱码中文字幕网站| 久久er国产精品免费观看2| 亚洲电影在线播放| 四虎永久免费观看| 免费无码又爽又刺激网站| 亚洲人成综合在线播放| 一级毛片直播亚洲| 久久国产免费一区二区三区| 一本色道久久88—综合亚洲精品| 亚洲国产成人精品91久久久| 国产精品区免费视频| 亚洲精品理论电影在线观看| 亚洲人成在线播放网站| 免费无码又爽又刺激聊天APP| 一级毛片成人免费看a| 亚洲精品美女久久久久9999| 四虎影视永久免费观看地址| 少妇无码一区二区三区免费|