C++核心准则ES.87:不要在条件语句中增加多余的==或!=

面向对象思考

共 3031字,需浏览 7分钟

 ·

2020-06-10 23:21

0ec939b8c50ffa2f4b62badb5dcb5f84.webp

ES.87: Don't add redundant == or != to conditions

ES.87:不要在条件语句中增加多余的==或!=


Reason(原因)

Doing so avoids verbosity and eliminates some opportunities for mistakes. Helps make style consistent and conventional.

这么做可以避免冗长的代码并且减少某些错误的机会。帮助提高代码的以执行并符合习惯。


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.

从定义的角度来讲,if语句、while语句、for语句中的条件判断得到true或false的结果。数值和0比较,指针和nullptr进行比较。

// 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是合法的,这是程序员意图的直接表达,而if(p != nullptr)却是一种冗长的表达方式。


Example(示例)

This rule is especially useful when a declaration is used as a condition

本规则在声明作为条件使用时特别有用。

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:

注意可以隐式类型转换为布尔类型的运算都可以用于条件语句。例如S:

for (string s; cin >> s; ) v.push_back(s);

This invokes istream's operator bool().

这段代码利用了istream的bool()运算符。


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:

将整数和0进行显示比较通常不是冗长形式。原因是(和指针和布尔类型不同,)整数通常可以表达多于两个有意义的值。另外通常使用0(zero)表示成功。因此,最好将整数比较作为特例。

void f(int i)
{
if (i) // suspect
// ...
if (i == success) // possibly better
// ...
}

Always remember that an integer can have more than two values.

一定记住整数可以拥有的有效值可以超过两个。


Example, bad(反面示例)

It has been noted that

已经提醒过了:

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  functions well. Being verbose and writing

这是一个常见的,初学者错误。如果你使用C风格字符串,以一定知道函数。保持冗长并书写

if(strcmp(p1, p2) != 0) { ... }   // are the two C-style strings equal? (mistake!)

would not in itself save you.

也没什么帮助。


Note(注意)

The opposite condition is most easily expressed using a negation:

使用!更容易表达反逻辑:

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

Enforcement(实施建议)

Easy, just check for redundant use of != and == in conditions.

容易,只需要检查条件语句中多余的!=和==。


原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es87-dont-add-redundant--or--to-conditions




觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!


浏览 18
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报