滿屏的if-else,怎么消滅它們?
本文轉(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,例如:
- long long func() {
- const unsigned ARRAY_SIZE = 50000;
- int data[ARRAY_SIZE];
- const unsigned DATA_STRIDE = 256;
- for (unsigned c = 0; c < ARRAY_SIZE; ++c) data[c] = std::rand() % DATA_STRIDE;
- long long sum = 0;
- for (unsigned c = 0; c < ARRAY_SIZE; ++c) {
- if (data[c] >= 128) sum += data[c];
- }
- return sum;
- }
可以通過(guò)表結(jié)構(gòu)去掉代碼中的if分支
- long long func() {
- const unsigned ARRAY_SIZE = 50000;
- int data[ARRAY_SIZE];
- const unsigned DATA_STRIDE = 256;
- int lookup[DATA_STRIDE];
- for (unsigned c = 0; c < DATA_STRIDE; ++c) {
- lookup[c] = (c >= 128) ? c : 0;
- }
- for (unsigned c = 0; c < ARRAY_SIZE; ++c) data[c] = std::rand() % DATA_STRIDE;
- long long sum = 0;
- for (unsigned c = 0; c < ARRAY_SIZE; ++c) {
- sum += lookup[data[c]];
- }
- return sum;
- }
使用switch-case替換if-else:一般情況下switch-case比if-else效率高一些,而且邏輯也更清晰,例如:
- void func() {
- if (a == 1) {
- ...
- } else if (a == 2) {
- ...
- } else if (a == 3) {
- ...
- } else if (a == 4) {
- ...
- } else {
- ...
- }
- }
try-catch替換:if-else很多情況下都用于錯(cuò)誤處理,如果我們使用try-catch處理錯(cuò)誤,是不是就可以消滅if-else了呢,拿數(shù)值運(yùn)算代碼舉例:
- class Number {
- public:
- friend Number operator+ (const Number& x, const Number& y);
- friend Number operator- (const Number& x, const Number& y);
- friend Number operator* (const Number& x, const Number& y);
- friend Number operator/ (const Number& x, const Number& y);
- // ...
- };
最簡(jiǎn)單的可以這樣調(diào)用:
- void f(Number x, Number y) {
- // ...
- Number sum = x + y;
- Number diff = x - y;
- Number prod = x * y;
- Number quot = x / y;
- // ...
- }
但是如果需要處理錯(cuò)誤,例如除0或者數(shù)值溢出等,函數(shù)得到的就是錯(cuò)誤的結(jié)果,調(diào)用者需要做處理。
先看使用錯(cuò)誤碼的方式:
- class Number {
- public:
- enum ReturnCode {
- Success,
- Overflow,
- Underflow,
- DivideByZero
- };
- Number add(const Number& y, ReturnCode& rc) const;
- Number sub(const Number& y, ReturnCode& rc) const;
- Number mul(const Number& y, ReturnCode& rc) const;
- Number div(const Number& y, ReturnCode& rc) const;
- // ...
- };
- int f(Number x, Number y)
- {
- // ...
- Number::ReturnCode rc;
- Number sum = x.add(y, rc);
- if (rc == Number::Overflow) {
- // ...code that handles overflow...
- return -1;
- } else if (rc == Number::Underflow) {
- // ...code that handles underflow...
- return -1;
- } else if (rc == Number::DivideByZero) {
- // ...code that handles divide-by-zero...
- return -1;
- }
- Number diff = x.sub(y, rc);
- if (rc == Number::Overflow) {
- // ...code that handles overflow...
- return -1;
- } else if (rc == Number::Underflow) {
- // ...code that handles underflow...
- return -1;
- } else if (rc == Number::DivideByZero) {
- // ...code that handles divide-by-zero...
- return -1;
- }
- Number prod = x.mul(y, rc);
- if (rc == Number::Overflow) {
- // ...code that handles overflow...
- return -1;
- } else if (rc == Number::Underflow) {
- // ...code that handles underflow...
- return -1;
- } else if (rc == Number::DivideByZero) {
- // ...code that handles divide-by-zero...
- return -1;
- }
- Number quot = x.div(y, rc);
- if (rc == Number::Overflow) {
- // ...code that handles overflow...
- return -1;
- } else if (rc == Number::Underflow) {
- // ...code that handles underflow...
- return -1;
- } else if (rc == Number::DivideByZero) {
- // ...code that handles divide-by-zero...
- return -1;
- }
- // ...
- }
再看使用異常處理的方式:
- void f(Number x, Number y)
- {
- try {
- // ...
- Number sum = x + y;
- Number diff = x - y;
- Number prod = x * y;
- Number quot = x / y;
- // ...
- }
- catch (Number::Overflow& exception) {
- // ...code that handles overflow...
- }
- catch (Number::Underflow& exception) {
- // ...code that handles underflow...
- }
- catch (Number::DivideByZero& exception) {
- // ...code that handles divide-by-zero...
- }
如果有更多的運(yùn)算,或者有更多的錯(cuò)誤碼,異常處理的優(yōu)勢(shì)會(huì)更明顯。
提前return:對(duì)于某些錯(cuò)誤處理可以考慮提前return,直接看代碼:
- void func(A *a) {
- if (a) {
- ...
- } else {
- log_error(...);
- return;
- }
- }
適當(dāng)情況下通過(guò)反轉(zhuǎn)if條件就可以刪除掉else分支。
合并分支表達(dá)式:有些情況下可以通過(guò)合并表達(dá)式來(lái)消除if-else,例如:
- void func() {
- if (a < 20) return;
- if (b > 30) return;
- if (c < 18) return;
- }
可以改為
- void func() {
- if (a < 20 || b > 30 || c < 18) return;
- }
策略模式:熟悉設(shè)計(jì)模式的同學(xué)可能都知道,一般代碼中if-else過(guò)多,那就可以考慮使用策略模式啦,例如:
- enum class CalOperation {
- add,
- sub
- };
- int NoStragegy(CalOperation ope) {
- if (ope == CalOperation::add) {
- std::cout << "this is add operation" << std::endl;
- } else if (ope == CalOperation::sub) {
- std::cout << "this is sub operation" << std::endl;
- } // 如何將來(lái)需要增加乘法或者除法或者其它運(yùn)算,還需要增加if-else
- return 0;
- }
這種if-else可以通過(guò)策略模式進(jìn)行消除:
- #ifndef __CALCULATION__
- #define __CALCULATION__
- #include <iostream>
- class Calculation {
- public:
- Calculation() {}
- virtual ~Calculation() {}
- virtual void operation() { std::cout << "base operation" << std::endl; }
- };
- #endif
- #ifndef __ADD__
- #define __ADD__
- #include "calculation.h"
- class Add : public Calculation {
- void operation() override { std::cout << "this is add operation" << std::endl; }
- };
- #endif
- #ifndef __SUB__
- #define __SUB__
- #include "calculation.h"
- class Sub : public Calculation {
- void operation() override { std::cout << "this is sub operation" << std::endl; }
- };
- #endif
- int Stragegy() {
- Calculation *cal = new Add();
- cal->operation();
- delete cal;
- Calculation *cal2 = new Sub(); // 這里將來(lái)都可以用工廠模式改掉,不會(huì)違反開(kāi)放封閉原則
- cal2->operation();
- delete cal2;
- return 0;
- }
將來(lái)如果有乘法除法和其它運(yùn)算規(guī)則,只需要再加一個(gè)繼承基類(lèi)的子類(lèi)即可。方便擴(kuò)展,且遵循設(shè)計(jì)原則。
職責(zé)鏈模式:職責(zé)鏈模式盡管不能消滅if-else,但它可以用于改良if-else,使其更靈活,例如:
- #include <iostream>
- using std::cout;
- void func(int num) {
- if (num >= 0 && num <= 10) {
- cout << "0-10 \n";
- } else if (num > 10 && num <= 20) {
- cout << "10-20 \n";
- } else if (num > 20 && num <= 30) {
- cout << "20-30 \n";
- } else if (num > 30 && num <= 40) {
- cout << "30-40 \n";
- } else if (num > 40 && num <= 50) {
- cout << "40-50 \n";
- } else if (num > 50 && num <= 60) {
- cout << "50-60 \n";
- } else {
- cout << "not handle \n";
- }
- }
- int main() {
- func(25);
- func(43);
- return 0;
- }
可以考慮改為下面的形式:
- #include <iostream>
- using std::cout;
- struct Handle {
- virtual void process(int num) {}
- };
- struct Handle1 : public Handle {
- Handle1(Handle *processor) : processor_(processor) {}
- void process(int num) override {
- if (num >= 0 && num <= 10) {
- cout << "0-10 \n";
- } else {
- processor_->process(num);
- }
- }
- Handle *processor_;
- };
- struct Handle2 : public Handle {
- Handle2(Handle *processor) : processor_(processor) {}
- void process(int num) override {
- if (num >= 10 && num <= 20) {
- cout << "10-20 \n";
- } else {
- processor_->process(num);
- }
- }
- Handle *processor_;
- };
- struct Handle3 : public Handle {
- Handle3(Handle *processor) : processor_(processor) {}
- void process(int num) override {
- if (num >= 20 && num <= 30) {
- cout << "20-30 \n";
- } else {
- cout << "not handle \n";
- }
- }
- Handle *processor_;
- };
- int main() {
- Handle *handle3 = new Handle3(nullptr);
- Handle *handle2 = new Handle2(handle3);
- Handle *handle1 = new Handle2(handle2);
- handle1->process(24);
- handle1->process(54);
- return 0;
- }
- 三目運(yùn)算符:某些簡(jiǎn)單情況下可以使用三目運(yùn)算符消滅if-else,例如:
- int func(int num) {
- if (num > 20) return 1;
- else return 0;
- }
三目運(yùn)算符:某些簡(jiǎn)單情況下可以使用三目運(yùn)算符消滅if-else,例如:
- int func(int num) {
- if (num > 20) return 1;
- else return 0;
- }
可以改為:
- int func(int num) {
- return num > 20 ? 1 : 0;
- }
這樣是不是代碼也更清晰了一些。
else-if消除:有時(shí)候有些人寫(xiě)的代碼確實(shí)就是這樣,例如:
- int func(int num) {
- int ret = 0;
- if (num == 1) {
- ret = 3;
- } else if (num == 2) {
- ret = 5;
- } else {
- ret = 6;
- }
- return ret;
- }
是不是可以考慮改為:
- int func(int num) {
- if (num == 1) return 3;
- if (num == 2) return 5;
- return 6;
- }