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

滿屏的if-else,怎么消滅它們?

開(kāi)發(fā) 前端
過(guò)多的if-else不僅導(dǎo)致程序運(yùn)行效率低下,而且導(dǎo)致代碼圈復(fù)雜度過(guò)高。如果大家有使用過(guò)靜態(tài)代碼分析工具,就會(huì)知道圈復(fù)雜度是衡量代碼質(zhì)量的一項(xiàng)重要的指標(biāo),圈復(fù)雜度越高,代碼出現(xiàn)bug的可能性也越大。

[[386493]]

本文轉(zhuǎn)載自微信公眾號(hào)「程序喵大人」,作者程序喵大人。轉(zhuǎn)載本文請(qǐng)聯(lián)系程序喵大人公眾號(hào)。

之前我曾經(jīng)在知乎寫(xiě)過(guò)一篇回答,詳細(xì)介紹了if-else的效率問(wèn)題。

 

過(guò)多的if-else不僅導(dǎo)致程序運(yùn)行效率低下,而且導(dǎo)致代碼圈復(fù)雜度過(guò)高。如果大家有使用過(guò)靜態(tài)代碼分析工具,就會(huì)知道圈復(fù)雜度是衡量代碼質(zhì)量的一項(xiàng)重要的指標(biāo),圈復(fù)雜度越高,代碼出現(xiàn)bug的可能性也越大。

我們可能剛開(kāi)始寫(xiě)的代碼很簡(jiǎn)潔,只有一個(gè)if-else分支,但由于需求的疊加和各種錯(cuò)誤處理,我們有時(shí)候不得已要多加幾個(gè)if-else,久而久之就發(fā)現(xiàn),滿屏的if-else,令你極其討厭自己寫(xiě)的代碼。

至于如何消滅if-else,可謂八仙過(guò)海各顯神通,這里介紹幾種常見(jiàn)的方法:

巧用表結(jié)構(gòu):一般如果某些條件可存儲(chǔ),可以考慮把條件存起來(lái)用于去掉if-else,例如:

  1. long long func() { 
  2.     const unsigned ARRAY_SIZE = 50000; 
  3.     int data[ARRAY_SIZE]; 
  4.     const unsigned DATA_STRIDE = 256; 
  5.  
  6.     for (unsigned c = 0; c < ARRAY_SIZE; ++c) data[c] = std::rand() % DATA_STRIDE; 
  7.     long long sum = 0; 
  8.  
  9.     for (unsigned c = 0; c < ARRAY_SIZE; ++c) { 
  10.         if (data[c] >= 128) sum += data[c]; 
  11.     } 
  12.     return sum

可以通過(guò)表結(jié)構(gòu)去掉代碼中的if分支

  1. long long func() { 
  2.     const unsigned ARRAY_SIZE = 50000; 
  3.     int data[ARRAY_SIZE]; 
  4.     const unsigned DATA_STRIDE = 256; 
  5.     int lookup[DATA_STRIDE]; 
  6.     for (unsigned c = 0; c < DATA_STRIDE; ++c) { 
  7.         lookup[c] = (c >= 128) ? c : 0; 
  8.     } 
  9.  
  10.     for (unsigned c = 0; c < ARRAY_SIZE; ++c) data[c] = std::rand() % DATA_STRIDE; 
  11.     long long sum = 0; 
  12.  
  13.     for (unsigned c = 0; c < ARRAY_SIZE; ++c) { 
  14.         sum += lookup[data[c]]; 
  15.     } 
  16.     return sum

使用switch-case替換if-else:一般情況下switch-case比if-else效率高一些,而且邏輯也更清晰,例如:

  1. void func() { 
  2.     if (a == 1) { 
  3.         ... 
  4.     } else if (a == 2) { 
  5.         ... 
  6.     } else if (a == 3) { 
  7.         ... 
  8.     } else if (a == 4) { 
  9.         ... 
  10.     } else { 
  11.         ... 
  12.     } 

try-catch替換:if-else很多情況下都用于錯(cuò)誤處理,如果我們使用try-catch處理錯(cuò)誤,是不是就可以消滅if-else了呢,拿數(shù)值運(yùn)算代碼舉例:

  1. class Number { 
  2. public
  3.   friend Number operator+ (const Number& x, const Number& y); 
  4.   friend Number operator- (const Number& x, const Number& y); 
  5.   friend Number operator* (const Number& x, const Number& y); 
  6.   friend Number operator/ (const Number& x, const Number& y); 
  7.   // ... 
  8. }; 

最簡(jiǎn)單的可以這樣調(diào)用:

  1. void f(Number x, Number y) { 
  2.   // ... 
  3.   Number sum  = x + y; 
  4.   Number diff = x - y; 
  5.   Number prod = x * y; 
  6.   Number quot = x / y; 
  7.   // ... 

但是如果需要處理錯(cuò)誤,例如除0或者數(shù)值溢出等,函數(shù)得到的就是錯(cuò)誤的結(jié)果,調(diào)用者需要做處理。

先看使用錯(cuò)誤碼的方式:

  1. class Number { 
  2. public
  3.   enum ReturnCode { 
  4.     Success, 
  5.     Overflow, 
  6.     Underflow, 
  7.     DivideByZero 
  8.   }; 
  9.   Number add(const Number& y, ReturnCode& rc) const; 
  10.   Number sub(const Number& y, ReturnCode& rc) const; 
  11.   Number mul(const Number& y, ReturnCode& rc) const; 
  12.   Number div(const Number& y, ReturnCode& rc) const; 
  13.   // ... 
  14. }; 
  15.  
  16. int f(Number x, Number y) 
  17.   // ... 
  18.   Number::ReturnCode rc; 
  19.   Number sum = x.add(y, rc); 
  20.   if (rc == Number::Overflow) { 
  21.     // ...code that handles overflow... 
  22.     return -1; 
  23.   } else if (rc == Number::Underflow) { 
  24.     // ...code that handles underflow... 
  25.     return -1; 
  26.   } else if (rc == Number::DivideByZero) { 
  27.     // ...code that handles divide-by-zero... 
  28.     return -1; 
  29.   } 
  30.   Number diff = x.sub(y, rc); 
  31.   if (rc == Number::Overflow) { 
  32.     // ...code that handles overflow... 
  33.     return -1; 
  34.   } else if (rc == Number::Underflow) { 
  35.     // ...code that handles underflow... 
  36.     return -1; 
  37.   } else if (rc == Number::DivideByZero) { 
  38.     // ...code that handles divide-by-zero... 
  39.     return -1; 
  40.   } 
  41.   Number prod = x.mul(y, rc); 
  42.   if (rc == Number::Overflow) { 
  43.     // ...code that handles overflow... 
  44.     return -1; 
  45.   } else if (rc == Number::Underflow) { 
  46.     // ...code that handles underflow... 
  47.     return -1; 
  48.   } else if (rc == Number::DivideByZero) { 
  49.     // ...code that handles divide-by-zero... 
  50.     return -1; 
  51.   } 
  52.   Number quot = x.div(y, rc); 
  53.   if (rc == Number::Overflow) { 
  54.     // ...code that handles overflow... 
  55.     return -1; 
  56.   } else if (rc == Number::Underflow) { 
  57.     // ...code that handles underflow... 
  58.     return -1; 
  59.   } else if (rc == Number::DivideByZero) { 
  60.     // ...code that handles divide-by-zero... 
  61.     return -1; 
  62.   } 
  63.   // ... 

再看使用異常處理的方式:

  1. void f(Number x, Number y) 
  2.   try { 
  3.     // ... 
  4.     Number sum  = x + y; 
  5.     Number diff = x - y; 
  6.     Number prod = x * y; 
  7.     Number quot = x / y; 
  8.     // ... 
  9.   } 
  10.   catch (Number::Overflow& exception) { 
  11.     // ...code that handles overflow... 
  12.   } 
  13.   catch (Number::Underflow& exception) { 
  14.     // ...code that handles underflow... 
  15.   } 
  16.   catch (Number::DivideByZero& exception) { 
  17.     // ...code that handles divide-by-zero... 
  18.   } 

如果有更多的運(yùn)算,或者有更多的錯(cuò)誤碼,異常處理的優(yōu)勢(shì)會(huì)更明顯。

提前return:對(duì)于某些錯(cuò)誤處理可以考慮提前return,直接看代碼:

  1. void func(A *a) { 
  2.     if (a) { 
  3.         ... 
  4.     } else { 
  5.         log_error(...); 
  6.         return
  7.     } 

適當(dāng)情況下通過(guò)反轉(zhuǎn)if條件就可以刪除掉else分支。

合并分支表達(dá)式:有些情況下可以通過(guò)合并表達(dá)式來(lái)消除if-else,例如:

  1. void func() { 
  2.     if (a < 20) return
  3.     if (b > 30) return
  4.     if (c < 18) return

可以改為

  1. void func() { 
  2.     if (a < 20 || b > 30 || c < 18) return

策略模式:熟悉設(shè)計(jì)模式的同學(xué)可能都知道,一般代碼中if-else過(guò)多,那就可以考慮使用策略模式啦,例如:

  1. enum class CalOperation { 
  2.     add
  3.     sub 
  4. }; 
  5.  
  6. int NoStragegy(CalOperation ope) { 
  7.     if (ope == CalOperation::add) { 
  8.         std::cout << "this is add operation" << std::endl; 
  9.     } else if (ope == CalOperation::sub) { 
  10.         std::cout << "this is sub operation" << std::endl; 
  11.     } // 如何將來(lái)需要增加乘法或者除法或者其它運(yùn)算,還需要增加if-else 
  12.     return 0; 

這種if-else可以通過(guò)策略模式進(jìn)行消除:

  1. #ifndef __CALCULATION__ 
  2. #define __CALCULATION__ 
  3.  
  4. #include <iostream> 
  5.  
  6. class Calculation { 
  7.    public
  8.     Calculation() {} 
  9.  
  10.     virtual ~Calculation() {} 
  11.  
  12.     virtual void operation() { std::cout << "base operation" << std::endl; } 
  13. }; 
  14.  
  15. #endif 
  16.  
  17. #ifndef __ADD__ 
  18. #define __ADD__ 
  19.  
  20. #include "calculation.h" 
  21.  
  22. class Add : public Calculation { 
  23.     void operation() override { std::cout << "this is add operation" << std::endl; } 
  24. }; 
  25.  
  26. #endif 
  27. #ifndef __SUB__ 
  28. #define __SUB__ 
  29.  
  30. #include "calculation.h" 
  31.  
  32. class Sub : public Calculation { 
  33.     void operation() override { std::cout << "this is sub operation" << std::endl; } 
  34. }; 
  35.  
  36. #endif 
  37. int Stragegy() { 
  38.     Calculation *cal = new Add(); 
  39.     cal->operation(); 
  40.     delete cal; 
  41.  
  42.     Calculation *cal2 = new Sub(); // 這里將來(lái)都可以用工廠模式改掉,不會(huì)違反開(kāi)放封閉原則 
  43.     cal2->operation(); 
  44.     delete cal2; 
  45.  
  46.     return 0; 

將來(lái)如果有乘法除法和其它運(yùn)算規(guī)則,只需要再加一個(gè)繼承基類(lèi)的子類(lèi)即可。方便擴(kuò)展,且遵循設(shè)計(jì)原則。

職責(zé)鏈模式:職責(zé)鏈模式盡管不能消滅if-else,但它可以用于改良if-else,使其更靈活,例如:

  1. #include <iostream> 
  2.  
  3. using std::cout; 
  4.  
  5. void func(int num) { 
  6.     if (num >= 0 && num <= 10) { 
  7.         cout << "0-10 \n"
  8.     } else if (num > 10 && num <= 20) { 
  9.         cout << "10-20 \n"
  10.     } else if (num > 20 && num <= 30) { 
  11.         cout << "20-30 \n"
  12.     } else if (num > 30 && num <= 40) { 
  13.         cout << "30-40 \n"
  14.     } else if (num > 40 && num <= 50) { 
  15.         cout << "40-50 \n"
  16.     } else if (num > 50 && num <= 60) { 
  17.         cout << "50-60 \n"
  18.     } else { 
  19.         cout << "not handle \n"
  20.     } 
  21.  
  22. int main() { 
  23.     func(25); 
  24.     func(43); 
  25.     return 0; 

可以考慮改為下面的形式:

  1. #include <iostream> 
  2.  
  3. using std::cout; 
  4.  
  5. struct Handle { 
  6.     virtual void process(int num) {} 
  7. }; 
  8.  
  9. struct Handle1 : public Handle { 
  10.     Handle1(Handle *processor) : processor_(processor) {} 
  11.  
  12.     void process(int num) override { 
  13.         if (num >= 0 && num <= 10) { 
  14.             cout << "0-10 \n"
  15.         } else { 
  16.             processor_->process(num); 
  17.         } 
  18.     } 
  19.  
  20.     Handle *processor_; 
  21. }; 
  22.  
  23. struct Handle2 : public Handle { 
  24.     Handle2(Handle *processor) : processor_(processor) {} 
  25.  
  26.     void process(int num) override { 
  27.         if (num >= 10 && num <= 20) { 
  28.             cout << "10-20 \n"
  29.         } else { 
  30.             processor_->process(num); 
  31.         } 
  32.     } 
  33.  
  34.     Handle *processor_; 
  35. }; 
  36.  
  37. struct Handle3 : public Handle { 
  38.     Handle3(Handle *processor) : processor_(processor) {} 
  39.  
  40.     void process(int num) override { 
  41.         if (num >= 20 && num <= 30) { 
  42.             cout << "20-30 \n"
  43.         } else { 
  44.             cout << "not handle \n"
  45.         } 
  46.     } 
  47.  
  48.     Handle *processor_; 
  49. }; 
  50.  
  51. int main() { 
  52.     Handle *handle3 = new Handle3(nullptr); 
  53.     Handle *handle2 = new Handle2(handle3); 
  54.     Handle *handle1 = new Handle2(handle2); 
  55.     handle1->process(24); 
  56.     handle1->process(54); 
  57.     return 0; 
  58. 三目運(yùn)算符:某些簡(jiǎn)單情況下可以使用三目運(yùn)算符消滅if-else,例如: 
  59. int func(int num) { 
  60.     if (num > 20) return 1; 
  61.     else return 0; 

三目運(yùn)算符:某些簡(jiǎn)單情況下可以使用三目運(yùn)算符消滅if-else,例如:

  1. int func(int num) { 
  2.     if (num > 20) return 1; 
  3.     else return 0; 

可以改為:

  1. int func(int num) { 
  2.     return num > 20 ? 1 : 0; 

這樣是不是代碼也更清晰了一些。

else-if消除:有時(shí)候有些人寫(xiě)的代碼確實(shí)就是這樣,例如:

  1. int func(int num) { 
  2.     int ret = 0; 
  3.     if (num == 1) { 
  4.         ret = 3; 
  5.     } else if (num == 2) { 
  6.         ret = 5; 
  7.     } else { 
  8.         ret = 6; 
  9.     } 
  10.     return ret; 
  11. }  

是不是可以考慮改為:

  1. int func(int num) { 
  2.     if (num == 1) return 3; 
  3.     if (num == 2) return 5; 
  4.     return 6; 

 

責(zé)任編輯:武曉燕 來(lái)源: 程序喵大人
相關(guān)推薦

2021-04-13 06:39:13

代碼重構(gòu)code

2020-04-20 15:40:03

if-elseJava優(yōu)化

2020-03-11 08:00:12

if-else優(yōu)化運(yùn)算符

2023-06-02 07:30:24

If-else結(jié)構(gòu)流程控制

2013-03-06 10:28:57

ifJava

2021-11-04 08:53:00

if-else代碼Java

2020-10-22 09:20:22

SQLNoSQL 數(shù)據(jù)庫(kù)

2022-07-11 08:16:55

策略模式if-else

2020-04-09 08:29:50

編程語(yǔ)言事件驅(qū)動(dòng)

2020-12-15 09:31:58

CTOif-else代碼

2020-05-13 14:15:25

if-else代碼前端

2025-04-24 08:40:00

JavaScript代碼return語(yǔ)句

2021-05-17 14:57:23

策略模式代碼

2021-01-29 07:45:27

if-else代碼數(shù)據(jù)

2024-06-18 18:36:03

2020-09-27 14:24:58

if-else cod業(yè)務(wù)

2022-07-04 08:32:55

Map函數(shù)式接口

2024-04-26 08:58:54

if-else代碼JavaSpring

2023-11-14 08:00:00

Angular前端開(kāi)發(fā)

2024-03-25 10:00:00

C++編程else
點(diǎn)贊
收藏

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