自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

是時候和else關(guān)鍵字說再見了……

開發(fā) 后端
沒有程序員不知道else關(guān)鍵字,If-else幾乎遍布于所有編程語言,這種簡單的條件邏輯使所有人都很容易理解。但優(yōu)秀程序員的標(biāo)志是,不使用這個關(guān)鍵字。

本文轉(zhuǎn)載自公眾號“讀芯術(shù)”(ID:AI_Discovery)

沒有程序員不知道else關(guān)鍵字,If-else幾乎遍布于所有編程語言,這種簡單的條件邏輯使所有人都很容易理解。

[[321176]]

但優(yōu)秀程序員的標(biāo)志是,不使用這個關(guān)鍵字。

筆者在開始編程的時候,最大錯誤之一是在編寫條件句時過度使用else關(guān)鍵字,早五年前筆者就告別else了。

原因何在呢?

想一下else是什么意思,其意為“如果滿足A就執(zhí)行這個,如果不滿足A就執(zhí)行那個”。

是時候和else關(guān)鍵字說再見了……

圖源:bevnet

如果A是二進(jìn)制,就不存在問題——因為只存在兩種情況。

但是如果A是二進(jìn)制變量的集合,或者包含著更大的變量,出現(xiàn)問題的機(jī)會就可能會出乎意料的大,且難以理解、測試和維護(hù)。

避免if/else if,只使用if語句,花時間確保if組的輸入條件是互斥的,這樣答案就不依賴于執(zhí)行順序了。

  • 使用switch — case語句
  • 使用多態(tài)性處理復(fù)雜的條件情況,使代碼更像狀態(tài)模式。
  • 其保證了主要的執(zhí)行通道,且有著更少的特殊情況。
  • 其迫使編程人員在每個函數(shù)開始時寫入處理數(shù)據(jù)所需的所有條件。

示例

例子是這樣的:一個信號燈(即信號燈對象)有著三種不同的狀態(tài),紅色、黃色和綠色,每種狀態(tài)都有著其自己的一系列規(guī)則。規(guī)則如下:

  • 假設(shè)信號燈目前是紅色,則在一定延遲后,狀態(tài)由紅轉(zhuǎn)綠。
  • 然后在另一個延遲之后,狀態(tài)由綠轉(zhuǎn)黃。
  • 短暫延遲后,狀態(tài)由黃轉(zhuǎn)紅。
  • 不斷循環(huán)

不要使用if-else關(guān)鍵字

  1. constLightState= { 
  2.            GREEN: 0, 
  3.            YELLOW: 1, 
  4.            RED: 2 
  5.          } 
  6.                       varTrafficLight=function () { 
  7.                         var count =0  
  8.                         // default state = red 
  9.            var currentState =0
  10.                        this.change=function(state) { 
  11.              if (count++ >= 10 ) return 
  12.              currentState = state 
  13.              this.go(currentState) 
  14.            } 
  15.            this.go=function(state) { 
  16.              if (currentState ==LightState.GREEN) { 
  17.                console.log("Green -->for 1 minute") 
  18.                this.change(LightState.YELLOW) 
  19.              } 
  20.              elseif (currentState ==LightState.YELLOW) { 
  21.                console.log("Yellow -->for 10 seconds") 
  22.                this.change(LightState.RED) 
  23.              } elseif (currentState ==LightState.RED) { 
  24.                console.log("Red -->for 1 minute"); 
  25.                this.change(LightState.GREEN) 
  26.              } else { 
  27.                throwError("Invalid State") 
  28.              } 
  29.            } 
  30.            this.start=function() { 
  31.              this.change(LightState.GREEN) 
  32.            } 
  33.          } 

更簡單的方式

來看看不用else該怎么做:

  1. this.go=function (state) { 
  2.             if (currentState ==LightState.GREEN) { 
  3.               console.log("Green -->for 1 minute") 
  4.               this.change(LightState.YELLOW) 
  5.             } 
  6.             if (currentState ==LightState.YELLOW) { 
  7.               console.log("Yellow -->for 10 seconds") 
  8.               this.change(LightState.RED) 
  9.             } 
  10.             if (currentState ==LightState.RED) { 
  11.               console.log("Red -->for 1 minute"); 
  12.               this.change(LightState.GREEN) 
  13.             } 
  14.             if (currentState != LightState.GREEN&& currentState != LightState.RED&& currentState != LightState.YELLOW) { 
  15.               throwError("Invalid State") 
  16.             } 
  17.         } 

或者可以用一個switch代替,不得不合并不同的場景時,它看起來干凈得多,而if-else很快就會失控。

若干場景良好的情況下,switch 語句可能會比if-else語句更快。

  1. this.go=function (state) { 
  2.             if (currentState ==LightState.GREEN) { 
  3.               console.log("Green -->for 1 minute") 
  4.               this.change(LightState.YELLOW) 
  5.             } 
  6.             if (currentState ==LightState.YELLOW) { 
  7.               console.log("Yellow -->for 10 seconds") 
  8.               this.change(LightState.RED) 
  9.             } 
  10.             if (currentState ==LightState.RED) { 
  11.               console.log("Red -->for 1 minute"); 
  12.               this.change(LightState.GREEN) 
  13.             } 
  14.             if (currentState != LightState.GREEN&& currentState != LightState.RED&& currentState != LightState.YELLOW) { 
  15.               throwError("Invalid State") 
  16.             } 
  17.         } 

可以使用狀態(tài)模式刪除這些代碼中的所有if-else關(guān)鍵字

[[321178]]

圖源:unsplash

在這里,引入了許多if-else block/switch語句來保護(hù)各種條件,這個狀態(tài)模式適合這樣的場景。它允許對象根據(jù)當(dāng)前的狀態(tài)有不同的行為,并且用戶可以定義狀態(tài)特定的行為。

在這種模式下,開始考慮信號燈的可能狀態(tài),然后相應(yīng)地隔離代碼。

  • 對于狀態(tài)特定的行為,需要有單獨的對象。
  • 信號燈中定義的操作將行為委托給當(dāng)前狀態(tài)的對象。
  • 狀態(tài)本身觸發(fā)狀態(tài)轉(zhuǎn)換

信號燈:Green(1 minute) → Yellow (10 seconds)→ Red (1 minute)

  1. varTrafficLight=function () { 
  2.                              var count =0 
  3.                              // default state =green 
  4.                var currentState =newGreen(this);  
  5.                              this.change=function (state) { 
  6.                    // limits number of changes 
  7.                    if (count++ >= 10) return; 
  8.                    currentState = state
  9.                    currentState.go(); 
  10.                } 
  11.                this.start=function () { 
  12.                    currentState.go(); 
  13.                } 
  14.            }  
  15.                           varRed=function (light) { 
  16.                this.light= light 
  17.                              this.go=function () { 
  18.                    console.log(("Red -->for 1 minute")) 
  19.                    light.change(newGreen(light)); 
  20.                } 
  21.            }  
  22.                           varYellow=function (light) { 
  23.                this.light= light; 
  24.              
  25.                this.go=function () { 
  26.                    console.log("Yellow -->for 10 seconds") 
  27.                    light.change(newRed(light)); 
  28.                } 
  29.            }; 
  30.                              varGreen=function (light) { 
  31.                this.light= light; 
  32.              
  33.                this.go=function () { 
  34.                    console.log("Green -->for 1 minute"); 
  35.                    light.change(newYellow(light)); 
  36.                } 
  37.            }; 

輸出如下:

  1. Green → for 1 minute 
  2. Yellow → for 10 seconds 
  3. Red → for 1 minute 
  4. Green → for 1 minute 
  5. Yellow → for 10 seconds 
  6. Red → for 1 minute 
  7. Green → for 1 minute 
  8. Yellow → for 10 seconds 
  9. Red → for 1 minute 
  10. Green → for 1 minute 
  11. Yellow → for 10 seconds 

好代碼與糟糕代碼的區(qū)別在哪,你get到了嗎?

責(zé)任編輯:趙寧寧 來源: 讀芯術(shù)
相關(guān)推薦

2023-02-26 00:17:45

2020-07-13 20:26:30

手機(jī)64G手機(jī)APP

2025-02-19 08:27:56

2013-12-20 09:59:34

小米閃購模式雷軍

2024-11-08 14:00:29

關(guān)鍵字Animal編程

2021-07-21 07:11:21

TeamviewerWindowsMac

2019-10-10 10:30:26

MVCModelController

2013-03-19 11:28:01

Windows 7 R

2021-01-12 09:22:18

Synchronize線程開發(fā)技術(shù)

2013-08-13 14:22:33

開發(fā)者微軟Windows Pho

2019-05-09 10:48:46

無人駕駛人工智能配送機(jī)器人

2014-07-14 11:47:03

火狐瀏覽器

2015-10-10 11:08:36

控制面板Windows 10微軟

2009-08-01 08:46:47

2009-08-21 14:58:56

C# this關(guān)鍵字

2013-01-30 10:12:14

Pythonyield

2018-04-20 15:56:09

Pythonglobal關(guān)鍵字

2009-09-02 09:24:03

C# this關(guān)鍵字

2009-09-17 09:30:00

Linq LET關(guān)鍵字

2012-03-01 12:50:03

Java
點贊
收藏

51CTO技術(shù)棧公眾號