<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### Remove Control Flag(移除控制標記) 在一系列布爾表達式(boolean expressions)中,某個變量帶有「控制標記」(control flag)的作用。 以break 語句或return 的語句取代控制標記。 **動機(Motivation)** 在一系列條件表達式中,你常常會看到「用以判斷何時停止條件檢查」的控制標記(control flag): ~~~ set done to false while not done if (condition) do something set done to true next step of loop ~~~ 這樣的控制標記帶來的麻煩超過了它所帶來的便利。人們之所以會使用這樣的控制標記,因為結構化編程原則告訴他們:每個子程序(routines)只能有一個入口(entry) 和一個出口(exit)。我贊同「單一入口」原則(而且現代編程語言也強迫我們這樣做),但是「單一出口」原則會讓你在代碼中加入討厭的控制標記,大大降低條件表達式的可讀性。這就是編程語言提供break 語句和continue 語句的原因:你可以用它們跳出復雜的條件語句。去掉控制標記所產生的效果往往讓你大吃一驚:條件語句真正的用途會清晰得多。 **作法(Mechanics)** 對控制標記(control flags)的處理,最顯而易見的辦法就是使用Java 提供的break 語句或continue 語句。 - 找出「讓你得以跳出這段邏輯」的控制標記值。 - 找出「將可跳出條件式之值賦予標記變量」的那個語句,代以恰當的break 語句或continue 語句。 - 每次替換后,編譯并測試。 在未能提供break 和continue 語句的編程語言中,我們可以使用另一種辦法: - 運用Extract Method,將整段邏輯提煉到一個獨立函數中。 - 找出「讓你得以跳出這段邏輯」的那些控制標記值。 - 找出「將可跳出條件式之值賦予標記變量」的那個語句,代以恰當的return 語句。 - 每次替換后,編譯并測試。 即使在支持break 和continue 語句的編程語言中,我通常也優先考慮上述第二方案。因為return 語句可以非常清楚地表示:不再執行該函數中的其他任何代碼。 如果還有這一類代碼,你早晚需要將這段代碼提煉出來。 請注意標記變量是否會影響這段邏輯的最后結果。如果有影響,使用break 語句之后你還得保留控制標記值。如果你已經將這段邏輯提煉成一個獨立函數,也可以將控制標記值放在return 語句中返回。 **范例:以break 取代簡單的控制標記** 下列函數用來檢查一系列人名之中是否包含兩個可疑人物的名字(這兩個人的名字硬編碼于代碼中〕: ~~~ void checkSecurity(String[] people) { boolean found = false; for (int i = 0; i < people.length; i++) { if (! found) { if (people[i].equals ("Don")){ sendAlert(); found = true; } if (people[i].equals ("John")){ sendAlert(); found = true; } } } } ~~~ 這種情況下很容易找出控制標記:當變量found 被賦予true 時,搜索就結束。我可以逐一引入break 語句: ~~~ void checkSecurity(String[] people) { boolean found = false; for (int i = 0; i < people.length; i++) { if (! found) { if (people[i].equals ("Don")){ sendAlert(); break; } if (people[i].equals ("John")){ sendAlert(); found = true; } } } } ~~~ 最后獲得這樣的成功: ~~~ void checkSecurity(String[] people) { boolean found = false; for (int i = 0; i < people.length; i++) { if (! found) { if (people[i].equals ("Don")){ sendAlert(); break; } if (people[i].equals ("John")){ sendAlert(); break; } } } } ~~~ 然后我就可以把對控制標記的所有引用去掉: ~~~ void checkSecurity(String[] people) { for (int i = 0; i < people.length; i++) { if (people[i].equals ("Don")){ sendAlert(); break; } if (people[i].equals ("John")){ sendAlert(); break; } } } ~~~ **范例:以return 返回控制標記** 本項重構的另一種形式將使用return 語句。為了闡述這種用法,我把前面的例子稍加修改,以控制標記記錄搜索結果: ~~~ void checkSecurity(String[] people) { String found = ""; for (int i = 0; i < people.length; i++) { if (found.equals("")) { if (people[i].equals ("Don")){ sendAlert(); found = "Don"; } if (people[i].equals ("John")){ sendAlert(); found = "John"; } } } someLaterCode(found); } ~~~ 在這里,變量found 做了兩件事:它既是控制標記,也是運算結果。遇到這種情況,我喜歡先把計算found 變量的代碼提煉到一個獨立函數中: ~~~ void checkSecurity(String[] people) { String found = foundMiscreant(people); someLaterCode(found); } String foundMiscreant(String[] people){ String found = ""; for (int i = 0; i < people.length; i++) { if (found.equals("")) { if (people[i].equals ("Don")){ sendAlert(); found = "Don"; } if (people[i].equals ("John")){ sendAlert(); found = "John"; } } } return found; } ~~~ 然后以return 語句取代控制標記: ~~~ String foundMiscreant(String[] people){ String found = ""; for (int i = 0; i < people.length; i++) { if (found.equals("")) { if (people[i].equals ("Don")){ sendAlert(); return "Don"; } if (people[i].equals ("John")){ sendAlert(); found = "John"; } } } return found; } ~~~ 最后完全去掉控制標記: ~~~ String foundMiscreant(String[] people){ for (int i = 0; i < people.length; i++) { if (people[i].equals ("Don")){ sendAlert(); return "Don"; } if (people[i].equals ("John")){ sendAlert(); return "John"; } } return ""; } ~~~ 即使不需要返回某值,你也可以使用語句來取代控制標記。這時候你只需 要一個空的return 語句就行了。 當然,如果以此辦法去處理帶有副作用(連帶影響)的函數,會有一些問題。所以我需要先以 Separate Query from Modifier 將函數副作用分離出去。稍后你會看到這方面的例子。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看