我期待幹出下面的C代碼:DRY緩存
if (i < SIZE_OF_V) {
if (V[i])
K = V[d];
else
K = V[d] = (expensive complicated call);
} else {
K = (expensive complicated call);
}
有什麼建議?一個本地宏可以工作,但我更喜歡更現代的選擇。
我期待幹出下面的C代碼:DRY緩存
if (i < SIZE_OF_V) {
if (V[i])
K = V[d];
else
K = V[d] = (expensive complicated call);
} else {
K = (expensive complicated call);
}
有什麼建議?一個本地宏可以工作,但我更喜歡更現代的選擇。
你可以簡單地做
if (i < SIZE_OF_V && V[i]) {
K = V[d];
} else {
K = (expensive complicated call);
if (i < SIZE_OF_V)
V[d] = K;
}
你也可以把你的expensive complicated call
在外部功能和CALLIT從兩個地方你if
條件內:
int myExpensiveAndComplicatedCall() {
// Do things
}
if (i < SIZE_OF_V) {
if (V[i])
K = V[d];
else
K = V[d] = myExpensiveAndComplicatedCall();
} else {
K = myExpensiveAndComplicatedCall();
}
我沒有得到這個詞'DRY 「你可以請嗎? – Omkant
@Omkant DRY:「不要重複自己」 – Paulpro
呵呵哈哈......這樣的縮寫..謝謝@ ascii-lime – Omkant