ivaneeo's blog

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

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks
          范例(Examples)
          我從下列這段簡單代碼開始:
          int discount(int inputVal, int quantity, int yearToDate) {
              if(inputVal > 50) inputVal -= 2;
              if(quantity > 100) inputVal -= 1;
              if(yearToDate > 1000) inputVal -= 4;
              return inputVal;
          }
          以臨時變量取代對參數的賦值動作,得到下列代碼:
           int discount(int inputVal, int quantity, int yearToDate) {
              int result = inputVal;
              if(inputVal > 50)
          result -= 2;
              if(quantity > 100) result -= 1;
              if(yearToDate > 1000) result -= 4;
              return result
          }
          還可以為參數加上關鍵詞final,從而強制它遵循[不對參數賦值]這一慣例:
            int discount(final int inputVal, final int quantity, final int yearToDate) {
              int result = inputVal;
              if(inputVal > 50)
          result -= 2;
              if(quantity > 100) result -= 1;
              if(yearToDate > 1000) result -= 4;
              return result
          }

          不過我的承認,我并不經常使用final來修飾參數,因為我發現,對于提高短函數的清晰度,這個辦法并無太大幫助。我通常會在較長的函數中使用它,讓它幫助我檢查參數是否被做了修改。
          posted on 2005-08-29 15:40 ivaneeo 閱讀(208) 評論(0)  編輯  收藏 所屬分類: refactoring-從地獄中重生
          主站蜘蛛池模板: 卢龙县| 灵璧县| 胶州市| 北辰区| 华池县| 定南县| 南江县| 嘉黎县| 黄大仙区| 闽清县| 来凤县| 大埔区| 横山县| 滦南县| 库车县| 色达县| 泰和县| 吉林市| 望都县| 甘肃省| 丹阳市| 闽清县| 临西县| 彭水| 资源县| 保定市| 台江县| 北票市| 苍梧县| 伽师县| 封丘县| 和硕县| 阳高县| 罗田县| 晋宁县| 比如县| 合水县| 昆明市| 左贡县| 尖扎县| 延安市|