成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

開發 前端
過多的if-else不僅導致程序運行效率低下,而且導致代碼圈復雜度過高。如果大家有使用過靜態代碼分析工具,就會知道圈復雜度是衡量代碼質量的一項重要的指標,圈復雜度越高,代碼出現bug的可能性也越大。

[[386493]]

本文轉載自微信公眾號「程序喵大人」,作者程序喵大人。轉載本文請聯系程序喵大人公眾號。

之前我曾經在知乎寫過一篇回答,詳細介紹了if-else的效率問題。

 

過多的if-else不僅導致程序運行效率低下,而且導致代碼圈復雜度過高。如果大家有使用過靜態代碼分析工具,就會知道圈復雜度是衡量代碼質量的一項重要的指標,圈復雜度越高,代碼出現bug的可能性也越大。

我們可能剛開始寫的代碼很簡潔,只有一個if-else分支,但由于需求的疊加和各種錯誤處理,我們有時候不得已要多加幾個if-else,久而久之就發現,滿屏的if-else,令你極其討厭自己寫的代碼。

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

巧用表結構:一般如果某些條件可存儲,可以考慮把條件存起來用于去掉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

可以通過表結構去掉代碼中的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很多情況下都用于錯誤處理,如果我們使用try-catch處理錯誤,是不是就可以消滅if-else了呢,拿數值運算代碼舉例:

  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. }; 

最簡單的可以這樣調用:

  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.   // ... 

但是如果需要處理錯誤,例如除0或者數值溢出等,函數得到的就是錯誤的結果,調用者需要做處理。

先看使用錯誤碼的方式:

  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.   } 

如果有更多的運算,或者有更多的錯誤碼,異常處理的優勢會更明顯。

提前return:對于某些錯誤處理可以考慮提前return,直接看代碼:

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

適當情況下通過反轉if條件就可以刪除掉else分支。

合并分支表達式:有些情況下可以通過合并表達式來消除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

策略模式:熟悉設計模式的同學可能都知道,一般代碼中if-else過多,那就可以考慮使用策略模式啦,例如:

  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.     } // 如何將來需要增加乘法或者除法或者其它運算,還需要增加if-else 
  12.     return 0; 

這種if-else可以通過策略模式進行消除:

  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(); // 這里將來都可以用工廠模式改掉,不會違反開放封閉原則 
  43.     cal2->operation(); 
  44.     delete cal2; 
  45.  
  46.     return 0; 

將來如果有乘法除法和其它運算規則,只需要再加一個繼承基類的子類即可。方便擴展,且遵循設計原則。

職責鏈模式:職責鏈模式盡管不能消滅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. 三目運算符:某些簡單情況下可以使用三目運算符消滅if-else,例如: 
  59. int func(int num) { 
  60.     if (num > 20) return 1; 
  61.     else return 0; 

三目運算符:某些簡單情況下可以使用三目運算符消滅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消除:有時候有些人寫的代碼確實就是這樣,例如:

  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; 

 

責任編輯:武曉燕 來源: 程序喵大人
相關推薦

2021-04-13 06:39:13

代碼重構code

2020-04-20 15:40:03

if-elseJava優化

2020-03-11 08:00:12

if-else優化運算符

2023-06-02 07:30:24

If-else結構流程控制

2013-03-06 10:28:57

ifJava

2021-11-04 08:53:00

if-else代碼Java

2020-10-22 09:20:22

SQLNoSQL 數據庫

2022-07-11 08:16:55

策略模式if-else

2020-04-09 08:29:50

編程語言事件驅動

2021-05-17 14:57:23

策略模式代碼

2020-12-15 09:31:58

CTOif-else代碼

2020-05-13 14:15:25

if-else代碼前端

2025-04-24 08:40:00

JavaScript代碼return語句

2025-06-26 01:10:00

服務定位解析器Spring

2020-09-27 14:24:58

if-else cod業務

2024-06-18 18:36:03

2021-01-29 07:45:27

if-else代碼數據

2022-07-04 08:32:55

Map函數式接口

2023-11-14 08:00:00

Angular前端開發

2024-04-26 08:58:54

if-else代碼JavaSpring
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 97精品超碰一区二区三区 | 特级丰满少妇一级aaaa爱毛片 | 国产免费一级片 | 中文字幕日韩在线观看 | 欧美不卡在线 | 亚洲精品视频三区 | 99热热热| 久操伊人 | 在线免费观看a级片 | 91美女在线观看 | 中文字幕久久久 | 亚洲一区二区三区免费 | av网站在线看 | 人人看人人搞 | 久久99国产精一区二区三区 | 黄色一级片aaa | 亚洲天堂色| 欧美淫片| 欧区一欧区二欧区三免费 | 欧美日日 | 国产a视频 | 精品一区二区久久 | 国产高清精品一区二区三区 | 亚洲精品久久久久中文字幕欢迎你 | 久久黄视频| 黄色香蕉视频在线观看 | 日本欧美黄色片 | 亚洲二区在线 | 精品一区二区三区四区五区 | 久久久久国产一区二区 | 大象一区| 亚洲欧美在线免费观看 | 亚洲精品乱码久久久久久按摩观 | 韩国av网站在线观看 | 九九成人 | 一区2区 | 亚洲女人的天堂 | 久久成人一区 | 伊伊综合网 | 欧美日韩电影一区二区 | 一级毛毛片|