C++核心準(zhǔn)則ES.87:不要在條件語(yǔ)句中增加多余的==或!=

ES.87: Don't add redundant?==?or?!=?to conditions
ES.87:不要在條件語(yǔ)句中增加多余的==或!=
Reason(原因)
Doing so avoids verbosity and eliminates some opportunities for mistakes. Helps make style consistent and conventional.
這么做可以避免冗長(zhǎng)的代碼并且減少某些錯(cuò)誤的機(jī)會(huì)。幫助提高代碼的以執(zhí)行并符合習(xí)慣。
Example(示例)
By definition, a condition in an?if-statement,?while-statement, or a?for-statement selects between?true?and?false. A numeric value is compared to?0?and a pointer value to?nullptr.
從定義的角度來(lái)講,if語(yǔ)句、while語(yǔ)句、for語(yǔ)句中的條件判斷得到true或false的結(jié)果。數(shù)值和0比較,指針和nullptr進(jìn)行比較。
// These all mean "if `p` is not `nullptr`"
if (p) { ... } // good
if (p != 0) { ... } // redundant `!=0`; bad: don't use 0 for pointers
if (p != nullptr) { ... } // redundant `!=nullptr`, not recommended
Often,?if (p)?is read as "if?p?is valid" which is a direct expression of the programmers intent, whereas?if (p != nullptr)?would be a long-winded workaround.
通常,if(p)被讀作如果p是合法的,這是程序員意圖的直接表達(dá),而if(p != nullptr)卻是一種冗長(zhǎng)的表達(dá)方式。
Example(示例)
This rule is especially useful when a declaration is used as a condition
本規(guī)則在聲明作為條件使用時(shí)特別有用。
if (auto pc = dynamic_cast(ps)) { ... } // execute if ps points to a kind of Circle, good
if (auto pc = dynamic_cast(ps); pc != nullptr) { ... } // not recommended Example(示例)
Note that implicit conversions to bool are applied in conditions. For example:
注意可以隱式類型轉(zhuǎn)換為布爾類型的運(yùn)算都可以用于條件語(yǔ)句。例如S:
for (string s; cin >> s; ) v.push_back(s);
This invokes?istream's?operator bool().
這段代碼利用了istream的bool()運(yùn)算符。
Note(注意)
Explicit comparison of an integer to?0?is in general not redundant. The reason is that (as opposed to pointers and Booleans) an integer often has more than two reasonable values. Furthermore?0?(zero) is often used to indicate success. Consequently, it is best to be specific about the comparison.S:
將整數(shù)和0進(jìn)行顯示比較通常不是冗長(zhǎng)形式。原因是(和指針和布爾類型不同,)整數(shù)通??梢员磉_(dá)多于兩個(gè)有意義的值。另外通常使用0(zero)表示成功。因此,最好將整數(shù)比較作為特例。
void f(int i)
{
if (i) // suspect
// ...
if (i == success) // possibly better
// ...
}
Always remember that an integer can have more than two values.
一定記住整數(shù)可以擁有的有效值可以超過(guò)兩個(gè)。
Example, bad(反面示例)
It has been noted that
已經(jīng)提醒過(guò)了:
if(strcmp(p1, p2)) { ... } // are the two C-style strings equal? (mistake!)
is a common beginners error. If you use C-style strings, you must know the?
這是一個(gè)常見(jiàn)的,初學(xué)者錯(cuò)誤。如果你使用C風(fēng)格字符串,以一定知道
if(strcmp(p1, p2) != 0) { ... } // are the two C-style strings equal? (mistake!)
would not in itself save you.
也沒(méi)什么幫助。
Note(注意)
The opposite condition is most easily expressed using a negation:
使用!更容易表達(dá)反邏輯:
// These all mean "if `p` is `nullptr`"
if (!p) { ... } // good
if (p == 0) { ... } // redundant `== 0`; bad: don't use `0` for pointers
if (p == nullptr) { ... } // redundant `== nullptr`, not recommendedEnforcement(實(shí)施建議)
Easy, just check for redundant use of?!=?and?==?in conditions.
容易,只需要檢查條件語(yǔ)句中多余的!=和==。
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es87-dont-add-redundant--or--to-conditions
覺(jué)得本文有幫助?請(qǐng)分享給更多人。
關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_(kāi)發(fā),面向?qū)ο笏伎迹?/span>
